diff --git a/src/components/input/inputContainer/inputContainer.tsx b/src/components/input/inputContainer/inputContainer.tsx index bf1385ed0..8c341331a 100644 --- a/src/components/input/inputContainer/inputContainer.tsx +++ b/src/components/input/inputContainer/inputContainer.tsx @@ -70,7 +70,7 @@ export const InputContainer: React.FC = (props) => { )}
{children}
- {maxLength && ( + {maxLength != null && (

[{inputLength}/{maxLength}]

diff --git a/src/components/input/inputText/inputText.stories.tsx b/src/components/input/inputText/inputText.stories.tsx index 921dda578..6d85a7040 100644 --- a/src/components/input/inputText/inputText.stories.tsx +++ b/src/components/input/inputText/inputText.stories.tsx @@ -1,5 +1,6 @@ import type { Meta, StoryObj } from '@storybook/react'; -import { InputText } from './inputText'; +import { useState, type ChangeEvent } from 'react'; +import { InputText, type IInputTextProps } from './inputText'; const meta: Meta = { title: 'components/Input/InputText', @@ -16,11 +17,29 @@ const meta: Meta = { type Story = StoryObj; /** - * Default usage example of the InputText component. + * Default uncontrolled usage example of the InputText component. */ export const Default: Story = { args: { - placeholder: 'Placeholder', + placeholder: 'Uncontrolled input', + }, +}; + +const ControlledInput = (props: IInputTextProps) => { + const [value, setValue] = useState(''); + + const handleChange = (event: ChangeEvent) => setValue(event.target.value); + + return ; +}; + +/** + * Usage example of a controlled input. + */ +export const Controlled: Story = { + render: (props) => , + args: { + placeholder: 'Controlled input', }, }; diff --git a/src/components/input/inputText/inputText.tsx b/src/components/input/inputText/inputText.tsx index 8ab65a428..f62776c50 100644 --- a/src/components/input/inputText/inputText.tsx +++ b/src/components/input/inputText/inputText.tsx @@ -1,4 +1,4 @@ -import { useRef } from 'react'; +import { useState, type ChangeEvent } from 'react'; import { InputContainer, type IInputComponentProps } from '../inputContainer'; import { useInputProps } from '../useInputProps'; @@ -6,20 +6,18 @@ export interface IInputTextProps extends IInputComponentProps {} export const InputText: React.FC = (props) => { const { containerProps, inputProps } = useInputProps(props); - const { value, ...otherInputProps } = inputProps; + const { value, onChange, ...otherInputProps } = inputProps; - const inputRef = useRef(null); - const inputLength = value?.toString().length ?? inputRef.current?.value.length; + const [inputLength, setInputLength] = useState(0); - console.log({ - inputLength, - valueLength: inputProps.value?.toString().length, - refLength: inputRef.current?.value.length, - }); + const handleOnChange = (event: ChangeEvent) => { + setInputLength(event.target.value.length); + onChange?.(event); + }; return ( - + ); };