Skip to content

Commit

Permalink
feat(studio): custom string input component with character count
Browse files Browse the repository at this point in the history
Composes the default string input rendering with a simple character count. Initially only used in SEO fields, but can be later applied where applicable.
  • Loading branch information
mathiazom committed Aug 27, 2024
1 parent 79da6d5 commit 41c49eb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
15 changes: 15 additions & 0 deletions studio/components/StringInputWithCharacterCount.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Box, Stack, Text } from "@sanity/ui";
import { StringInputProps } from "sanity";

export const StringInputWithCharacterCount = (props: StringInputProps) => {
const characterCount = props.value?.length ?? 0;

return (
<Stack space={3}>
<Box>{props.renderDefault(props)}</Box>
<Text size={1} muted>
{characterCount} characters
</Text>
</Stack>
);
};
7 changes: 7 additions & 0 deletions studio/schemas/objects/seo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { defineField } from "sanity";
import { StringInputWithCharacterCount } from "studio/components/StringInputWithCharacterCount";

const seoFieldID = {
title: "seoTitle",
Expand Down Expand Up @@ -28,6 +29,9 @@ const seo = defineField({
.error("A title of minimum 15 characters is required"),
Rule.max(70).error("A title cannot exceed 70 characters"),
],
components: {
input: StringInputWithCharacterCount,
},
}),
defineField({
name: seoFieldID.description,
Expand All @@ -43,6 +47,9 @@ const seo = defineField({
"A description of more than 160 characters has a lower chance of converting visitors",
),
],
components: {
input: StringInputWithCharacterCount,
},
}),
defineField({
name: seoFieldID.keywords,
Expand Down

0 comments on commit 41c49eb

Please sign in to comment.