From 6a079012d879fc58dee242908770e41a14a4bd11 Mon Sep 17 00:00:00 2001 From: Ugo Stephant Date: Mon, 28 Nov 2022 14:33:47 +0100 Subject: [PATCH] fix(react): allow selectfield to not be searchable --- packages/react/lib/SelectField/index.js | 17 ++++-- .../react/lib/SelectField/index.stories.js | 12 ++++- .../react/lib/SelectField/index.test.js.snap | 52 +++++++++---------- 3 files changed, 48 insertions(+), 33 deletions(-) diff --git a/packages/react/lib/SelectField/index.js b/packages/react/lib/SelectField/index.js index 9c0b8a37d..a8dba5b79 100644 --- a/packages/react/lib/SelectField/index.js +++ b/packages/react/lib/SelectField/index.js @@ -40,6 +40,7 @@ const SelectField = forwardRef(({ disabled = false, multiple = false, noOptionsLabel = 'No options', + searchable = true, searchMinCharacters = 2, searchThreshold = 400, required = false, @@ -168,10 +169,15 @@ const SelectField = forwardRef(({ } state.valid = onValidate(parseValue(state.value), { required, multiple }); + onChange_({ close: false, resetSearch: true }); }; const onSearchInputChange = e => { + if (!searchable || disabled) { + return; + } + dispatch({ search: e.target.value }); }; @@ -181,7 +187,7 @@ const SelectField = forwardRef(({ if (onSearch) { dispatch({ searching: true }); results = await onSearch(state.search); - } else { + } else if (searchable) { results = filterOptions(state.search); } @@ -245,7 +251,7 @@ const SelectField = forwardRef(({ if (state.selectedItem >= 0) { onRemoveOption(state.value[state.selectedItem]); } else { - dispatch({ selectedItem: state.value.length - 1 }); + dispatch({ selectedItem: (state.value?.length ?? 0) - 1 }); } break; @@ -254,13 +260,13 @@ const SelectField = forwardRef(({ if (state.selectedItem > 0) { dispatch({ selectedItem: state.selectedItem - 1 }); } else if (state.selectedItem === -1 && !state.search) { - dispatch({ selectedItem: state.value.length - 1 }); + dispatch({ selectedItem: (state.value?.length ?? 0) - 1 }); } break; case 'ArrowRight': - if (state.selectedItem < state.value.length - 1) { + if (state.selectedItem < (state.value?.length ?? 0) - 1) { dispatch({ selectedItem: state.selectedItem + 1 }); } else if (state.selectedItem !== -1) { dispatch({ selectedItem: -1 }); @@ -379,6 +385,7 @@ const SelectField = forwardRef(({ state.dirty ? 'dirty' : 'pristine', !state.valid && state.dirty ? 'invalid' : 'valid', { + searchable, searching: state.searching, empty: isEmpty(), focused: state.focused, @@ -417,7 +424,7 @@ const SelectField = forwardRef(({ onChange={onSearchInputChange} ref={searchInputRef} autoFocus={autoFocus} - disabled={disabled} + disabled={disabled || !searchable} onFocus={onFocus_} onBlur={onBlur_} onKeyPress={onKeyPress_} diff --git a/packages/react/lib/SelectField/index.stories.js b/packages/react/lib/SelectField/index.stories.js index 2a26023ac..874242816 100644 --- a/packages/react/lib/SelectField/index.stories.js +++ b/packages/react/lib/SelectField/index.stories.js @@ -25,13 +25,21 @@ export const controlled = () => { value={value} placeholder="Type a name" options={['Item 1', 'Item 2', { title: 'Item 3', value: 'item-3' }]} - parseTitle={o => o.title || o} - parseValue={o => o.value || o} + parseTitle={o => o?.title || o} + parseValue={o => o?.value || o} onChange={field => setValue(field.value)} /> ); }; +export const notSearchable = () => ( + +); + export const autoFocused = () => ( should allow arbitrary values 1`] = `