From 4d280f0846b51ff86386f0ca023bef5d9dc00f14 Mon Sep 17 00:00:00 2001 From: Yangshun Tay Date: Sat, 29 Oct 2022 18:21:59 +0800 Subject: [PATCH] [ui][text input] add description prop --- apps/storybook/stories/text-input.stories.tsx | 22 +++++++++++++++++++ packages/ui/src/TextInput/TextInput.tsx | 19 +++++++++++----- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/apps/storybook/stories/text-input.stories.tsx b/apps/storybook/stories/text-input.stories.tsx index b4c5943b..8ace0518 100644 --- a/apps/storybook/stories/text-input.stories.tsx +++ b/apps/storybook/stories/text-input.stories.tsx @@ -12,6 +12,9 @@ export default { autoComplete: { control: 'text', }, + description: { + control: 'text', + }, disabled: { control: 'boolean', }, @@ -183,6 +186,25 @@ export function Error() { ); } +export function Description() { + const [value, setValue] = useState('1234567'); + + return ( + + ); +} + export function AddOns() { return (
diff --git a/packages/ui/src/TextInput/TextInput.tsx b/packages/ui/src/TextInput/TextInput.tsx index 28d90bce..6765f132 100644 --- a/packages/ui/src/TextInput/TextInput.tsx +++ b/packages/ui/src/TextInput/TextInput.tsx @@ -62,6 +62,7 @@ type EndAddOnProps = type BaseProps = Readonly<{ defaultValue?: string; + description?: React.ReactNode; endIcon?: React.ComponentType>; errorMessage?: React.ReactNode; id?: string; @@ -99,6 +100,7 @@ const stateClasses: Record< function TextInput( { defaultValue, + description, disabled, endAddOn, endAddOnType, @@ -119,7 +121,7 @@ function TextInput( const hasError = errorMessage != null; const generatedId = useId(); const id = idParam ?? generatedId; - const errorId = useId(); + const messageId = useId(); const state: State = hasError ? 'error' : 'normal'; const { input: inputClass, container: containerClass } = stateClasses[state]; @@ -175,7 +177,9 @@ function TextInput( })()} - {errorMessage && ( -

- {errorMessage} + {(errorMessage ?? description) && ( +

+ {errorMessage ?? description}

)}