{"version":3,"file":"Select.mjs","names":[],"sources":["../../../src/select/Select.tsx"],"sourcesContent":["/*!\n *\n *  Copyright (c) \"Neo4j\"\n *  Neo4j Sweden AB [http://neo4j.com]\n *\n *  This file is part of Neo4j.\n *\n *  Neo4j is free software: you can redistribute it and/or modify\n *  it under the terms of the GNU General Public License as published by\n *  the Free Software Foundation, either version 3 of the License, or\n *  (at your option) any later version.\n *\n *  This program is distributed in the hope that it will be useful,\n *  but WITHOUT ANY WARRANTY; without even the implied warranty of\n *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n *  GNU General Public License for more details.\n *\n *  You should have received a copy of the GNU General Public License\n *  along with this program.  If not, see <http://www.gnu.org/licenses/>.\n */\nimport { tokens } from '@neo4j-ndl/base';\nimport classNames from 'classnames';\nimport type React from 'react';\nimport { useEffect, useId, useMemo, useState } from 'react';\nimport ReactSelect, { type GroupBase } from 'react-select';\nimport AsyncSelect from 'react-select/async';\n/** Different types of select components */\nimport Creatable from 'react-select/creatable';\n\nimport {\n  FormFieldLabel,\n  FormFieldMessage,\n  useFormField,\n} from '../_common/form-control';\nimport { type PolymorphicCommonProps } from '../_common/types';\nimport { needleWarningMessage } from '../_common/utils';\nimport { useIsInsideDialog } from '../dialog/dialog-context';\nimport {\n  CustomClearIndication,\n  CustomControl,\n  CustomIndicatorsContainer,\n  CustomInput,\n  CustomMenu,\n  CustomMenuList,\n  CustomMenuPortal,\n  CustomMultiValue,\n  CustomOption,\n  CustomPlaceholder,\n  CustomSingleValue,\n  CustomValueContainer,\n  DropdownIndicatorCurrying,\n  SelectFieldContext,\n} from './Overrides';\nimport { type SelectOverrideCustomProps, type SelectProps } from './types';\n\n/**\n *\n *\n * Helpers\n *\n *\n */\n\nconst overrideComponents = <\n  OptionType,\n  IsMulti extends boolean = false,\n  GroupType extends GroupBase<OptionType> = GroupBase<OptionType>,\n>(\n  props: SelectOverrideCustomProps<OptionType, IsMulti, GroupType>,\n) =>\n  ({\n    ClearIndicator: CustomClearIndication(props),\n    Control: CustomControl(props),\n    DropdownIndicator: DropdownIndicatorCurrying(props),\n    IndicatorSeparator: null,\n    IndicatorsContainer: CustomIndicatorsContainer(props),\n    Input: CustomInput(props),\n    Menu: CustomMenu(props),\n    MenuList: CustomMenuList(props),\n    MenuPortal: CustomMenuPortal(props),\n    MultiValue: CustomMultiValue(props),\n    Option: CustomOption(props),\n    Placeholder: CustomPlaceholder(props),\n    SingleValue: CustomSingleValue(props),\n    ValueContainer: CustomValueContainer(props),\n  }) as SelectProps<\n    OptionType,\n    IsMulti,\n    GroupType\n  >['selectProps']['components'];\n\nconst customStyles = <\n  OptionType,\n  IsMulti extends boolean = false,\n  GroupType extends GroupBase<OptionType> = GroupBase<OptionType>,\n>() => {\n  return {\n    clearIndicator: () => ({}),\n    control: () => ({}),\n    dropdownIndicator: () => ({}),\n    indicatorsContainer: () => ({}),\n    input: () => ({}),\n    menu: (provided) => ({\n      bottom: provided.bottom,\n      top: provided.top,\n    }),\n    menuList: (provided) => ({\n      maxHeight: provided.maxHeight,\n      minHeight: provided.minHeight,\n    }),\n    menuPortal: (provided) => ({\n      ...provided,\n      zIndex: tokens.zIndex.popover,\n    }),\n    option: () => ({}),\n    placeholder: () => ({}),\n    singleValue: () => ({}),\n    valueContainer: (provided) => ({\n      ...provided,\n      padding: '0',\n    }),\n  } as SelectProps<OptionType, IsMulti, GroupType>['selectProps']['styles'];\n};\n\n/**\n * @deprecated Use `Combobox` instead. This component will be removed in a future release.\n */\nexport const Select = <\n  OptionType,\n  IsMulti extends boolean = false,\n  GroupType extends GroupBase<OptionType> = GroupBase<OptionType>,\n  T extends React.ElementType = 'div',\n>({\n  as,\n  label,\n  helpText,\n  errorText,\n  isFluid = true,\n  size = 'medium',\n  type = 'select',\n  ariaLabel,\n  selectProps = {},\n  isClean = false,\n  isDisabled,\n  isRequired = false,\n  hasRequiredIndicator = false,\n  htmlAttributes,\n  className,\n  style,\n  ref,\n  ...restProps\n}: PolymorphicCommonProps<T, SelectProps<OptionType, IsMulti, GroupType>>) => {\n  const isInsideDialog = useIsInsideDialog();\n  const menuPosition = isInsideDialog ? 'fixed' : 'absolute';\n  const identifier = useId();\n  // `selectProps.required` predates `isRequired`, so either one keeps the\n  // indicator and the native validation in sync.\n  const isFieldRequired = isRequired || Boolean(selectProps.required);\n  // The shared form field resolves the ids and which message is displayed -\n  // an error always replaces the help text.\n  const { field } = useFormField({\n    controlId: identifier,\n    errorText,\n    hasRequiredIndicator,\n    helpText,\n    isRequired: isFieldRequired,\n    label,\n  });\n  const fieldContextValue = useMemo(\n    () => ({ hasError: field.hasError, messageId: field.messageId }),\n    [field.hasError, field.messageId],\n  );\n  const [isMenuOpen, setIsMenuOpen] = useState(false);\n\n  const handleMenuOpen = () => {\n    setIsMenuOpen(true);\n    if (selectProps.onMenuOpen) {\n      selectProps.onMenuOpen();\n    }\n  };\n\n  const handleMenuClose = () => {\n    setIsMenuOpen(false);\n    if (selectProps.onMenuClose) {\n      selectProps.onMenuClose();\n    }\n  };\n\n  // Handle Escape and Enter key to prevent it from bubbling up to dialog\n  const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {\n    if (\n      (event.key === 'Escape' && isMenuOpen) ||\n      (event.key === 'Enter' && isMenuOpen)\n    ) {\n      event.stopPropagation();\n    }\n\n    if (selectProps.onKeyDown) {\n      selectProps.onKeyDown(event);\n    }\n  };\n\n  const Component: React.ElementType = as ?? 'div';\n\n  const ComponentOverrides = useMemo(\n    () =>\n      overrideComponents<OptionType, IsMulti, GroupType>({\n        isClean,\n        size,\n      }),\n    [size, isClean],\n  );\n\n  const StyleOverrides = useMemo(\n    () => customStyles<OptionType, IsMulti, GroupType>(),\n    [],\n  );\n\n  useEffect(() => {\n    if (label === undefined && ariaLabel === undefined) {\n      needleWarningMessage(\n        'A Select needs to have a label or an aria label to be accessible. link: https://dequeuniversity.com/rules/axe/4.2/label?application=axeAPI',\n      );\n    }\n  }, [label, ariaLabel]);\n\n  let SelectElement: React.JSX.Element;\n  switch (type) {\n    case 'async': {\n      SelectElement = (\n        <AsyncSelect\n          name={identifier}\n          inputId={identifier}\n          isDisabled={Boolean(isDisabled) || selectProps.isDisabled}\n          menuPlacement=\"auto\"\n          aria-label={ariaLabel}\n          aria-invalid={Boolean(errorText)}\n          {...selectProps}\n          required={isFieldRequired}\n          onKeyDown={handleKeyDown}\n          onMenuOpen={handleMenuOpen}\n          onMenuClose={handleMenuClose}\n          menuPosition={selectProps.menuPosition ?? menuPosition}\n          hideSelectedOptions={false}\n          closeMenuOnSelect={Boolean(selectProps.isMulti) === false}\n          styles={{ ...StyleOverrides, ...selectProps.styles }}\n          components={{ ...ComponentOverrides, ...selectProps.components }}\n        />\n      );\n      break;\n    }\n    case 'creatable':\n      SelectElement = (\n        <Creatable\n          name={identifier}\n          inputId={identifier}\n          isDisabled={Boolean(isDisabled) || selectProps.isDisabled}\n          menuPlacement=\"auto\"\n          {...selectProps}\n          aria-label={ariaLabel}\n          aria-invalid={Boolean(errorText)}\n          required={isFieldRequired}\n          onKeyDown={handleKeyDown}\n          onMenuOpen={handleMenuOpen}\n          onMenuClose={handleMenuClose}\n          menuPosition={selectProps.menuPosition ?? menuPosition}\n          hideSelectedOptions={false}\n          closeMenuOnSelect={Boolean(selectProps.isMulti) === false}\n          styles={{ ...StyleOverrides, ...selectProps.styles }}\n          components={{ ...ComponentOverrides, ...selectProps.components }}\n        />\n      );\n      break;\n    default:\n      SelectElement = (\n        <ReactSelect\n          name={identifier}\n          inputId={identifier}\n          isDisabled={Boolean(isDisabled) || selectProps.isDisabled}\n          menuPlacement=\"auto\"\n          {...selectProps}\n          aria-label={ariaLabel}\n          aria-invalid={Boolean(errorText)}\n          required={isFieldRequired}\n          onKeyDown={handleKeyDown}\n          onMenuOpen={handleMenuOpen}\n          onMenuClose={handleMenuClose}\n          menuPosition={selectProps.menuPosition ?? menuPosition}\n          hideSelectedOptions={false}\n          closeMenuOnSelect={Boolean(selectProps.isMulti) === false}\n          styles={{ ...StyleOverrides, ...selectProps.styles }}\n          components={{ ...ComponentOverrides, ...selectProps.components }}\n        />\n      );\n  }\n\n  const classes = classNames('ndl-select', className, {\n    'ndl-creatable': type === 'creatable',\n    'ndl-disabled': selectProps.isDisabled,\n    'ndl-fluid': isFluid,\n    'ndl-has-error': Boolean(errorText),\n    'ndl-large': size === 'large',\n    'ndl-medium': size === 'medium',\n    'ndl-multi': selectProps.isMulti,\n    'ndl-small': size === 'small',\n  });\n\n  return (\n    <Component\n      ref={ref}\n      className={classes}\n      style={style}\n      {...restProps}\n      {...htmlAttributes}\n    >\n      {Boolean(label) && <FormFieldLabel field={field} size={size} />}\n      <SelectFieldContext.Provider value={fieldContextValue}>\n        {SelectElement}\n      </SelectFieldContext.Provider>\n      <FormFieldMessage field={field} size={size} />\n    </Component>\n  );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAqIE;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;;;;;;;;;AAtFF,MAAM,sBAKJ,WAEC;CACC,gBAAgB,sBAAsB,KAAK;CAC3C,SAAS,cAAc,KAAK;CAC5B,mBAAmB,0BAA0B,KAAK;CAClD,oBAAoB;CACpB,qBAAqB,0BAA0B,KAAK;CACpD,OAAO,YAAY,KAAK;CACxB,MAAM,WAAW,KAAK;CACtB,UAAU,eAAe,KAAK;CAC9B,YAAY,iBAAiB,KAAK;CAClC,YAAY,iBAAiB,KAAK;CAClC,QAAQ,aAAa,KAAK;CAC1B,aAAa,kBAAkB,KAAK;CACpC,aAAa,kBAAkB,KAAK;CACpC,gBAAgB,qBAAqB,KAAK;AAC5C;AAMF,MAAM,qBAIC;CACL,OAAO;EACL,uBAAuB,CAAC;EACxB,gBAAgB,CAAC;EACjB,0BAA0B,CAAC;EAC3B,4BAA4B,CAAC;EAC7B,cAAc,CAAC;EACf,OAAO,cAAc;GACnB,QAAQ,SAAS;GACjB,KAAK,SAAS;EA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