From a0bb72d0a39373c4fccc019a600a61492b4be658 Mon Sep 17 00:00:00 2001 From: rayyan224 Date: Sat, 26 Feb 2022 15:19:03 -0500 Subject: [PATCH 1/2] feat: Intial Implantiation of value feature --- src/components/Select/Select.stories.tsx | 9 +++++++++ src/components/Select/Select.tsx | 11 +++++++++++ src/components/Select/types.ts | 5 +++++ 3 files changed, 25 insertions(+) diff --git a/src/components/Select/Select.stories.tsx b/src/components/Select/Select.stories.tsx index 982aceee0..4f96b9c17 100644 --- a/src/components/Select/Select.stories.tsx +++ b/src/components/Select/Select.stories.tsx @@ -162,3 +162,12 @@ Nodata.args = { label: 'Label Text', defaultOptionIndex: 0, }; + +export const ControlledValue = Template.bind({}); +ControlledValue.args = { + options: smallOptionsList, + onChange: onTestOptionChange, + label: 'Label Text', + value: 'txt', + defaultOptionIndex: 0, +}; diff --git a/src/components/Select/Select.tsx b/src/components/Select/Select.tsx index 87054099e..70e10962f 100644 --- a/src/components/Select/Select.tsx +++ b/src/components/Select/Select.tsx @@ -22,6 +22,7 @@ const { const Select: React.FC = ({ defaultOptionIndex, disabled = false, + value, errorMessage = '', id = String(Date.now()), label, @@ -62,6 +63,16 @@ const Select: React.FC = ({ document.removeEventListener('click', handleClickOutside); }; }, []); + useEffect(() => { + if (value) { + const valueOptionItem = options.find( + (optionItem) => optionItem.id == value, + ); + setSelectedOptionIndex( + valueOptionItem ? options.indexOf(valueOptionItem) : 0, + ); + } + }, [selectedOptionIndex, value]); return ( Date: Sat, 26 Feb 2022 15:20:31 -0500 Subject: [PATCH 2/2] chore:remove default from story --- src/components/Select/Select.stories.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/Select/Select.stories.tsx b/src/components/Select/Select.stories.tsx index 4f96b9c17..428c0c207 100644 --- a/src/components/Select/Select.stories.tsx +++ b/src/components/Select/Select.stories.tsx @@ -169,5 +169,4 @@ ControlledValue.args = { onChange: onTestOptionChange, label: 'Label Text', value: 'txt', - defaultOptionIndex: 0, };