import React from 'react'; import {getCursor} from '@workday/canvas-kit-react/collection'; import {useComboboxInput, useComboboxInputConstrained} from '@workday/canvas-kit-react/combobox'; import { ErrorType, composeHooks, cornerShapeStencil, createElemPropsHook, createSubcomponent, } from '@workday/canvas-kit-react/common'; import {SystemIcon, systemIconStencil} from '@workday/canvas-kit-react/icon'; import {InputGroup, TextInput, textInputStencil} from '@workday/canvas-kit-react/text-input'; import {CSProps, createStencil, handleCsProp, px2rem} from '@workday/canvas-kit-styling'; import { chevronDownSmallIcon, chevronUpSmallIcon, searchIcon, } from '@workday/canvas-system-icons-web'; import {system} from '@workday/canvas-tokens-web'; import {MultiSelectedItemProps} from './MultiSelectedItem'; import {MultiSelectedList} from './MultiSelectedList'; import {useMultiSelectModel} from './useMultiSelectModel'; export const multiSelectInputStencil = createStencil({ extends: cornerShapeStencil, base: { border: `${px2rem(1)} solid ${system.color.border.input.default}`, display: 'flex', flexDirection: 'column', backgroundColor: system.legacy.color.surface.default, [cornerShapeStencil.vars.shape]: system.legacy.shape.lg, boxSizing: 'border-box', minHeight: system.legacy.size.md, transition: '0.2s box-shadow, 0.2s border-color', margin: 0, // Fix Safari [textInputStencil.vars.width]: '100%', [systemIconStencil.vars.color]: system.color.fg.default, '&:hover, &.hover': { borderColor: system.legacy.color.border.input.hover, }, '&:has(:focus-visible:not([disabled])), &.focus': { borderColor: system.legacy.color.brand.border.primary, boxShadow: `inset 0 0 0 1px ${system.legacy.color.brand.focus.primary}`, }, '& [data-part="user-input"]': { fontFamily: system.fontFamily.default, fontSize: system.legacy.fontSize.subtext.lg, fontWeight: system.fontWeight.normal, lineHeight: system.legacy.lineHeight.subtext.lg, backgroundColor: system.legacy.color.surface.transparent, borderRadius: system.legacy.shape.md, // collapse the height of the input by the border width so that an empty multi-select // is the same height as a `TextInput` '&:where([data-part="user-input"], [data-part="form-input"])': { height: px2rem(38), }, // Remove the focus ring - it is handled at the container level border: 'none !important', boxShadow: 'none !important', outlineWidth: '0px', '&:where(:not([aria-autocomplete]))': { caretColor: 'transparent', cursor: 'default', '&::selection': { backgroundColor: 'transparent', }, }, }, '&:has(:disabled, .disabled) :where([data-part="user-input"])': { opacity: system.opacity.disabled, [systemIconStencil.vars.color]: system.color.fg.disabled, }, '&:has(:disabled, .disabled)': { opacity: system.opacity.disabled, [systemIconStencil.vars.color]: system.color.fg.disabled, }, '&:has(:disabled, .disabled) :where([data-part="form-input"])': { opacity: 0, [systemIconStencil.vars.color]: system.color.fg.disabled, }, '& :where([data-part="form-input"])': { position: 'absolute', top: 0, bottom: 0, left: 0, right: 0, opacity: 0, cursor: 'default', pointerEvents: 'none', }, '& :where([data-part="separator"])': { backgroundColor: system.legacy.color.border.default, height: 1, margin: `0 ${system.legacy.gap.sm}`, }, '& :where([data-part="list"])': { display: 'flex', gap: system.legacy.gap.sm, padding: system.legacy.padding.xs, flexWrap: 'wrap', }, }, modifiers: { error: { error: { borderColor: system.legacy.color.brand.border.critical, boxShadow: `inset 0 0 0 ${px2rem(2)} ${system.legacy.color.brand.focus.critical}`, backgroundColor: system.legacy.color.brand.surface.critical.default, '&:has(:hover, :disabled, :focus-visible), &:is(.hover, .disabled, .focus)': { borderColor: system.legacy.color.brand.border.critical, }, '&:has(:focus-visible:not([disabled])), &.focus': { borderColor: system.legacy.color.brand.border.critical, boxShadow: `inset 0 0 0 ${px2rem(2)} ${system.legacy.color.brand.focus.critical}, 0 0 0 2px ${ system.legacy.color.focus.inverse }, 0 0 0 4px ${system.legacy.color.brand.border.primary}`, outlineOffset: px2rem(2), }, }, caution: { borderColor: system.legacy.color.brand.border.caution, boxShadow: `inset 0 0 0 ${px2rem(2)} ${system.legacy.color.brand.focus.caution.inner}`, backgroundColor: system.legacy.color.brand.surface.caution.default, '&:has(:hover, .hover, :disabled, .disabled, :focus-visible:not([disabled])), .focus:not(:has([disabled]))': { borderColor: system.legacy.color.brand.border.caution, }, '&:hover, &.hover': { borderColor: system.legacy.color.brand.border.caution, }, '&:has(:focus-visible, .focus):not(:has([disabled]))': { boxShadow: `inset 0 0 0 ${px2rem(2)} ${system.legacy.color.brand.focus.caution.inner}, 0 0 0 2px ${system.legacy.color.focus.inverse}, 0 0 0 4px ${system.legacy.color.brand.border.primary}`, }, outlineOffset: px2rem(2), }, }, }, }); /** @deprecated use `multiSelectInputStencil` instead. This will be removed in a future version. */ export const multiSelectStencil = multiSelectInputStencil; export const useMultiSelectInput = composeHooks( createElemPropsHook(useMultiSelectModel)((model, ref) => { return { onKeyDown(event: React.KeyboardEvent) { // Space bar key is hit and menu is open if (event.key === ' ' && model.state.visibility === 'visible') { const id = getCursor(model.state); // There are 2 modes. Search mode and navigation mode. Searching clears the cursor. Using // the down arrow enables the cursor and thus navigation mode. // - Navigation mode: toggle cursor item // - Search mode: enter a space in the search if (id) { model.events.select({id}); event.preventDefault(); } } if ( (event.key === 'ArrowDown' || event.key === 'ArrowUp') && model.state.visibility === 'hidden' ) { // prevent navigating vertically on the input model.events.show(event); event.preventDefault(); } }, }; }), useComboboxInputConstrained, useComboboxInput ); const useMultiSelectSearchInput = composeHooks( createElemPropsHook(useMultiSelectModel)(model => ({ onKeyDown(event: React.KeyboardEvent) { if ( model.state.visibility === 'hidden' && event.key.length === 1 && !event.altKey && !event.ctrlKey && !event.metaKey ) { model.events.show(event); } }, })), useMultiSelectInput ); export interface MultiSelectInputProps extends CSProps, Pick< React.InputHTMLAttributes, 'disabled' | 'className' | 'style' | 'aria-label' | 'aria-labelledby' >, Pick { error?: ErrorType; } export const MultiSelectInput = createSubcomponent(TextInput)({ modelHook: useMultiSelectModel, elemPropsHook: useMultiSelectInput, })( ( { className, cs, style, error, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy, removeLabel, disabled, formInputProps, ...elemProps }, Element, model ) => { return (
); } ); export const MultiSelectSearchInput = createSubcomponent(TextInput)({ modelHook: useMultiSelectModel, elemPropsHook: useMultiSelectSearchInput, })( ( { className, cs, style, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy, removeLabel, formInputProps, ref, disabled, error, ...elemProps }, Element, model ) => { return (
); } );