Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow disabling selected item events #1297

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/hooks/useMultipleSelection/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,10 @@ It is required to pass either `selectedItem` or `index` to

Optional properties:

- `disabled`: If this is set to true, then no event handlers will be returned
from `getSelectedItemProps` and a `disabled` prop will be returned
(effectively disabling `Delete` and `Backspace` keys events).

- `ref`: if you need to access the dropdown element via a ref object, you'd call
the function like this: `getDropdown({ref: yourDropdownRef})`. As a result,
the dropdown element will receive a composed `ref` property, which guarantees
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ describe('getSelectedItemProps', () => {

expect(itemProps.tabIndex).toEqual(0)
})

test("handlers are not called if it's disabled", () => {
const {result} = renderUseMultipleSelection()
const itemProps = result.current.getSelectedItemProps({
disabled: true,
index: 0,
selectedItem: items[0],
})

expect(itemProps.onChange).toBeUndefined()
expect(itemProps.onKeyDown).toBeUndefined()
expect(itemProps.onBlur).toBeUndefined()
expect(itemProps.disabled).toBe(true)
})
})

describe('user props', () => {
Expand Down
6 changes: 4 additions & 2 deletions src/hooks/useMultipleSelection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,10 @@ function useMultipleSelection(userProps = {}) {
}
}),
tabIndex: index === latestState.activeIndex ? 0 : -1,
onClick: callAllEventHandlers(onClick, selectedItemHandleClick),
onKeyDown: callAllEventHandlers(onKeyDown, selectedItemHandleKeyDown),
...(!rest.disabled && {
onClick: callAllEventHandlers(onClick, selectedItemHandleClick),
onKeyDown: callAllEventHandlers(onKeyDown, selectedItemHandleKeyDown),
}),
...rest,
}
},
Expand Down
1 change: 1 addition & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@ export interface A11yRemovalMessage<Item> {
export interface UseMultipleSelectionGetSelectedItemPropsOptions<Item>
extends React.HTMLProps<HTMLElement>,
GetPropsWithRefKey {
disabled?: boolean
index?: number
selectedItem: Item
}
Expand Down