|
| 1 | +import { type ComponentProps, type KeyboardEvent, type Ref, useRef } from 'react'; |
| 2 | + |
| 3 | +export type DatePickerSize = 'lg' | 'md' | 'sm'; |
| 4 | + |
| 5 | +export type DatePickerProps = Omit<ComponentProps<'div'>, 'children'> & { |
| 6 | + size?: DatePickerSize; |
| 7 | + isError?: boolean; |
| 8 | + isDisabled?: boolean; |
| 9 | + children: (props: { |
| 10 | + yearRef: Ref<HTMLInputElement>; |
| 11 | + monthRef: Ref<HTMLInputElement>; |
| 12 | + dateRef: Ref<HTMLInputElement>; |
| 13 | + }) => JSX.Element; |
| 14 | +}; |
| 15 | + |
| 16 | +export const DatePicker = (props: DatePickerProps) => { |
| 17 | + const { className, size = 'lg', isError, isDisabled, children, ...rest } = props; |
| 18 | + |
| 19 | + const yearRef = useRef<HTMLInputElement>(null); |
| 20 | + const monthRef = useRef<HTMLInputElement>(null); |
| 21 | + const dateRef = useRef<HTMLInputElement>(null); |
| 22 | + |
| 23 | + function handleKeyDown(event: KeyboardEvent<HTMLInputElement>) { |
| 24 | + if (event.key === 'ArrowRight') { |
| 25 | + moveRight(event); |
| 26 | + } else if (event.key === 'ArrowLeft') { |
| 27 | + moveLeft(event); |
| 28 | + } else if (event.key.match(/^[^0-9]$/)) { |
| 29 | + if (!event.ctrlKey && !event.metaKey) { |
| 30 | + event.preventDefault(); |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + function moveRight(event: KeyboardEvent<HTMLInputElement>) { |
| 36 | + const input = event.target as HTMLInputElement; |
| 37 | + if (input.selectionStart !== input.selectionEnd) { |
| 38 | + return; |
| 39 | + } |
| 40 | + if (input.selectionEnd === input.value.length) { |
| 41 | + event.preventDefault(); |
| 42 | + if (input === yearRef.current) { |
| 43 | + monthRef.current?.focus(); |
| 44 | + } else if (input === monthRef.current) { |
| 45 | + dateRef.current?.focus(); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + function moveLeft(event: KeyboardEvent<HTMLInputElement>) { |
| 51 | + const input = event.target as HTMLInputElement; |
| 52 | + if (input.selectionStart !== input.selectionEnd) { |
| 53 | + return; |
| 54 | + } |
| 55 | + if (input.selectionStart === 0) { |
| 56 | + event.preventDefault(); |
| 57 | + if (input === monthRef.current) { |
| 58 | + yearRef.current?.focus(); |
| 59 | + } else if (input === dateRef.current) { |
| 60 | + monthRef.current?.focus(); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return ( |
| 66 | + <div |
| 67 | + className={`inline-flex h-14 -space-x-1 rounded-8 border border-solid-gray-600 bg-[--bg] p-0.5 pe-0 text-solid-gray-900 [--bg:theme(colors.white)] focus-within:border-black hover:border-solid-gray-900 data-[size=md]:h-12 data-[size=sm]:h-10 data-[disabled]:border-solid-gray-300 data-[error]:border-error-1 data-[disabled]:text-solid-gray-420 data-[disabled]:[--bg:theme(colors.solid-gray.50)] data-[error]:focus-within:border-red-1000 data-[error]:hover:border-red-1000 forced-colors:data-[disabled]:border-[GrayText] forced-colors:data-[disabled]:text-[GrayText] ${className ?? ''}`} |
| 68 | + data-size={size} |
| 69 | + data-error={isError || null} |
| 70 | + data-disabled={isDisabled || null} |
| 71 | + onKeyDown={handleKeyDown} |
| 72 | + {...rest} |
| 73 | + > |
| 74 | + {children({ yearRef, monthRef, dateRef })} |
| 75 | + </div> |
| 76 | + ); |
| 77 | +}; |
0 commit comments