Skip to content
This repository was archived by the owner on Aug 21, 2023. It is now read-only.

Commit 40f1da3

Browse files
committed
adds deactivateInputFilterAction to droplist
1 parent 6aae21a commit 40f1da3

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

src/components/DropList/DropList.Combobox.jsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ function Combobox({
3333
customEmptyList = null,
3434
customEmptyListItems,
3535
'data-cy': dataCy = `DropList.${VARIANTS.COMBOBOX}`,
36+
deactivateInputFilterAction,
3637
focusToggler = noop,
3738
handleSelectedItemChange = noop,
3839
inputPlaceholder = 'Search',
@@ -86,6 +87,8 @@ function Combobox({
8687
},
8788

8889
onInputValueChange({ inputValue }) {
90+
if (deactivateInputFilterAction) return
91+
8992
let filtered = filterItems(items, inputValue, actionItemRef)
9093
const isListEmpty = filtered.length === 0
9194

@@ -216,6 +219,12 @@ function Combobox({
216219
onFocus: event => {
217220
onMenuFocus(event)
218221
},
222+
onChange: event => {
223+
if (deactivateInputFilterAction) {
224+
event.persist()
225+
onInputChange(event.target.value, inputFilteredItems, event)
226+
}
227+
},
219228
onKeyDown: event => {
220229
if (event.key === 'Tab') {
221230
toggleOpenedState(false)

src/components/DropList/DropList.jsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ function DropListManager({
4141
customEmptyList = null,
4242
customEmptyListItems,
4343
'data-cy': dataCy,
44+
deactivateInputFilterAction = false,
4445
enableLeftRightNavigation = false,
4546
focusTogglerOnMenuClose = true,
4647
getTippyInstance = noop,
@@ -284,6 +285,7 @@ function DropListManager({
284285
customEmptyList={customEmptyList}
285286
customEmptyListItems={customEmptyListItems}
286287
data-cy={dataCy}
288+
deactivateInputFilterAction={deactivateInputFilterAction}
287289
enableLeftRightNavigation={enableLeftRightNavigation}
288290
focusToggler={focusToggler}
289291
handleSelectedItemChange={handleSelectedItemChange}
@@ -355,6 +357,8 @@ DropListManager.propTypes = {
355357
),
356358
/** Data attr applied to the DropList for Cypress tests. By default one of 'DropList.Select' or 'DropList.Combobox' depending on the variant used */
357359
'data-cy': PropTypes.string,
360+
/** On combobox, deactivate the default action of filtering so you can use the input as you please (like search) */
361+
deactivateInputFilterAction: PropTypes.bool,
358362
/** Enable navigation with Right and Left arrows (useful for horizontally rendered lists) */
359363
enableLeftRightNavigation: PropTypes.bool,
360364
/** Automatically moves the focus back to the toggler when the DropList is closed */
@@ -375,7 +379,7 @@ DropListManager.propTypes = {
375379
menuCSS: PropTypes.any,
376380
/** Custom width for the Menu */
377381
menuWidth: PropTypes.any,
378-
/** Callback that fires when combobox search input changes, gives acces to value and resulting filtered items `onInputChange(value, filteredItems)` */
382+
/** Callback that fires when combobox search input changes, gives acces to value, resulting filtered items and event (if deactivateInputFilterAction is on) `onInputChange(value, filteredItems, event)` */
379383
onInputChange: PropTypes.func,
380384
/** Callback that fires when the menu loses focus */
381385
onMenuBlur: PropTypes.func,

src/components/DropList/DropList.stories.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ You can use the `autoSetComboboxAt` prop to automatically choose between the 2 v
248248
onDropListLeave={() => {
249249
console.log('DropList left')
250250
}}
251+
onInputChange={(value, e) => {
252+
console.log('🚀 ~ value', value)
253+
console.log('🚀 ~ e', e)
254+
}}
251255
items={select(
252256
'Items',
253257
{

src/components/DropList/DropList.test.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,13 +582,33 @@ describe('Combobox', () => {
582582
)
583583

584584
user.type(getByPlaceholderText('Search'), 'J')
585-
586585
expect(onInputChangeSpy).toHaveBeenCalledWith('J', [
587586
{ label: 'John' },
588587
{ label: 'Jeff' },
589588
])
590589
})
591590

591+
test('should return value, items and event onInputChange if deactivateInputFilterAction on', () => {
592+
const onInputChangeSpy = jest.fn()
593+
const { getByPlaceholderText } = render(
594+
<DropList
595+
isMenuOpen
596+
deactivateInputFilterAction
597+
items={someItems}
598+
toggler={<SimpleButton text="Button Toggler" />}
599+
variant="combobox"
600+
onInputChange={onInputChangeSpy}
601+
/>
602+
)
603+
604+
user.type(getByPlaceholderText('Search'), 'J')
605+
expect(onInputChangeSpy).toHaveBeenCalledWith(
606+
'J',
607+
someItems,
608+
expect.anything()
609+
)
610+
})
611+
592612
test('should hide the search input on combobox if list empty', () => {
593613
const { queryByRole, getByPlaceholderText } = render(
594614
<DropList

0 commit comments

Comments
 (0)