@betterinternship/components 3.4.2 → 3.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/autocomplete.d.ts +1 -2
- package/dist/components/autocomplete.js +75 -64
- package/dist/components/autocomplete.js.map +1 -1
- package/dist/components/checkbox-field.d.ts +18 -0
- package/dist/components/checkbox-field.js +25 -0
- package/dist/components/checkbox-field.js.map +1 -0
- package/dist/components/checkbox-group.d.ts +33 -0
- package/dist/components/checkbox-group.js +43 -0
- package/dist/components/checkbox-group.js.map +1 -0
- package/dist/components/checkbox.d.ts +1 -1
- package/dist/components/checkbox.js +6 -2
- package/dist/components/checkbox.js.map +1 -1
- package/dist/components/chip-select.d.ts +11 -2
- package/dist/components/chip-select.js +9 -4
- package/dist/components/chip-select.js.map +1 -1
- package/dist/components/{form-date-picker.d.ts → date-picker.d.ts} +11 -8
- package/dist/components/date-picker.js +57 -0
- package/dist/components/date-picker.js.map +1 -0
- package/dist/components/dropdown.js +7 -2
- package/dist/components/dropdown.js.map +1 -1
- package/dist/components/form-field.d.ts +60 -5
- package/dist/components/form-field.js +55 -5
- package/dist/components/form-field.js.map +1 -1
- package/dist/components/input.js +3 -1
- package/dist/components/input.js.map +1 -1
- package/dist/components/label.js +1 -1
- package/dist/components/label.js.map +1 -1
- package/dist/components/{form-month-picker.d.ts → month-picker.d.ts} +6 -6
- package/dist/components/month-picker.js +57 -0
- package/dist/components/month-picker.js.map +1 -0
- package/dist/components/radio-group.d.ts +11 -1
- package/dist/components/radio-group.js +17 -4
- package/dist/components/radio-group.js.map +1 -1
- package/dist/components/select.js +38 -2
- package/dist/components/select.js.map +1 -1
- package/dist/components/sheet.d.ts +1 -1
- package/dist/components/switch.d.ts +1 -1
- package/dist/components/switch.js +4 -1
- package/dist/components/switch.js.map +1 -1
- package/dist/components/table.js +5 -5
- package/dist/components/table.js.map +1 -1
- package/dist/components/textarea.d.ts +1 -1
- package/dist/components/textarea.js +3 -1
- package/dist/components/textarea.js.map +1 -1
- package/dist/components/time-input.d.ts +9 -0
- package/dist/components/time-input.js +65 -0
- package/dist/components/time-input.js.map +1 -0
- package/dist/design-tokens.d.ts +1 -1
- package/dist/hooks/use-field-focus.d.ts +1 -1
- package/dist/hooks/use-field-focus.js +1 -1
- package/dist/hooks/use-field-focus.js.map +1 -1
- package/dist/index.d.ts +5 -9
- package/dist/index.js +7 -11
- package/dist/index.js.map +1 -1
- package/dist/node_modules/tailwind-merge/dist/bundle-mjs.js +54 -2
- package/dist/node_modules/tailwind-merge/dist/bundle-mjs.js.map +1 -1
- package/dist/utils.js +11 -1
- package/dist/utils.js.map +1 -1
- package/package.json +28 -40
- package/theme.css +3 -1
- package/dist/components/form-checkbox-group.d.ts +0 -31
- package/dist/components/form-checkbox-group.js +0 -30
- package/dist/components/form-checkbox-group.js.map +0 -1
- package/dist/components/form-checkbox.d.ts +0 -15
- package/dist/components/form-checkbox.js +0 -15
- package/dist/components/form-checkbox.js.map +0 -1
- package/dist/components/form-date-picker.js +0 -56
- package/dist/components/form-date-picker.js.map +0 -1
- package/dist/components/form-input.d.ts +0 -11
- package/dist/components/form-input.js +0 -13
- package/dist/components/form-input.js.map +0 -1
- package/dist/components/form-month-picker.js +0 -56
- package/dist/components/form-month-picker.js.map +0 -1
- package/dist/components/form-radio.d.ts +0 -15
- package/dist/components/form-radio.js +0 -20
- package/dist/components/form-radio.js.map +0 -1
- package/dist/components/form-textarea.d.ts +0 -11
- package/dist/components/form-textarea.js +0 -12
- package/dist/components/form-textarea.js.map +0 -1
- package/dist/components/form.d.ts +0 -24
- package/dist/components/form.js +0 -71
- package/dist/components/form.js.map +0 -1
- package/dist/components/time-input-native.d.ts +0 -14
- package/dist/components/time-input-native.js +0 -62
- package/dist/components/time-input-native.js.map +0 -1
- package/dist/node_modules/react-hook-form/dist/index.esm.js +0 -706
- package/dist/node_modules/react-hook-form/dist/index.esm.js.map +0 -1
package/dist/components/form.js
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
import { jsx } from 'react/jsx-runtime';
|
|
3
|
-
import * as React from 'react';
|
|
4
|
-
import { Slot } from '@radix-ui/react-slot';
|
|
5
|
-
import { useFormContext, FormProvider, Controller } from '../node_modules/react-hook-form/dist/index.esm.js';
|
|
6
|
-
import { cn } from '../utils.js';
|
|
7
|
-
import { Label } from './label.js';
|
|
8
|
-
|
|
9
|
-
const Form = FormProvider;
|
|
10
|
-
const FormFieldContext = React.createContext({});
|
|
11
|
-
const FormField = ({ ...props }) => {
|
|
12
|
-
return (jsx(FormFieldContext.Provider, { value: { name: props.name }, children: jsx(Controller, { ...props }) }));
|
|
13
|
-
};
|
|
14
|
-
const useFormField = () => {
|
|
15
|
-
const fieldContext = React.useContext(FormFieldContext);
|
|
16
|
-
const itemContext = React.useContext(FormItemContext);
|
|
17
|
-
const { getFieldState, formState } = useFormContext();
|
|
18
|
-
const fieldState = getFieldState(fieldContext.name, formState);
|
|
19
|
-
if (!fieldContext) {
|
|
20
|
-
throw new Error("useFormField should be used within <FormField>");
|
|
21
|
-
}
|
|
22
|
-
const { id } = itemContext;
|
|
23
|
-
return {
|
|
24
|
-
id,
|
|
25
|
-
name: fieldContext.name,
|
|
26
|
-
formItemId: `${id}-form-item`,
|
|
27
|
-
formDescriptionId: `${id}-form-item-description`,
|
|
28
|
-
formMessageId: `${id}-form-item-message`,
|
|
29
|
-
...fieldState,
|
|
30
|
-
};
|
|
31
|
-
};
|
|
32
|
-
const FormItemContext = React.createContext({});
|
|
33
|
-
const FormItem = React.forwardRef(({ className, ...props }, ref) => {
|
|
34
|
-
const id = React.useId();
|
|
35
|
-
return (jsx(FormItemContext.Provider, { value: { id }, children: jsx("div", { ref: ref, className: cn("space-y-field-label-gap", className), ...props }) }));
|
|
36
|
-
});
|
|
37
|
-
FormItem.displayName = "FormItem";
|
|
38
|
-
const FormLabel = React.forwardRef(({ className, required = false, children, ...props }, ref) => {
|
|
39
|
-
const { error, formItemId } = useFormField();
|
|
40
|
-
return (jsx(Label, { label: children, required: required, className: "mb-0", labelProps: {
|
|
41
|
-
ref,
|
|
42
|
-
htmlFor: formItemId,
|
|
43
|
-
...props,
|
|
44
|
-
className: cn(error && 'text-destructive', className),
|
|
45
|
-
} }));
|
|
46
|
-
});
|
|
47
|
-
FormLabel.displayName = "FormLabel";
|
|
48
|
-
const FormControl = React.forwardRef(({ ...props }, ref) => {
|
|
49
|
-
const { error, formItemId, formDescriptionId, formMessageId } = useFormField();
|
|
50
|
-
return (jsx(Slot, { ref: ref, id: formItemId, "aria-describedby": !error
|
|
51
|
-
? `${formDescriptionId}`
|
|
52
|
-
: `${formDescriptionId} ${formMessageId}`, "aria-invalid": !!error, ...props }));
|
|
53
|
-
});
|
|
54
|
-
FormControl.displayName = "FormControl";
|
|
55
|
-
const FormDescription = React.forwardRef(({ className, ...props }, ref) => {
|
|
56
|
-
const { formDescriptionId } = useFormField();
|
|
57
|
-
return (jsx("p", { ref: ref, id: formDescriptionId, className: cn("text-sm leading-normal text-muted-foreground", className), ...props }));
|
|
58
|
-
});
|
|
59
|
-
FormDescription.displayName = "FormDescription";
|
|
60
|
-
const FormMessage = React.forwardRef(({ className, children, ...props }, ref) => {
|
|
61
|
-
const { error, formMessageId } = useFormField();
|
|
62
|
-
const body = error ? String(error?.message) : children;
|
|
63
|
-
if (!body) {
|
|
64
|
-
return null;
|
|
65
|
-
}
|
|
66
|
-
return (jsx("p", { ref: ref, id: formMessageId, className: cn("text-sm leading-normal font-medium text-destructive", className), ...props, children: body }));
|
|
67
|
-
});
|
|
68
|
-
FormMessage.displayName = "FormMessage";
|
|
69
|
-
|
|
70
|
-
export { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, useFormField };
|
|
71
|
-
//# sourceMappingURL=form.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"form.js","sources":["../../src/components/form.tsx"],"sourcesContent":["\"use client\";\r\n\r\nimport * as React from \"react\";\r\nimport { Slot } from \"@radix-ui/react-slot\";\r\nimport {\r\n Controller,\r\n ControllerProps,\r\n FieldPath,\r\n FieldValues,\r\n FormProvider,\r\n useFormContext,\r\n} from \"react-hook-form\";\r\n\r\nimport { cn } from \"@/utils\";\r\nimport { Label } from './label';\r\n\r\nconst Form = FormProvider;\r\n\r\ntype FormFieldContextValue<\r\n TFieldValues extends FieldValues = FieldValues,\r\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\r\n> = {\r\n name: TName;\r\n};\r\n\r\nconst FormFieldContext = React.createContext<FormFieldContextValue>(\r\n {} as FormFieldContextValue,\r\n);\r\n\r\nconst FormField = <\r\n TFieldValues extends FieldValues = FieldValues,\r\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\r\n>({\r\n ...props\r\n}: ControllerProps<TFieldValues, TName>) => {\r\n return (\r\n <FormFieldContext.Provider value={{ name: props.name }}>\r\n <Controller {...props} />\r\n </FormFieldContext.Provider>\r\n );\r\n};\r\n\r\nconst useFormField = () => {\r\n const fieldContext = React.useContext(FormFieldContext);\r\n const itemContext = React.useContext(FormItemContext);\r\n const { getFieldState, formState } = useFormContext();\r\n\r\n const fieldState = getFieldState(fieldContext.name, formState);\r\n\r\n if (!fieldContext) {\r\n throw new Error(\"useFormField should be used within <FormField>\");\r\n }\r\n\r\n const { id } = itemContext;\r\n\r\n return {\r\n id,\r\n name: fieldContext.name,\r\n formItemId: `${id}-form-item`,\r\n formDescriptionId: `${id}-form-item-description`,\r\n formMessageId: `${id}-form-item-message`,\r\n ...fieldState,\r\n };\r\n};\r\n\r\ntype FormItemContextValue = {\r\n id: string;\r\n};\r\n\r\nconst FormItemContext = React.createContext<FormItemContextValue>(\r\n {} as FormItemContextValue,\r\n);\r\n\r\nconst FormItem = React.forwardRef<\r\n HTMLDivElement,\r\n React.HTMLAttributes<HTMLDivElement>\r\n>(({ className, ...props }, ref) => {\r\n const id = React.useId();\r\n\r\n return (\r\n <FormItemContext.Provider value={{ id }}>\r\n <div ref={ref} className={cn(\"space-y-field-label-gap\", className)} {...props} />\r\n </FormItemContext.Provider>\r\n );\r\n});\r\nFormItem.displayName = \"FormItem\";\r\n\r\nconst FormLabel = React.forwardRef<\r\n HTMLLabelElement,\r\n React.ComponentPropsWithoutRef<'label'> & { required?: boolean }\r\n>(({ className, required = false, children, ...props }, ref) => {\r\n const { error, formItemId } = useFormField();\r\n\r\n return (\r\n <Label\r\n label={children}\r\n required={required}\r\n className=\"mb-0\"\r\n labelProps={{\r\n ref,\r\n htmlFor: formItemId,\r\n ...props,\r\n className: cn(error && 'text-destructive', className),\r\n }}\r\n />\r\n );\r\n});\r\nFormLabel.displayName = \"FormLabel\";\r\n\r\nconst FormControl = React.forwardRef<\r\n React.ElementRef<typeof Slot>,\r\n React.ComponentPropsWithoutRef<typeof Slot>\r\n>(({ ...props }, ref) => {\r\n const { error, formItemId, formDescriptionId, formMessageId } =\r\n useFormField();\r\n\r\n return (\r\n <Slot\r\n ref={ref}\r\n id={formItemId}\r\n aria-describedby={\r\n !error\r\n ? `${formDescriptionId}`\r\n : `${formDescriptionId} ${formMessageId}`\r\n }\r\n aria-invalid={!!error}\r\n {...props}\r\n />\r\n );\r\n});\r\nFormControl.displayName = \"FormControl\";\r\n\r\nconst FormDescription = React.forwardRef<\r\n HTMLParagraphElement,\r\n React.HTMLAttributes<HTMLParagraphElement>\r\n>(({ className, ...props }, ref) => {\r\n const { formDescriptionId } = useFormField();\r\n\r\n return (\r\n <p\r\n ref={ref}\r\n id={formDescriptionId}\r\n className={cn(\"text-sm leading-normal text-muted-foreground\", className)}\r\n {...props}\r\n />\r\n );\r\n});\r\nFormDescription.displayName = \"FormDescription\";\r\n\r\nconst FormMessage = React.forwardRef<\r\n HTMLParagraphElement,\r\n React.HTMLAttributes<HTMLParagraphElement>\r\n>(({ className, children, ...props }, ref) => {\r\n const { error, formMessageId } = useFormField();\r\n const body = error ? String(error?.message) : children;\r\n\r\n if (!body) {\r\n return null;\r\n }\r\n\r\n return (\r\n <p\r\n ref={ref}\r\n id={formMessageId}\r\n className={cn(\"text-sm leading-normal font-medium text-destructive\", className)}\r\n {...props}\r\n >\r\n {body}\r\n </p>\r\n );\r\n});\r\nFormMessage.displayName = \"FormMessage\";\r\n\r\nexport {\r\n useFormField,\r\n Form,\r\n FormItem,\r\n FormLabel,\r\n FormControl,\r\n FormDescription,\r\n FormMessage,\r\n FormField,\r\n};\r\n"],"names":[],"mappings":";;;;;;;;AAgBA;AASA;AAIA;;AAWA;AAEA;;;;;;AAQI;;AAGF;;;;;;;AAQE;;AAEJ;AAMA;AAIA;AAIE;AAEA;AAKF;AACA;AAEA;;AAME;;AAOM;AACA;;AAED;AAGP;AACA;AAEA;AAIE;AAGA;;AAOQ;AAMV;AACA;AAEA;AAIE;;AAUF;AACA;AAEA;;AAKE;;AAGE;;;AAaJ;AACA;;"}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import * as React from 'react';
|
|
2
|
-
export type TimeInputNativeProps = {
|
|
3
|
-
label: string;
|
|
4
|
-
/** "HH:MM" */
|
|
5
|
-
value?: string;
|
|
6
|
-
onChange?: (v?: string) => void;
|
|
7
|
-
required?: boolean;
|
|
8
|
-
helper?: string;
|
|
9
|
-
className?: string;
|
|
10
|
-
tooltip?: string;
|
|
11
|
-
tooltipId?: string;
|
|
12
|
-
labelAddon?: React.ReactNode;
|
|
13
|
-
} & Omit<React.InputHTMLAttributes<HTMLInputElement>, 'onChange'>;
|
|
14
|
-
export declare function TimeInputNative({ label, value, onChange, required, helper, className, tooltip, tooltipId, labelAddon, ...props }: TimeInputNativeProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
3
|
-
import * as React from 'react';
|
|
4
|
-
import { Input } from './input.js';
|
|
5
|
-
import { FormField } from './form-field.js';
|
|
6
|
-
import { Clock } from 'lucide-react';
|
|
7
|
-
import { Popover, PopoverAnchor, PopoverTrigger, PopoverContent } from './popover.js';
|
|
8
|
-
import { useFieldFocus, FieldChromeContext, fieldFocusOutlineClassName, fieldInteractionClassName } from '../hooks/use-field-focus.js';
|
|
9
|
-
import { cn } from '../utils.js';
|
|
10
|
-
|
|
11
|
-
function TimeInputNative({ label, value, onChange, required = true, helper, className, tooltip, tooltipId, labelAddon, ...props }) {
|
|
12
|
-
const [open, setOpen] = React.useState(false);
|
|
13
|
-
const formatTime = (time) => {
|
|
14
|
-
if (!time)
|
|
15
|
-
return '';
|
|
16
|
-
const [hour, minute] = time.split(':');
|
|
17
|
-
return `${String(Number(hour) % 12 || 12).padStart(2, '0')}:${minute} ${Number(hour) >= 12 ? 'PM' : 'AM'}`;
|
|
18
|
-
};
|
|
19
|
-
const [draft, setDraft] = React.useState(() => formatTime(value));
|
|
20
|
-
React.useEffect(() => { setDraft(formatTime(value)); }, [value]);
|
|
21
|
-
const disabled = props.disabled;
|
|
22
|
-
const { keyboardFocus, focusProps } = useFieldFocus();
|
|
23
|
-
const updatePart = (part, next) => {
|
|
24
|
-
const [hour = '09', minute = '00'] = (value || '09:00').split(':');
|
|
25
|
-
const currentHour = Number(hour);
|
|
26
|
-
const nextHour = part === 'hour'
|
|
27
|
-
? Number(next) % 12 + (currentHour >= 12 ? 12 : 0)
|
|
28
|
-
: part === 'period' ? currentHour % 12 + (next === 'PM' ? 12 : 0) : currentHour;
|
|
29
|
-
onChange?.(`${String(nextHour).padStart(2, '0')}:${part === 'minute' ? next : minute}`);
|
|
30
|
-
};
|
|
31
|
-
return (jsxs(FormField, { label: label, required: required, tooltip: tooltip, tooltipId: tooltipId, labelAddon: labelAddon, className: className, children: [jsxs(Popover, { open: open, onOpenChange: setOpen, children: [jsx(PopoverAnchor, { asChild: true, children: jsx("div", { ...focusProps, className: cn('relative', !disabled && fieldInteractionClassName, !disabled && keyboardFocus && fieldFocusOutlineClassName), children: jsxs(FieldChromeContext, { value: true, children: [jsx(Input, { ...props, type: "text", placeholder: props.placeholder ?? 'HH:MM AM/PM', value: draft, onChange: (event) => {
|
|
32
|
-
const next = event.target.value;
|
|
33
|
-
// Permit incomplete time segments while typing, but no arbitrary text.
|
|
34
|
-
if (!/^\d{0,2}(:[0-5]?\d?)?(\s?[AaPp]?[Mm]?)?$/.test(next))
|
|
35
|
-
return;
|
|
36
|
-
const parts = next.match(/^(\d{1,2})/);
|
|
37
|
-
if (parts && Number(parts[1]) > 23)
|
|
38
|
-
return;
|
|
39
|
-
setDraft(next);
|
|
40
|
-
if (!next.trim()) {
|
|
41
|
-
onChange?.(undefined);
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
const match = next.trim().match(/^(\d{1,2}):(\d{2})\s*(AM|PM)?$/i);
|
|
45
|
-
if (!match)
|
|
46
|
-
return;
|
|
47
|
-
let hour = Number(match[1]);
|
|
48
|
-
const minute = Number(match[2]);
|
|
49
|
-
if (minute > 59 || hour > 23 || (match[3] && (hour < 1 || hour > 12)))
|
|
50
|
-
return;
|
|
51
|
-
if (match[3])
|
|
52
|
-
hour = hour % 12 + (match[3].toUpperCase() === 'PM' ? 12 : 0);
|
|
53
|
-
onChange?.(`${String(hour).padStart(2, '0')}:${match[2]}`);
|
|
54
|
-
}, onBlur: (event) => {
|
|
55
|
-
setDraft(formatTime(value));
|
|
56
|
-
props.onBlur?.(event);
|
|
57
|
-
}, onClick: (event) => { props.onClick?.(event); if (!event.defaultPrevented && !disabled && !props.readOnly)
|
|
58
|
-
setOpen(true); }, className: "pr-11" }), jsx(PopoverTrigger, { asChild: true, children: jsx("button", { type: "button", disabled: disabled || props.readOnly, "aria-label": `Choose ${label.toLowerCase()}`, className: "absolute inset-y-0 right-0 z-20 flex w-11 items-center justify-center rounded-control-lg text-muted-foreground hover:text-foreground disabled:opacity-50", children: jsx(Clock, { className: "size-4", "aria-hidden": "true" }) }) })] }) }) }), jsx(PopoverContent, { align: "start", sideOffset: 6, className: "w-72", "aria-label": `Choose ${label.toLowerCase()}`, onOpenAutoFocus: event => event.preventDefault(), children: jsxs("div", { className: "grid grid-cols-3 gap-2", children: [jsx("div", { children: jsx("select", { "aria-label": "Hour", className: "h-form-control rounded-control-lg border border-input bg-background px-field-padding-x text-base text-foreground", value: String(Number(value?.split(':')[0] ?? 9) % 12 || 12).padStart(2, '0'), onChange: event => updatePart('hour', event.target.value), children: Array.from({ length: 12 }, (_, hour) => { const text = String(hour + 1).padStart(2, '0'); return jsx("option", { value: text, children: text }, text); }) }) }), jsx("div", { children: jsx("select", { "aria-label": "Minute", className: "h-form-control rounded-control-lg border border-input bg-background px-field-padding-x text-base text-foreground", value: value?.split(':')[1] ?? '00', onChange: event => updatePart('minute', event.target.value), children: Array.from({ length: 60 }, (_, minute) => { const text = String(minute).padStart(2, '0'); return jsx("option", { value: text, children: text }, text); }) }) }), jsx("div", { children: jsxs("select", { "aria-label": "AM or PM", className: "h-form-control rounded-control-lg border border-input bg-background px-field-padding-x text-base text-foreground", value: Number(value?.split(':')[0] ?? 9) >= 12 ? 'PM' : 'AM', onChange: event => updatePart('period', event.target.value), children: [jsx("option", { value: "AM", children: "AM" }), jsx("option", { value: "PM", children: "PM" })] }) })] }) })] }), helper && jsx("p", { className: "mt-1 text-sm text-muted-foreground", children: helper })] }));
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export { TimeInputNative };
|
|
62
|
-
//# sourceMappingURL=time-input-native.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"time-input-native.js","sources":["../../src/components/time-input-native.tsx"],"sourcesContent":["'use client';\r\n\r\nimport * as React from 'react';\r\n\r\nimport { Input } from '@/components/input';\r\nimport { FormField } from './form-field';\r\nimport { Clock } from 'lucide-react';\r\nimport { Popover, PopoverAnchor, PopoverContent, PopoverTrigger } from './popover';\r\nimport { FieldChromeContext, fieldInteractionClassName, fieldFocusOutlineClassName, useFieldFocus } from '@/hooks/use-field-focus';\r\nimport { cn } from '@/utils';\r\n\r\nexport type TimeInputNativeProps = {\r\n label: string;\r\n /** \"HH:MM\" */\r\n value?: string;\r\n onChange?: (v?: string) => void;\r\n required?: boolean;\r\n helper?: string;\r\n className?: string;\r\n tooltip?: string;\r\n tooltipId?: string;\r\n labelAddon?: React.ReactNode;\r\n} & Omit<React.InputHTMLAttributes<HTMLInputElement>, 'onChange'>;\r\n\r\nexport function TimeInputNative({\r\n label,\r\n value,\r\n onChange,\r\n required = true,\r\n helper,\r\n className,\r\n tooltip,\r\n tooltipId,\r\n labelAddon,\r\n ...props\r\n}: TimeInputNativeProps) {\r\n const [open, setOpen] = React.useState(false);\r\n const formatTime = (time?: string) => {\r\n if (!time) return '';\r\n const [hour, minute] = time.split(':');\r\n return `${String(Number(hour) % 12 || 12).padStart(2, '0')}:${minute} ${Number(hour) >= 12 ? 'PM' : 'AM'}`;\r\n };\r\n const [draft, setDraft] = React.useState(() => formatTime(value));\r\n React.useEffect(() => { setDraft(formatTime(value)); }, [value]);\r\n const disabled = props.disabled;\r\n const { keyboardFocus, focusProps } = useFieldFocus();\r\n const updatePart = (part: 'hour' | 'minute' | 'period', next: string) => {\r\n const [hour = '09', minute = '00'] = (value || '09:00').split(':');\r\n const currentHour = Number(hour);\r\n const nextHour = part === 'hour'\r\n ? Number(next) % 12 + (currentHour >= 12 ? 12 : 0)\r\n : part === 'period' ? currentHour % 12 + (next === 'PM' ? 12 : 0) : currentHour;\r\n onChange?.(`${String(nextHour).padStart(2, '0')}:${part === 'minute' ? next : minute}`);\r\n };\r\n return (\r\n <FormField\r\n label={label}\r\n required={required}\r\n tooltip={tooltip}\r\n tooltipId={tooltipId}\r\n labelAddon={labelAddon}\r\n className={className}\r\n >\r\n <Popover open={open} onOpenChange={setOpen}>\r\n <PopoverAnchor asChild>\r\n <div {...focusProps} className={cn('relative', !disabled && fieldInteractionClassName, !disabled && keyboardFocus && fieldFocusOutlineClassName)}>\r\n <FieldChromeContext value={true}>\r\n <Input\r\n {...props}\r\n type=\"text\"\r\n placeholder={props.placeholder ?? 'HH:MM AM/PM'}\r\n value={draft}\r\n onChange={(event) => {\r\n const next = event.target.value;\r\n // Permit incomplete time segments while typing, but no arbitrary text.\r\n if (!/^\\d{0,2}(:[0-5]?\\d?)?(\\s?[AaPp]?[Mm]?)?$/.test(next)) return;\r\n const parts = next.match(/^(\\d{1,2})/);\r\n if (parts && Number(parts[1]) > 23) return;\r\n setDraft(next);\r\n if (!next.trim()) { onChange?.(undefined); return; }\r\n const match = next.trim().match(/^(\\d{1,2}):(\\d{2})\\s*(AM|PM)?$/i);\r\n if (!match) return;\r\n let hour = Number(match[1]);\r\n const minute = Number(match[2]);\r\n if (minute > 59 || hour > 23 || (match[3] && (hour < 1 || hour > 12))) return;\r\n if (match[3]) hour = hour % 12 + (match[3].toUpperCase() === 'PM' ? 12 : 0);\r\n onChange?.(`${String(hour).padStart(2, '0')}:${match[2]}`);\r\n }}\r\n onBlur={(event) => {\r\n setDraft(formatTime(value));\r\n props.onBlur?.(event);\r\n }}\r\n onClick={(event) => { props.onClick?.(event); if (!event.defaultPrevented && !disabled && !props.readOnly) setOpen(true); }}\r\n className=\"pr-11\"\r\n />\r\n <PopoverTrigger asChild>\r\n <button type=\"button\" disabled={disabled || props.readOnly} aria-label={`Choose ${label.toLowerCase()}`} className=\"absolute inset-y-0 right-0 z-20 flex w-11 items-center justify-center rounded-control-lg text-muted-foreground hover:text-foreground disabled:opacity-50\"><Clock className=\"size-4\" aria-hidden=\"true\" /></button>\r\n </PopoverTrigger>\r\n </FieldChromeContext>\r\n </div>\r\n </PopoverAnchor>\r\n <PopoverContent align=\"start\" sideOffset={6} className=\"w-72\" aria-label={`Choose ${label.toLowerCase()}`} onOpenAutoFocus={event => event.preventDefault()}>\r\n <div className=\"grid grid-cols-3 gap-2\">\r\n <div>\r\n <select aria-label=\"Hour\" className=\"h-form-control rounded-control-lg border border-input bg-background px-field-padding-x text-base text-foreground\" value={String(Number(value?.split(':')[0] ?? 9) % 12 || 12).padStart(2, '0')} onChange={event => updatePart('hour', event.target.value)}>\r\n {Array.from({ length: 12 }, (_, hour) => { const text = String(hour + 1).padStart(2, '0'); return <option key={text} value={text}>{text}</option>; })}\r\n </select>\r\n </div>\r\n <div>\r\n <select aria-label=\"Minute\" className=\"h-form-control rounded-control-lg border border-input bg-background px-field-padding-x text-base text-foreground\" value={value?.split(':')[1] ?? '00'} onChange={event => updatePart('minute', event.target.value)}>\r\n {Array.from({ length: 60 }, (_, minute) => { const text = String(minute).padStart(2, '0'); return <option key={text} value={text}>{text}</option>; })}\r\n </select>\r\n </div>\r\n <div>\r\n <select aria-label=\"AM or PM\" className=\"h-form-control rounded-control-lg border border-input bg-background px-field-padding-x text-base text-foreground\" value={Number(value?.split(':')[0] ?? 9) >= 12 ? 'PM' : 'AM'} onChange={event => updatePart('period', event.target.value)}>\r\n <option value=\"AM\">AM</option><option value=\"PM\">PM</option>\r\n </select>\r\n </div>\r\n </div>\r\n </PopoverContent>\r\n </Popover>\r\n {helper && <p className=\"mt-1 text-sm text-muted-foreground\">{helper}</p>}\r\n </FormField>\r\n );\r\n}\r\n"],"names":[],"mappings":";;;;;;;;;;AAwBM;AAYJ;AACA;AACE;AAAW;AACX;AACA;AACF;AACA;;AAEA;;AAEA;;AAEE;AACA;;AAEE;AACF;AACF;;AAoBQ;;AAEA;;;;;;AAIA;AAAoB;;;;AAEpB;;;;;;;;;AAMF;AAEE;AACA;AACF;AAC2G;AAgCnH;;"}
|