UNPKG

@mantine/dates

Version:

Calendars, date and time pickers based on Mantine components

1 lines 5.29 kB
{"version":3,"file":"TimeInput.mjs","names":["classes"],"sources":["../../../src/components/TimeInput/TimeInput.tsx"],"sourcesContent":["import cx from 'clsx';\nimport {\n __BaseInputProps,\n __InputStylesNames,\n BoxProps,\n ElementProps,\n factory,\n Factory,\n InputBase,\n StylesApiProps,\n useProps,\n useResolvedStylesApi,\n} from '@mantine/core';\nimport classes from './TimeInput.module.css';\n\nexport interface TimeInputProps\n extends\n BoxProps,\n __BaseInputProps,\n StylesApiProps<TimeInputFactory>,\n ElementProps<'input', 'size'> {\n /** Determines whether seconds input should be displayed @default false */\n withSeconds?: boolean;\n\n /** Minimum possible string time, if `withSeconds` is true, time should be in format HH:mm:ss, otherwise HH:mm */\n minTime?: string;\n\n /** Maximum possible string time, if `withSeconds` is true, time should be in format HH:mm:ss, otherwise HH:mm */\n maxTime?: string;\n}\n\nexport type TimeInputFactory = Factory<{\n props: TimeInputProps;\n ref: HTMLInputElement;\n stylesNames: __InputStylesNames;\n}>;\n\nexport const TimeInput = factory<TimeInputFactory>((_props) => {\n const props = useProps(['Input', 'InputWrapper', 'TimeInput'], null, _props);\n const {\n classNames,\n styles,\n unstyled,\n vars,\n withSeconds,\n minTime,\n maxTime,\n value,\n onChange,\n step,\n ...others\n } = props;\n\n const { resolvedClassNames, resolvedStyles } = useResolvedStylesApi<TimeInputFactory>({\n classNames,\n styles,\n props,\n });\n\n /**\n * Check if time is within limits or not\n * If the given value is within limits, return 0\n * If the given value is greater than the maxTime, return 1\n * If the given value is less than the minTime, return -1\n */\n const checkIfTimeLimitExceeded = (val: string) => {\n if (minTime !== undefined || maxTime !== undefined) {\n const [hours, minutes, seconds] = val.split(':').map(Number);\n\n if (minTime) {\n const [minHours, minMinutes, minSeconds] = minTime.split(':').map(Number);\n\n if (\n hours < minHours ||\n (hours === minHours && minutes < minMinutes) ||\n (withSeconds && hours === minHours && minutes === minMinutes && seconds < minSeconds)\n ) {\n return -1;\n }\n }\n\n if (maxTime) {\n const [maxHours, maxMinutes, maxSeconds] = maxTime.split(':').map(Number);\n\n if (\n hours > maxHours ||\n (hours === maxHours && minutes > maxMinutes) ||\n (withSeconds && hours === maxHours && minutes === maxMinutes && seconds > maxSeconds)\n ) {\n return 1;\n }\n }\n }\n\n return 0;\n };\n\n const onTimeBlur = (event: React.FocusEvent<HTMLInputElement>) => {\n props.onBlur?.(event);\n if (minTime !== undefined || maxTime !== undefined) {\n const val = event.currentTarget.value;\n\n if (val) {\n const check = checkIfTimeLimitExceeded(val);\n if (check === 1) {\n if (maxTime) {\n event.currentTarget.value = maxTime;\n }\n\n props.onChange?.(event);\n } else if (check === -1) {\n if (minTime) {\n event.currentTarget.value = minTime;\n }\n\n props.onChange?.(event);\n }\n }\n }\n };\n\n return (\n <InputBase\n classNames={{ ...resolvedClassNames, input: cx(classes.input, resolvedClassNames?.input) }}\n styles={resolvedStyles}\n unstyled={unstyled}\n value={value}\n step={step ?? (withSeconds ? 1 : 60)}\n {...others}\n onChange={onChange}\n onBlur={onTimeBlur}\n type=\"time\"\n __staticSelector=\"TimeInput\"\n />\n );\n});\n\nTimeInput.classes = InputBase.classes;\nTimeInput.displayName = '@mantine/dates/TimeInput';\n\nexport namespace TimeInput {\n export type Props = TimeInputProps;\n export type Factory = TimeInputFactory;\n}\n"],"mappings":";;;;;;AAqCA,MAAa,YAAY,SAA2B,WAAW;CAC7D,MAAM,QAAQ,SAAS;EAAC;EAAS;EAAgB;CAAW,GAAG,MAAM,MAAM;CAC3E,MAAM,EACJ,YACA,QACA,UACA,MACA,aACA,SACA,SACA,OACA,UACA,MACA,GAAG,WACD;CAEJ,MAAM,EAAE,oBAAoB,mBAAmB,qBAAuC;EACpF;EACA;EACA;CACF,CAAC;;;;;;;CAQD,MAAM,4BAA4B,QAAgB;EAChD,IAAI,YAAY,KAAA,KAAa,YAAY,KAAA,GAAW;GAClD,MAAM,CAAC,OAAO,SAAS,WAAW,IAAI,MAAM,GAA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