Skip to content

Commit

Permalink
feat(search select input): add default value prop
Browse files Browse the repository at this point in the history
  • Loading branch information
Ddouglasz committed Apr 15, 2024
1 parent 2cad0e4 commit be21255
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ storiesOf('Components|Inputs', module)
option: dropdownValue,
};

const defaultValue = {
text: '',
options: [],
};

return (
<Section
backgroundColor={select(
Expand All @@ -112,6 +117,7 @@ storiesOf('Components|Inputs', module)
id={text('id', '')}
name={name}
value={value}
defaultValue={defaultValue}
onChange={(event) => {
action('onChange')(event);
if (event.target.name.endsWith('.textInput')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ export type TSelectableSearchInputProps = {
* Value of the input. Consists of text input and selected option.
*/
value: TValue;
/**
* Default value of the input. Consists of text input and selected option.
*/
defaultValue: TValue;
/**
* Called with the event of the input or dropdown when either the selectable dropdown or the text input have changed.
* The change event from the text input has a suffix of `.textInput` and the change event from the dropdown has a suffix of `.dropdown`.
Expand Down Expand Up @@ -254,7 +258,7 @@ const transformDataProps = (dataProps?: Record<string, string>) =>

const SelectableSearchInput = (props: TSelectableSearchInputProps) => {
const [dropdownHasFocus, toggleDropdownHasFocus] = useToggleState(false);
const [searchValue, setSearchValue] = useState(props.value.text || '');
const [searchValue, setSearchValue] = useState(props.defaultValue.text || '');
const containerRef = useRef<HTMLDivElement>(null);
const textInputRef = useRef<HTMLInputElement>(null);

Expand All @@ -264,7 +268,9 @@ const SelectableSearchInput = (props: TSelectableSearchInputProps) => {

// Ensure input state is always updated when the input changes
useEffect(() => {
setSearchValue(props.value.text);
if (props.value.text) {
setSearchValue(props.value.text);
}
}, [props.value.text]);

const optionsWithoutGroups = props.options.flatMap((option) => {
Expand Down

0 comments on commit be21255

Please sign in to comment.