-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from tinloof/tin-2666-characters-count-component
Input with characters count component
- Loading branch information
Showing
4 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
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,5 @@ | ||
--- | ||
"@tinloof/sanity-studio": minor | ||
--- | ||
|
||
Input with characters count component |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { IconSelectComponent } from "./IconSelectComponent"; | ||
export { InputWithCharacterCount } from "./input-with-characters-count"; | ||
export { PathnameFieldComponent } from "./PathnameFieldComponent"; |
70 changes: 70 additions & 0 deletions
70
packages/sanity-studio/src/components/input-with-characters-count.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,70 @@ | ||
import type { BadgeTone } from "@sanity/ui"; | ||
import { Badge, Flex, Stack } from "@sanity/ui"; | ||
import type { TextInputProps, TextOptions } from "sanity"; | ||
import { useFormValue } from "sanity"; | ||
|
||
type CountedTextOptions = { | ||
maxLength?: number; | ||
minLength?: number; | ||
} & TextOptions; | ||
|
||
function CharacterCount(props: { value?: string } & CountedTextOptions) { | ||
if (!props.maxLength && !props.minLength) { | ||
return null; | ||
} | ||
|
||
const { value = "" } = props; | ||
|
||
const maxPercentage = | ||
props.maxLength && (value.length / props.maxLength) * 100; | ||
|
||
let tone: BadgeTone = "primary"; | ||
|
||
if (maxPercentage && maxPercentage > 100) { | ||
tone = "critical"; | ||
} else if (maxPercentage && maxPercentage > 75) { | ||
tone = "caution"; | ||
} | ||
|
||
if (props.minLength && value.length < props.minLength) { | ||
tone = "caution"; | ||
} | ||
return ( | ||
<Badge tone={tone}> | ||
{value.length} / {props.maxLength} | ||
</Badge> | ||
); | ||
} | ||
|
||
export function InputWithCharacterCount(props: TextInputProps): JSX.Element { | ||
const document = useFormValue([]); | ||
|
||
if (!document) { | ||
return props.renderDefault(props); | ||
} | ||
|
||
const { name, title } = document as { | ||
name?: string; | ||
title?: string; | ||
}; | ||
|
||
let defaultTitle: string | undefined = undefined; | ||
|
||
if (props.id === "seo.title") { | ||
defaultTitle = title ?? name ?? undefined; | ||
} | ||
|
||
props.elementProps.placeholder = defaultTitle; | ||
|
||
return ( | ||
<Stack space={2}> | ||
{props.renderDefault(props)} | ||
<Flex justify="flex-end"> | ||
<CharacterCount | ||
value={props.value} | ||
{...((props.schemaType.options || {}) as CountedTextOptions)} | ||
/> | ||
</Flex> | ||
</Stack> | ||
); | ||
} |