UNPKG

@workday/canvas-kit-react

Version:

The parent module that contains all Workday Canvas Kit React components

28 lines (27 loc) 1.28 kB
import React from 'react'; import { createElemPropsHook } from '@workday/canvas-kit-react/common'; import { useComboboxModel } from './useComboboxModel'; /** * If there is nothing selected, move cursor to the first item, otherwise, move the cursor to the selected item */ export const useComboboxMoveCursorToSelected = createElemPropsHook(useComboboxModel)(model => { React.useLayoutEffect(() => { var _a; const cursorItemId = (_a = model.state.items.find(item => item.id === model.state.cursorId)) === null || _a === void 0 ? void 0 : _a.id; // If there is no selected item and items exists we want to set the cursor to the first item in the array if (!model.state.selectedIds.length && model.state.items.length && !cursorItemId) { model.events.goTo({ id: model.state.items[0].id }); } else if (model.state.selectedIds.length && !cursorItemId) { // If the user wants an item selected by default by passing `initialSelectedId` we select that item model.events.goTo({ id: model.state.selectedIds[0] }); } }, [ model.state.selectedIds, model.events, model.state.items, model.state.cursorId, model.state.visibility, ]); return {}; });