Skip to content

Commit 08aa103

Browse files
author
Kamil Korczynski
committed
feat: allow disabling selected item events
1 parent ce6c4e1 commit 08aa103

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

src/hooks/useMultipleSelection/__tests__/getSelectedItemProps.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ describe('getSelectedItemProps', () => {
3232

3333
expect(itemProps.tabIndex).toEqual(0)
3434
})
35+
36+
test("handlers are not called if it's disabled", () => {
37+
const {result} = renderUseMultipleSelection()
38+
const itemProps = result.current.getSelectedItemProps({
39+
disabled: true,
40+
index: 0,
41+
selectedItem: items[0],
42+
})
43+
44+
expect(itemProps.onChange).toBeUndefined()
45+
expect(itemProps.onKeyDown).toBeUndefined()
46+
expect(itemProps.onBlur).toBeUndefined()
47+
expect(itemProps.disabled).toBe(true)
48+
})
3549
})
3650

3751
describe('user props', () => {

src/hooks/useMultipleSelection/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
getInitialState,
1313
defaultProps,
1414
isKeyDownOperationPermitted,
15-
validatePropTypes
15+
validatePropTypes,
1616
} from './utils'
1717
import downshiftMultipleSelectionReducer from './reducer'
1818
import * as stateChangeTypes from './stateChangeTypes'
@@ -190,8 +190,10 @@ function useMultipleSelection(userProps = {}) {
190190
}
191191
}),
192192
tabIndex: index === latestState.activeIndex ? 0 : -1,
193-
onClick: callAllEventHandlers(onClick, selectedItemHandleClick),
194-
onKeyDown: callAllEventHandlers(onKeyDown, selectedItemHandleKeyDown),
193+
...(!rest.disabled && {
194+
onClick: callAllEventHandlers(onClick, selectedItemHandleClick),
195+
onKeyDown: callAllEventHandlers(onKeyDown, selectedItemHandleKeyDown),
196+
}),
195197
...rest,
196198
}
197199
},

0 commit comments

Comments
 (0)