From b87afb1383fe5e0178ca1323280f12b3b0610910 Mon Sep 17 00:00:00 2001 From: Yangshun Tay Date: Wed, 12 Oct 2022 18:57:37 +0800 Subject: [PATCH] [ui][select] add errorMessage and placeholder --- apps/storybook/stories/select.stories.tsx | 100 +++++++++++++++++----- packages/ui/src/Select/Select.tsx | 29 ++++++- 2 files changed, 106 insertions(+), 23 deletions(-) diff --git a/apps/storybook/stories/select.stories.tsx b/apps/storybook/stories/select.stories.tsx index 35c74362..7cfe1187 100644 --- a/apps/storybook/stories/select.stories.tsx +++ b/apps/storybook/stories/select.stories.tsx @@ -14,6 +14,9 @@ export default { control: { type: 'select' }, options: SelectDisplays, }, + errorMessage: { + control: 'text', + }, isLabelHidden: { control: 'boolean', }, @@ -23,6 +26,9 @@ export default { name: { control: 'text', }, + placeholder: { + control: 'text', + }, }, component: Select, title: 'Select', @@ -181,28 +187,78 @@ export function Required() { const [value, setValue] = useState('apple'); return ( -
- + ); +} + +export function Placeholder() { + return ( + ); } diff --git a/packages/ui/src/Select/Select.tsx b/packages/ui/src/Select/Select.tsx index d3af3e00..d23935a9 100644 --- a/packages/ui/src/Select/Select.tsx +++ b/packages/ui/src/Select/Select.tsx @@ -20,11 +20,13 @@ type Props = Readonly<{ borderStyle?: SelectBorderStyle; defaultValue?: T; display?: SelectDisplay; + errorMessage?: string; isLabelHidden?: boolean; label: string; name?: string; onChange?: (value: string) => void; options: ReadonlyArray>; + placeholder?: string; value?: T; }> & Readonly; @@ -34,15 +36,25 @@ const borderClasses: Record = { borderless: 'border-transparent bg-transparent', }; +type State = 'error' | 'normal'; + +const stateClasses: Record = { + error: + 'border-danger-300 text-danger-900 placeholder-danger-300 focus:outline-none focus:ring-danger-500 focus:border-danger-500', + normal: 'focus:border-primary-500 focus:ring-primary-500', +}; + function Select( { borderStyle = 'bordered', defaultValue, display, disabled, + errorMessage, label, isLabelHidden, options, + placeholder, required, value, onChange, @@ -50,7 +62,10 @@ function Select( }: Props, ref: ForwardedRef, ) { + const hasError = errorMessage != null; const id = useId(); + const errorId = useId(); + const state: State = hasError ? 'error' : 'normal'; return (
@@ -69,10 +84,12 @@ function Select( )} + {errorMessage && ( +

+ {errorMessage} +

+ )}
); }