{"version":3,"file":"PickerInputBase.cjs","names":["factory","useInputProps","useId","useRef","Input","Modal","Popover","cx","classes","useMergedRef","HiddenDatesInput"],"sources":["../../../src/components/PickerInputBase/PickerInputBase.tsx"],"sourcesContent":["import { useEffect, useRef } from 'react';\nimport cx from 'clsx';\nimport {\n  __BaseInputProps,\n  __InputStylesNames,\n  BoxProps,\n  ClearSectionMode,\n  ElementProps,\n  factory,\n  Factory,\n  Input,\n  InputVariant,\n  MantineSize,\n  Modal,\n  ModalProps,\n  Popover,\n  PopoverProps,\n  StylesApiProps,\n  useInputProps,\n} from '@mantine/core';\nimport { useDidUpdate, useDisclosure, useId, useMergedRef } from '@mantine/hooks';\nimport { DatePickerType } from '../../types';\nimport { DateFormatter } from '../../utils';\nimport { HiddenDatesInput, HiddenDatesInputValue } from '../HiddenDatesInput';\nimport classes from './PickerInputBase.module.css';\n\nexport type PickerInputBaseStylesNames = __InputStylesNames;\n\nexport interface DateInputSharedProps\n  extends\n    Omit<__BaseInputProps, 'size'>,\n    ElementProps<'button', 'defaultValue' | 'value' | 'onChange' | 'type'> {\n  /** Determines whether the dropdown is closed when date is selected, not applicable with `type=\"multiple\"` @default true */\n  closeOnChange?: boolean;\n\n  /** Type of the dropdown @default 'popover' */\n  dropdownType?: 'popover' | 'modal';\n\n  /** Props passed down to `Popover` component */\n  popoverProps?: Partial<Omit<PopoverProps, 'children'>>;\n\n  /** Props passed down to `Modal` component */\n  modalProps?: Partial<Omit<ModalProps, 'children'>>;\n\n  /** If set, clear button is displayed in the `rightSection` when the component has value. Ignored if `rightSection` prop is set. @default false */\n  clearable?: boolean;\n\n  /** Determines how the clear button and rightSection are rendered @default 'both' */\n  clearSectionMode?: ClearSectionMode;\n\n  /** Props passed down to the clear button */\n  clearButtonProps?: React.ComponentProps<'button'>;\n\n  /** If set, the component value cannot be changed by the user */\n  readOnly?: boolean;\n\n  /** Determines whether dates values should be sorted before `onChange` call, only applicable with type=\"multiple\" @default true */\n  sortDates?: boolean;\n\n  /** Separator between range value */\n  labelSeparator?: string;\n\n  /** Input placeholder */\n  placeholder?: string;\n\n  /** A function to format selected dates values into a string. By default, date is formatted based on the input type. */\n  valueFormatter?: DateFormatter;\n\n  /** Called when the dropdown is closed */\n  onDropdownClose?: () => void;\n}\n\nexport interface PickerInputBaseProps\n  extends\n    BoxProps,\n    DateInputSharedProps,\n    Omit<StylesApiProps<PickerInputBaseFactory>, 'classNames' | 'styles'> {\n  classNames?: Partial<Record<string, string>>;\n  styles?: Partial<Record<string, React.CSSProperties>>;\n  __staticSelector?: string;\n  children: React.ReactNode;\n  formattedValue: string | null | undefined;\n  dropdownHandlers: ReturnType<typeof useDisclosure>[1];\n  dropdownOpened: boolean;\n  onClear: () => void;\n  shouldClear: boolean;\n  value: HiddenDatesInputValue;\n  type: DatePickerType;\n  size?: MantineSize;\n  withTime?: boolean;\n\n  /** Set when the dropdown content includes presets, reduces dropdown start padding */\n  withPresets?: boolean;\n}\n\nexport type PickerInputBaseFactory = Factory<{\n  props: PickerInputBaseProps;\n  ref: HTMLButtonElement;\n  stylesNames: PickerInputBaseStylesNames;\n  variant: InputVariant;\n}>;\n\nexport const PickerInputBase = factory<PickerInputBaseFactory>((_props) => {\n  const {\n    inputProps,\n    wrapperProps,\n    placeholder,\n    classNames,\n    styles,\n    unstyled,\n    popoverProps,\n    modalProps,\n    dropdownType,\n    children,\n    formattedValue,\n    dropdownHandlers,\n    dropdownOpened,\n    onClick,\n    clearable,\n    clearSectionMode,\n    onClear,\n    clearButtonProps,\n    rightSection,\n    shouldClear,\n    readOnly,\n    disabled,\n    value,\n    name,\n    form,\n    type,\n    onDropdownClose,\n    withTime,\n    withPresets,\n    ref,\n    ...others\n  } = useInputProps('PickerInputBase', { size: 'sm' }, _props);\n\n  const _id = useId(inputProps.id);\n\n  const inputRef = useRef<HTMLButtonElement>(null);\n  const dropdownRef = useRef<HTMLDivElement>(null);\n  const keyboardInteractionRef = useRef(false);\n\n  const { close } = dropdownHandlers;\n\n  useEffect(() => {\n    if (disabled) {\n      close();\n    }\n  }, [disabled, close]);\n\n  useDidUpdate(() => {\n    if (dropdownOpened) {\n      keyboardInteractionRef.current = false;\n      return;\n    }\n\n    if (!keyboardInteractionRef.current) {\n      return;\n    }\n\n    const activeElement = document.activeElement;\n    const focusLost =\n      !activeElement ||\n      activeElement === document.body ||\n      !!dropdownRef.current?.contains(activeElement);\n\n    if (focusLost) {\n      inputRef.current?.focus({ preventScroll: true });\n    }\n  }, [dropdownOpened]);\n\n  const clearButton = (\n    <Input.ClearButton onClick={onClear} unstyled={unstyled} {...clearButtonProps} />\n  );\n\n  const handleClose = () => {\n    const isInvalidRangeValue = type === 'range' && Array.isArray(value) && value[0] && !value[1];\n    if (isInvalidRangeValue) {\n      onClear();\n    }\n\n    dropdownHandlers.close();\n  };\n\n  return (\n    <>\n      {dropdownType === 'modal' && !readOnly && !disabled && (\n        <Modal\n          opened={dropdownOpened}\n          onClose={handleClose}\n          withCloseButton={false}\n          size=\"auto\"\n          data-dates-modal\n          unstyled={unstyled}\n          {...modalProps}\n        >\n          {children}\n        </Modal>\n      )}\n\n      <Input.Wrapper {...wrapperProps} id={_id}>\n        <Popover\n          id={_id}\n          position=\"bottom-start\"\n          opened={dropdownOpened}\n          trapFocus\n          returnFocus={false}\n          unstyled={unstyled}\n          onClose={onDropdownClose}\n          {...popoverProps}\n          disabled={disabled || popoverProps?.disabled || dropdownType === 'modal' || readOnly}\n          onChange={(_opened) => {\n            if (!_opened) {\n              popoverProps?.onClose?.();\n              handleClose();\n            }\n          }}\n        >\n          <Popover.Target>\n            <Input\n              data-dates-input\n              data-read-only={readOnly || undefined}\n              disabled={disabled}\n              component=\"button\"\n              type=\"button\"\n              multiline\n              onClick={(event) => {\n                onClick?.(event);\n                dropdownHandlers.toggle();\n              }}\n              __clearSection={clearButton}\n              __clearable={clearable && shouldClear && !readOnly && !disabled}\n              __clearSectionMode={clearSectionMode}\n              rightSection={rightSection}\n              {...inputProps}\n              classNames={{ ...classNames, input: cx(classes.input, (classNames as any)?.input) }}\n              {...others}\n              ref={useMergedRef(inputRef, ref)}\n            >\n              {formattedValue || (\n                <Input.Placeholder\n                  error={inputProps.error}\n                  unstyled={unstyled}\n                  classNames={classNames}\n                  styles={styles}\n                  __staticSelector={inputProps.__staticSelector}\n                >\n                  {placeholder}\n                </Input.Placeholder>\n              )}\n            </Input>\n          </Popover.Target>\n\n          <Popover.Dropdown\n            data-dates-dropdown\n            data-with-presets={withPresets || undefined}\n            className={unstyled ? undefined : classes.dropdown}\n            ref={dropdownRef}\n            onKeyDown={() => {\n              keyboardInteractionRef.current = true;\n            }}\n            onPointerDown={() => {\n              keyboardInteractionRef.current = false;\n            }}\n          >\n            {children}\n          </Popover.Dropdown>\n        </Popover>\n      </Input.Wrapper>\n      <HiddenDatesInput value={value} name={name} form={form} type={type} withTime={withTime} />\n    </>\n  );\n});\n\nPickerInputBase.classes = classes;\nPickerInputBase.displayName = '@mantine/dates/PickerInputBase';\n"],"mappings":";;;;;;;;;;;AAsGA,MAAa,mBAAA,GAAkBA,cAAAA,QAAAA,EAAiC,WAAW;CACzE,MAAM,EACJ,YACA,cACA,aACA,YACA,QACA,UACA,cACA,YACA,cACA,UACA,gBACA,kBACA,gBACA,SACA,WACA,kBACA,SACA,kBACA,cACA,aACA,UACA,UACA,OACA,MACA,MACA,MACA,iBACA,UACA,aACA,KACA,GAAG,YAAA,GACDC,cAAAA,cAAAA,CAAc,mBAAmB,EAAE,MAAM,KAAK,GAAG,MAAM;CAE3D,MAAM,OAAA,GAAMC,eAAAA,MAAAA,CAAM,WAAW,EAAE;CAE/B,MAAM,YAAA,GAAWC,MAAAA,OAAAA,CAA0B,IAAI;CAC/C,MAAM,eAAA,GAAcA,MAAAA,OAAAA,CAAuB,IAAI;CAC/C,MAAM,0BAAA,GAAyBA,MAAAA,OAAAA,CAAO,KAAK;CAE3C,MAAM,EAAE,UAAU;CAElB,CAAA,GAAA,MAAA,UAAA,OAAgB;EACd,IAAI,UACF,MAAM;CAE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