diff --git a/src/components/Textarea/Textarea.stories.tsx b/src/components/Textarea/Textarea.stories.tsx index cef43f7010..e7e4884472 100644 --- a/src/components/Textarea/Textarea.stories.tsx +++ b/src/components/Textarea/Textarea.stories.tsx @@ -81,8 +81,8 @@ const Template: StoryFn = () => { value={value} onChange={onChangeValue} decorators={{ - beforeMaxLengthCount: (txt) => `entry maxLetters(${txt})`, - afterMaxLengthCount: (txt) => ` characters(${txt})`, + beforeMaxLetterCount: (txt) => `entry limit(${txt})`, + afterMaxLetterCount: (txt) => ` characters(${txt})`, }} /> diff --git a/src/components/Textarea/Textarea.tsx b/src/components/Textarea/Textarea.tsx index 4cf1525e2e..bd87587af1 100644 --- a/src/components/Textarea/Textarea.tsx +++ b/src/components/Textarea/Textarea.tsx @@ -31,7 +31,7 @@ type Props = { /** 入力可能な最大文字数。あと何文字入力できるかの表示が追加される。html的なvalidateは発生しない */ maxLetters?: number /** コンポーネント内の文言を変更するための関数を設定 */ - decorators?: DecoratorsType<'beforeMaxLengthCount' | 'afterMaxLengthCount'> + decorators?: DecoratorsType<'beforeMaxLetterCount' | 'afterMaxLetterCount'> /** * @deprecated placeholder属性は非推奨です。別途ヒント用要素の設置を検討してください。 */ @@ -52,8 +52,8 @@ const getStringLength = (value: string | number | readonly string[]) => { return formattedValue.length - (formattedValue.match(surrogatePairs) || []).length } -const TEXT_BEFORE_MAXlENGTH_COUNT = 'あと' -const TEXT_AFTER_MAXlENGTH_COUNT = '文字' +const TEXT_BEFORE_MAXLETTER_COUNT = 'あと' +const TEXT_AFTER_MAXLETTER_COUNT = '文字' const textarea = tv({ slots: { @@ -107,15 +107,15 @@ export const Textarea = forwardRef( const currentValue = props.defaultValue || props.value const [interimRows, setInterimRows] = useState(rows) const [count, setCount] = useState(currentValue ? getStringLength(currentValue) : 0) - const beforeMaxLengthCount = useMemo( + const beforeMaxLetterCount = useMemo( () => - decorators?.beforeMaxLengthCount?.(TEXT_BEFORE_MAXlENGTH_COUNT) || - TEXT_BEFORE_MAXlENGTH_COUNT, + decorators?.beforeMaxLetterCount?.(TEXT_BEFORE_MAXLETTER_COUNT) || + TEXT_BEFORE_MAXLETTER_COUNT, [decorators], ) - const afterMaxLengthCount = useMemo( + const afterMaxLetterCount = useMemo( () => - decorators?.afterMaxLengthCount?.(TEXT_AFTER_MAXlENGTH_COUNT) || TEXT_AFTER_MAXlENGTH_COUNT, + decorators?.afterMaxLetterCount?.(TEXT_AFTER_MAXLETTER_COUNT) || TEXT_AFTER_MAXLETTER_COUNT, [decorators], ) @@ -130,9 +130,15 @@ export const Textarea = forwardRef( } }, [autoFocus]) - const handleKeyup = useCallback((event: React.KeyboardEvent) => { - setCount(getStringLength(event.currentTarget.value)) - }, []) + const onKeyUp = useMemo( + () => + maxLetters + ? (event: React.KeyboardEvent) => { + setCount(getStringLength(event.currentTarget.value)) + } + : undefined, + [maxLetters], + ) const handleInput = useCallback( (e: React.ChangeEvent) => { if (!autoResize) { @@ -171,26 +177,30 @@ export const Textarea = forwardRef( } }, [className, count, error, maxLetters, width]) - return ( - <> - {/* eslint-disable-next-line smarthr/a11y-input-has-name-attribute */} -