Skip to content

Commit

Permalink
Merge pull request #160 from web3ui/feat/SelectValue
Browse files Browse the repository at this point in the history
Feat/select controlled value
  • Loading branch information
locothedev authored Feb 27, 2022
2 parents 1647ee6 + d92745f commit 951b7f0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/components/Select/Select.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,11 @@ Nodata.args = {
label: 'Label Text',
defaultOptionIndex: 0,
};

export const ControlledValue = Template.bind({});
ControlledValue.args = {
options: smallOptionsList,
onChange: onTestOptionChange,
label: 'Label Text',
value: 'txt',
};
11 changes: 11 additions & 0 deletions src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const {
const Select: React.FC<SelectProps> = ({
defaultOptionIndex,
disabled = false,
value,
errorMessage = '',
id = String(Date.now()),
label,
Expand Down Expand Up @@ -62,6 +63,16 @@ const Select: React.FC<SelectProps> = ({
document.removeEventListener('click', handleClickOutside);
};
}, []);
useEffect(() => {
if (value) {
const valueOptionItem = options.find(
(optionItem) => optionItem.id == value,
);
setSelectedOptionIndex(
valueOptionItem ? options.indexOf(valueOptionItem) : 0,
);
}
}, [selectedOptionIndex, value]);

return (
<DivStyledWrapper
Expand Down
5 changes: 5 additions & 0 deletions src/components/Select/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export interface SelectProps {
*/
label?: string;

/**
* Id of the Option that you want the Select to have. Use to control the value of the option
*/
value?: string;

/**
* standard onChange that returns the entire event, as normal you can access event.target
*/
Expand Down

0 comments on commit 951b7f0

Please sign in to comment.