@workday/canvas-kit-react
Version:
The parent module that contains all Workday Canvas Kit React components
27 lines (26 loc) • 961 B
JavaScript
import { createElemPropsHook } from '@workday/canvas-kit-react/common';
import { isElementDisabled } from './isElementDisabled';
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 (isElementDisabled(event.currentTarget) || state.nonInteractiveIds.includes(name)) {
return null;
}
events.select({ id: name });
return undefined;
};
return { onClick };
});