import React from 'react'; 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: {'data-id'?: string} = {} ) => { const name = elemProps['data-id'] || ''; const onClick = (event: React.MouseEvent) => { if (isElementDisabled(event.currentTarget) || state.nonInteractiveIds.includes(name)) { return null; } events.select({id: name}); return undefined; }; return {onClick}; });