-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v3 - custom string input with character count (#529)
* feat(studio): custom string input component with character count Composes the default string input rendering with a simple character count. Initially only used in SEO fields, but can be later applied where applicable. * feat(StringInputWithCharacterCount): max count * feat(StringInputWithCharacterCount): use character count input for more string fields * refactor(schema): rename Rule → rule in validation function * feat(StringInputWithCharacterCount): handle plural form * feat(StringInputWithCharacterCount): make maxCount required and show characters left
- Loading branch information
Showing
30 changed files
with
279 additions
and
115 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
studio/components/stringInputWithCharacterCount/StringInputWithCharacterCount.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Box, Stack, Text } from "@sanity/ui"; | ||
import { StringInputProps } from "sanity"; | ||
import styles from "./stringInputWithCharacterCount.module.css"; | ||
|
||
type StringInputWithCharacterCountProps = StringInputProps & { | ||
maxCount: number; | ||
}; | ||
|
||
const SR_COUNT_HINT_ID = "string-input-with-character-count-input-info"; | ||
|
||
export const StringInputWithCharacterCount = ({ | ||
maxCount, | ||
...defaultProps | ||
}: StringInputWithCharacterCountProps) => { | ||
const characterCount = defaultProps.value?.length ?? 0; | ||
const charactersLeft = maxCount - characterCount; | ||
const isOverLimit = charactersLeft < 0; | ||
const countText = `${Math.abs(charactersLeft)} character${charactersLeft !== 1 ? "s" : ""} ${isOverLimit ? "over limit" : charactersLeft === maxCount ? "allowed" : "left"}`; | ||
|
||
return ( | ||
<Stack space={3}> | ||
<Box> | ||
{defaultProps.renderDefault({ | ||
...defaultProps, | ||
elementProps: { | ||
...defaultProps.elementProps, | ||
"aria-describedby": `${defaultProps.elementProps["aria-describedby"]} ${SR_COUNT_HINT_ID}`, | ||
}, | ||
})} | ||
</Box> | ||
<span id={SR_COUNT_HINT_ID} className={styles.srOnly}> | ||
{`You can enter up to ${maxCount} character${charactersLeft !== 1 ? "s" : ""}`} | ||
</span> | ||
<Text | ||
size={1} | ||
muted | ||
className={isOverLimit ? styles.overLimit : ""} | ||
aria-live={"polite"} | ||
> | ||
{countText} | ||
</Text> | ||
</Stack> | ||
); | ||
}; |
13 changes: 13 additions & 0 deletions
13
studio/components/stringInputWithCharacterCount/stringInputWithCharacterCount.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
.srOnly { | ||
/* visually hidden input to still receive focus and support screen readers */ | ||
/* https://developer.mozilla.org/en-US/docs/Web/API/File_API/Using_files_from_web_applications#using_a_label_element_to_trigger_a_hidden_file_input_element */ | ||
position: absolute !important; | ||
height: 1px; | ||
width: 1px; | ||
overflow: hidden; | ||
clip: rect(1px, 1px, 1px, 1px); | ||
} | ||
|
||
.overLimit { | ||
color: rgb(203, 38, 24) !important; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.