UNPKG

@mantine/dates

Version:

Calendars, date and time pickers based on Mantine components

1 lines 8.59 kB
{"version":3,"file":"PickerInputBase.cjs","names":["Input","Modal","Popover","classes","HiddenDatesInput"],"sources":["../../../src/components/PickerInputBase/PickerInputBase.tsx"],"sourcesContent":["import 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 { useDisclosure } 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\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 ...others\n } = useInputProps('PickerInputBase', { size: 'sm' }, _props);\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 && (\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}>\n <Popover\n position=\"bottom-start\"\n opened={dropdownOpened}\n trapFocus\n returnFocus={false}\n unstyled={unstyled}\n onClose={onDropdownClose}\n {...popoverProps}\n 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 >\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 data-dates-dropdown>{children}</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":";;;;;;;;;AAkGA,MAAa,mBAAA,GAAA,cAAA,QAAA,EAAmD,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,GAAG,YAAA,GAAA,cAAA,cAAA,CACa,mBAAmB,EAAE,MAAM,KAAK,GAAG,MAAM;CAE3D,MAAM,cACJ,iBAAA,GAAA,kBAAA,IAAA,CAACA,cAAAA,MAAM,aAAP;EAAmB,S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