Skip to content

Commit

Permalink
feat(StringInputWithCharacterCount): max count
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiazom committed Sep 10, 2024
1 parent c837800 commit 0d4e537
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
16 changes: 12 additions & 4 deletions studio/components/StringInputWithCharacterCount.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { Box, Stack, Text } from "@sanity/ui";
import { StringInputProps } from "sanity";

export const StringInputWithCharacterCount = (props: StringInputProps) => {
const characterCount = props.value?.length ?? 0;
type StringInputWithCharacterCountProps = StringInputProps & {
maxCount?: number;
};

export const StringInputWithCharacterCount = ({
maxCount,
...defaultProps
}: StringInputWithCharacterCountProps) => {
const characterCount = defaultProps.value?.length ?? 0;

return (
<Stack space={3}>
<Box>{props.renderDefault(props)}</Box>
<Box>{defaultProps.renderDefault(defaultProps)}</Box>
<Text size={1} muted>
{characterCount} characters
{characterCount}
{maxCount && `/${maxCount}`} characters
</Text>
</Stack>
);
Expand Down
6 changes: 4 additions & 2 deletions studio/schemas/objects/seo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const seo = defineField({
Rule.max(70).error("A title cannot exceed 70 characters"),
],
components: {
input: StringInputWithCharacterCount,
input: (props) =>
StringInputWithCharacterCount({ ...props, maxCount: 70 }),
},
}),
defineField({
Expand All @@ -48,7 +49,8 @@ const seo = defineField({
),
],
components: {
input: StringInputWithCharacterCount,
input: (props) =>
StringInputWithCharacterCount({ ...props, maxCount: 160 }),
},
}),
defineField({
Expand Down

0 comments on commit 0d4e537

Please sign in to comment.