UNPKG

@workday/canvas-kit-react

Version:

The parent module that contains all Workday Canvas Kit React components

34 lines (33 loc) 1.6 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.useComboboxMoveCursorToSelected = void 0; const react_1 = __importDefault(require("react")); const common_1 = require("@workday/canvas-kit-react/common"); const useComboboxModel_1 = require("./useComboboxModel"); /** * If there is nothing selected, move cursor to the first item, otherwise, move the cursor to the selected item */ exports.useComboboxMoveCursorToSelected = (0, common_1.createElemPropsHook)(useComboboxModel_1.useComboboxModel)(model => { react_1.default.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 {}; });