UNPKG

@workday/canvas-kit-react

Version:

The parent module that contains all Workday Canvas Kit React components

28 lines (27 loc) 1.26 kB
import React from 'react'; import { createElemPropsHook, useLocalRef, dispatchInputEvent, } from '@workday/canvas-kit-react/common'; import { useComboboxModel } from './useComboboxModel'; /** * An arbitrary combobox can have any value. The list of options are suggestions to aid the user in * entering values. A Typeahead or Autocomplete are examples are arbitrary value comboboxes. */ export const useComboboxInputArbitrary = createElemPropsHook(useComboboxModel)((model, ref) => { const { elementRef, localRef } = useLocalRef(ref); // sync model selection state with inputs React.useLayoutEffect(() => { if (localRef.current) { const formValue = (model.state.selectedIds === 'all' ? [] : model.state.selectedIds).join(', '); if (formValue !== localRef.current.value) { dispatchInputEvent(localRef.current, formValue); } } }, [model.state.selectedIds, localRef]); return { ref: elementRef, onChange: (event) => { var _a, _b; (_a = model.onChange) === null || _a === void 0 ? void 0 : _a.call(model, event); (_b = model.onFilterChange) === null || _b === void 0 ? void 0 : _b.call(model, event); }, }; });