@workday/canvas-kit-react
Version:
The parent module that contains all Workday Canvas Kit React components
25 lines (24 loc) • 892 B
JavaScript
import { createElemPropsHook } from '@workday/canvas-kit-react/common';
import { useListModel } from './useListModel';
/**
* This elemProps hook adds selection support to a `*.Item` subcomponent of a collection. It adds a
* click handler that toggles selection status according to the [Selection
* Manager](#selection-manager) used.
*
* ```ts
* const useMyItem = composeHooks(
* useListItemSelect, // adds selection support to an item
* useListItemRegister
* );
* ```
*/
export const useListItemSelect = createElemPropsHook(useListModel)(({ state, events }, _, elemProps = {}) => {
const name = elemProps['data-id'] || '';
const onClick = (event) => {
if (!state.nonInteractiveIds.includes(name) &&
event.currentTarget.getAttribute('aria-disabled') !== 'true') {
events.select({ id: name });
}
};
return { onClick };
});