@proyecto-viviana/solid-stately 0.2.4 → 0.3.0
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/LICENSE +21 -0
- package/README.md +30 -283
- package/dist/autocomplete/createAutocompleteState.d.ts +3 -2
- package/dist/autocomplete/index.d.ts +1 -1
- package/dist/calendar/createCalendarState.d.ts +22 -7
- package/dist/calendar/createDateFieldState.d.ts +30 -7
- package/dist/calendar/createDatePickerState.d.ts +60 -0
- package/dist/calendar/createRangeCalendarState.d.ts +27 -8
- package/dist/calendar/createTimeFieldState.d.ts +34 -7
- package/dist/calendar/index.d.ts +7 -6
- package/dist/checkbox/createCheckboxGroupState.d.ts +12 -3
- package/dist/checkbox/index.d.ts +1 -1
- package/dist/collections/ListCollection.d.ts +1 -1
- package/dist/collections/createListState.d.ts +8 -8
- package/dist/collections/createMenuState.d.ts +7 -7
- package/dist/collections/createSelectionState.d.ts +5 -5
- package/dist/collections/index.d.ts +5 -5
- package/dist/collections/types.d.ts +23 -12
- package/dist/color/Color.d.ts +1 -1
- package/dist/color/createColorAreaState.d.ts +4 -2
- package/dist/color/createColorFieldState.d.ts +38 -12
- package/dist/color/createColorSliderState.d.ts +9 -2
- package/dist/color/createColorWheelState.d.ts +13 -2
- package/dist/color/getColorChannels.d.ts +20 -0
- package/dist/color/index.d.ts +6 -6
- package/dist/color/types.d.ts +4 -4
- package/dist/combobox/createComboBoxState.d.ts +27 -13
- package/dist/combobox/index.d.ts +1 -1
- package/dist/data/createAsyncList.d.ts +111 -0
- package/dist/data/createListData.d.ts +65 -0
- package/dist/data/createTreeData.d.ts +65 -0
- package/dist/data/index.d.ts +3 -0
- package/dist/datepicker/index.d.ts +8 -0
- package/dist/disclosure/createDisclosureState.d.ts +2 -2
- package/dist/disclosure/index.d.ts +1 -1
- package/dist/dnd/createDragState.d.ts +2 -2
- package/dist/dnd/createDraggableCollectionState.d.ts +2 -2
- package/dist/dnd/createDropState.d.ts +2 -2
- package/dist/dnd/createDroppableCollectionState.d.ts +3 -3
- package/dist/dnd/index.d.ts +5 -5
- package/dist/dnd/types.d.ts +16 -16
- package/dist/form/createFormValidationState.d.ts +3 -3
- package/dist/form/index.d.ts +1 -1
- package/dist/grid/createGridState.d.ts +2 -2
- package/dist/grid/index.d.ts +2 -2
- package/dist/grid/types.d.ts +17 -11
- package/dist/index.d.ts +31 -25
- package/dist/index.js +7990 -6264
- package/dist/index.js.map +1 -7
- package/dist/menu/index.d.ts +8 -0
- package/dist/numberfield/createNumberFieldState.d.ts +2 -2
- package/dist/numberfield/index.d.ts +1 -1
- package/dist/overlays/createOverlayTriggerState.d.ts +2 -2
- package/dist/overlays/index.d.ts +1 -1
- package/dist/radio/createRadioGroupState.d.ts +13 -4
- package/dist/radio/index.d.ts +1 -1
- package/dist/searchfield/createSearchFieldState.d.ts +2 -2
- package/dist/searchfield/index.d.ts +2 -2
- package/dist/select/createSelectState.d.ts +20 -3
- package/dist/select/index.d.ts +1 -1
- package/dist/selection/index.d.ts +11 -0
- package/dist/slider/createSliderState.d.ts +3 -3
- package/dist/slider/index.d.ts +2 -2
- package/dist/steplist/createStepListState.d.ts +48 -0
- package/dist/steplist/index.d.ts +1 -0
- package/dist/table/TableCollection.d.ts +3 -3
- package/dist/table/createTableColumnResizeState.d.ts +54 -0
- package/dist/table/createTableState.d.ts +2 -2
- package/dist/table/index.d.ts +5 -3
- package/dist/table/types.d.ts +10 -10
- package/dist/tabs/createTabListState.d.ts +5 -5
- package/dist/tabs/index.d.ts +1 -1
- package/dist/textfield/createTextFieldState.d.ts +2 -2
- package/dist/textfield/index.d.ts +1 -1
- package/dist/toast/createToastState.d.ts +15 -3
- package/dist/toast/index.d.ts +1 -1
- package/dist/toggle/createToggleGroupState.d.ts +45 -0
- package/dist/toggle/createToggleState.d.ts +2 -2
- package/dist/toggle/index.d.ts +2 -1
- package/dist/tooltip/createTooltipTriggerState.d.ts +2 -2
- package/dist/tooltip/index.d.ts +1 -1
- package/dist/tree/TreeCollection.d.ts +5 -4
- package/dist/tree/createTreeState.d.ts +2 -2
- package/dist/tree/index.d.ts +3 -3
- package/dist/tree/types.d.ts +19 -13
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/reactivity.d.ts +1 -1
- package/package.json +30 -30
- package/src/autocomplete/createAutocompleteState.d.ts +19 -17
- package/src/autocomplete/createAutocompleteState.ts +25 -32
- package/src/autocomplete/index.d.ts +6 -2
- package/src/autocomplete/index.ts +1 -1
- package/src/calendar/createCalendarState.d.ts +139 -117
- package/src/calendar/createCalendarState.ts +221 -74
- package/src/calendar/createDateFieldState.d.ts +130 -95
- package/src/calendar/createDateFieldState.ts +286 -151
- package/src/calendar/createDatePickerState.ts +347 -0
- package/src/calendar/createRangeCalendarState.d.ts +154 -129
- package/src/calendar/createRangeCalendarState.ts +293 -79
- package/src/calendar/createTimeFieldState.d.ts +81 -79
- package/src/calendar/createTimeFieldState.ts +187 -108
- package/src/calendar/index.d.ts +80 -7
- package/src/calendar/index.ts +21 -12
- package/src/checkbox/createCheckboxGroupState.d.ts +57 -55
- package/src/checkbox/createCheckboxGroupState.ts +57 -30
- package/src/checkbox/index.d.ts +6 -2
- package/src/checkbox/index.ts +1 -1
- package/src/collections/ListCollection.d.ts +25 -22
- package/src/collections/ListCollection.ts +152 -146
- package/src/collections/createListState.d.ts +73 -58
- package/src/collections/createListState.ts +267 -264
- package/src/collections/createMenuState.d.ts +36 -31
- package/src/collections/createMenuState.ts +112 -106
- package/src/collections/createSelectionState.d.ts +78 -65
- package/src/collections/createSelectionState.ts +340 -336
- package/src/collections/index.d.ts +38 -6
- package/src/collections/index.ts +41 -46
- package/src/collections/types.d.ts +101 -101
- package/src/collections/types.ts +181 -169
- package/src/color/Color.d.ts +20 -5
- package/src/color/Color.ts +1054 -951
- package/src/color/createColorAreaState.d.ts +67 -65
- package/src/color/createColorAreaState.ts +301 -293
- package/src/color/createColorFieldState.d.ts +46 -44
- package/src/color/createColorFieldState.ts +428 -292
- package/src/color/createColorSliderState.d.ts +58 -56
- package/src/color/createColorSliderState.ts +272 -241
- package/src/color/createColorWheelState.d.ts +42 -40
- package/src/color/createColorWheelState.ts +248 -211
- package/src/color/getColorChannels.ts +36 -0
- package/src/color/index.d.ts +36 -7
- package/src/color/index.ts +44 -47
- package/src/color/types.d.ts +68 -63
- package/src/color/types.ts +119 -127
- package/src/combobox/createComboBoxState.d.ts +107 -105
- package/src/combobox/createComboBoxState.ts +790 -703
- package/src/combobox/index.d.ts +10 -2
- package/src/combobox/index.ts +13 -13
- package/src/data/createAsyncList.ts +403 -0
- package/src/data/createListData.ts +298 -0
- package/src/data/createTreeData.ts +646 -0
- package/src/data/index.ts +25 -0
- package/src/datepicker/index.ts +32 -0
- package/src/disclosure/createDisclosureState.d.ts +47 -43
- package/src/disclosure/createDisclosureState.ts +9 -23
- package/src/disclosure/index.d.ts +10 -2
- package/src/disclosure/index.ts +1 -1
- package/src/dnd/createDragState.d.ts +51 -42
- package/src/dnd/createDragState.ts +151 -153
- package/src/dnd/createDraggableCollectionState.d.ts +52 -41
- package/src/dnd/createDraggableCollectionState.ts +158 -165
- package/src/dnd/createDropState.d.ts +64 -45
- package/src/dnd/createDropState.ts +202 -212
- package/src/dnd/createDroppableCollectionState.d.ts +88 -59
- package/src/dnd/createDroppableCollectionState.ts +354 -357
- package/src/dnd/index.d.ts +54 -6
- package/src/dnd/index.ts +63 -76
- package/src/dnd/types.d.ts +167 -156
- package/src/dnd/types.ts +316 -317
- package/src/form/createFormValidationState.d.ts +47 -45
- package/src/form/createFormValidationState.ts +356 -389
- package/src/form/index.d.ts +16 -2
- package/src/form/index.ts +15 -15
- package/src/grid/createGridState.d.ts +7 -4
- package/src/grid/createGridState.ts +44 -51
- package/src/grid/index.d.ts +3 -3
- package/src/grid/index.ts +2 -8
- package/src/grid/types.d.ts +136 -129
- package/src/grid/types.ts +24 -17
- package/src/index.d.ts +310 -26
- package/src/index.ts +87 -79
- package/src/menu/index.ts +17 -0
- package/src/numberfield/createNumberFieldState.d.ts +56 -54
- package/src/numberfield/createNumberFieldState.ts +422 -383
- package/src/numberfield/index.d.ts +6 -2
- package/src/numberfield/index.ts +5 -5
- package/src/overlays/createOverlayTriggerState.d.ts +22 -20
- package/src/overlays/createOverlayTriggerState.ts +66 -67
- package/src/overlays/index.d.ts +6 -2
- package/src/overlays/index.ts +5 -5
- package/src/radio/createRadioGroupState.d.ts +54 -52
- package/src/radio/createRadioGroupState.ts +66 -30
- package/src/radio/index.d.ts +7 -2
- package/src/radio/index.ts +1 -1
- package/src/searchfield/createSearchFieldState.d.ts +16 -14
- package/src/searchfield/createSearchFieldState.ts +56 -62
- package/src/searchfield/index.d.ts +3 -3
- package/src/searchfield/index.ts +2 -5
- package/src/select/createSelectState.d.ts +63 -61
- package/src/select/createSelectState.ts +290 -181
- package/src/select/index.d.ts +2 -2
- package/src/select/index.ts +1 -5
- package/src/selection/index.ts +27 -0
- package/src/slider/createSliderState.d.ts +60 -60
- package/src/slider/createSliderState.ts +233 -211
- package/src/slider/index.d.ts +3 -3
- package/src/slider/index.ts +2 -6
- package/src/ssr/index.d.ts +1 -1
- package/src/ssr/index.ts +2 -2
- package/src/steplist/createStepListState.ts +191 -0
- package/src/steplist/index.ts +5 -0
- package/src/table/TableCollection.d.ts +42 -40
- package/src/table/TableCollection.ts +61 -64
- package/src/table/createTableColumnResizeState.ts +242 -0
- package/src/table/createTableState.d.ts +7 -4
- package/src/table/createTableState.ts +10 -7
- package/src/table/index.d.ts +14 -4
- package/src/table/index.ts +10 -3
- package/src/table/types.d.ts +98 -92
- package/src/table/types.ts +17 -13
- package/src/tabs/createTabListState.d.ts +59 -57
- package/src/tabs/createTabListState.ts +58 -39
- package/src/tabs/index.d.ts +8 -2
- package/src/tabs/index.ts +1 -1
- package/src/textfield/createTextFieldState.d.ts +16 -14
- package/src/textfield/createTextFieldState.ts +7 -18
- package/src/textfield/index.d.ts +6 -2
- package/src/textfield/index.ts +1 -1
- package/src/toast/createToastState.d.ts +92 -81
- package/src/toast/createToastState.ts +56 -44
- package/src/toast/index.d.ts +12 -2
- package/src/toast/index.ts +1 -1
- package/src/toggle/createToggleGroupState.ts +117 -0
- package/src/toggle/createToggleState.d.ts +19 -19
- package/src/toggle/createToggleState.ts +2 -16
- package/src/toggle/index.d.ts +2 -2
- package/src/toggle/index.ts +6 -4
- package/src/tooltip/createTooltipTriggerState.d.ts +17 -17
- package/src/tooltip/createTooltipTriggerState.ts +184 -183
- package/src/tooltip/index.d.ts +7 -2
- package/src/tooltip/index.ts +6 -6
- package/src/tree/TreeCollection.d.ts +26 -23
- package/src/tree/TreeCollection.ts +215 -175
- package/src/tree/createTreeState.d.ts +7 -4
- package/src/tree/createTreeState.ts +390 -392
- package/src/tree/index.d.ts +10 -4
- package/src/tree/index.ts +13 -13
- package/src/tree/types.d.ts +131 -126
- package/src/tree/types.ts +183 -174
- package/src/utils/index.d.ts +2 -2
- package/src/utils/index.ts +1 -1
- package/src/utils/reactivity.d.ts +2 -2
- package/src/utils/reactivity.ts +3 -3
|
@@ -5,29 +5,30 @@
|
|
|
5
5
|
* Based on @react-stately/datepicker useTimeFieldState
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import { createSignal, createMemo, type Accessor } from
|
|
8
|
+
import { createSignal, createMemo, type Accessor } from "solid-js";
|
|
9
9
|
import {
|
|
10
10
|
type Time,
|
|
11
11
|
type CalendarDateTime,
|
|
12
12
|
type ZonedDateTime,
|
|
13
|
+
type DateValue,
|
|
13
14
|
getLocalTimeZone,
|
|
14
15
|
DateFormatter,
|
|
15
|
-
} from
|
|
16
|
-
import { access, type MaybeAccessor } from
|
|
17
|
-
import type { ValidationState } from
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
16
|
+
} from "@internationalized/date";
|
|
17
|
+
import { access, type MaybeAccessor } from "../utils";
|
|
18
|
+
import type { ValidationState } from "./createCalendarState";
|
|
19
|
+
import {
|
|
20
|
+
createFormValidationState,
|
|
21
|
+
DEFAULT_VALIDATION_RESULT,
|
|
22
|
+
VALID_VALIDITY_STATE,
|
|
23
|
+
type ValidationBehavior,
|
|
24
|
+
type ValidationFunction,
|
|
25
|
+
type ValidationResult,
|
|
26
|
+
type ValidityState,
|
|
27
|
+
} from "../form";
|
|
22
28
|
|
|
23
29
|
export type TimeValue = Time | CalendarDateTime | ZonedDateTime;
|
|
24
30
|
|
|
25
|
-
export type TimeSegmentType =
|
|
26
|
-
| 'hour'
|
|
27
|
-
| 'minute'
|
|
28
|
-
| 'second'
|
|
29
|
-
| 'dayPeriod'
|
|
30
|
-
| 'literal';
|
|
31
|
+
export type TimeSegmentType = "hour" | "minute" | "second" | "dayPeriod" | "literal";
|
|
31
32
|
|
|
32
33
|
export interface TimeSegment {
|
|
33
34
|
/** The type of segment. */
|
|
@@ -65,21 +66,37 @@ export interface TimeFieldStateProps<T extends TimeValue = Time> {
|
|
|
65
66
|
isReadOnly?: MaybeAccessor<boolean>;
|
|
66
67
|
/** Whether the field is required. */
|
|
67
68
|
isRequired?: MaybeAccessor<boolean>;
|
|
69
|
+
/** Whether the value is invalid (controlled). */
|
|
70
|
+
isInvalid?: boolean;
|
|
68
71
|
/** The locale to use for formatting. */
|
|
69
72
|
locale?: string;
|
|
70
73
|
/** The granularity (hour, minute, second). */
|
|
71
|
-
granularity?:
|
|
74
|
+
granularity?: "hour" | "minute" | "second";
|
|
72
75
|
/** Whether to show 12 or 24 hour format. */
|
|
73
76
|
hourCycle?: 12 | 24;
|
|
77
|
+
/** Whether to force leading zeroes in the hour segment. */
|
|
78
|
+
shouldForceLeadingZeros?: boolean;
|
|
74
79
|
/** Validation state. */
|
|
75
80
|
validationState?: MaybeAccessor<ValidationState | undefined>;
|
|
81
|
+
/** Validation behavior mode. */
|
|
82
|
+
validationBehavior?: ValidationBehavior;
|
|
83
|
+
/** Custom validation function. */
|
|
84
|
+
validate?: ValidationFunction<T>;
|
|
85
|
+
/** Description text. */
|
|
86
|
+
description?: string;
|
|
87
|
+
/** Error message. */
|
|
88
|
+
errorMessage?: string;
|
|
76
89
|
/** The placeholder value. */
|
|
77
90
|
placeholderValue?: T;
|
|
91
|
+
/** Whether dates outside the min/max range are unavailable. Added for API consistency. */
|
|
92
|
+
isDateUnavailable?: (date: DateValue) => boolean;
|
|
78
93
|
}
|
|
79
94
|
|
|
80
95
|
export interface TimeFieldState<T extends TimeValue = Time> {
|
|
81
96
|
/** The current value. */
|
|
82
97
|
value: Accessor<T | null>;
|
|
98
|
+
/** The default value. */
|
|
99
|
+
defaultValue: T | null;
|
|
83
100
|
/** Sets the value. */
|
|
84
101
|
setValue: (value: T | null) => void;
|
|
85
102
|
/** The segments that make up the time. */
|
|
@@ -101,34 +118,40 @@ export interface TimeFieldState<T extends TimeValue = Time> {
|
|
|
101
118
|
/** The validation state. */
|
|
102
119
|
validationState: Accessor<ValidationState | undefined>;
|
|
103
120
|
/** The granularity. */
|
|
104
|
-
granularity:
|
|
121
|
+
granularity: "hour" | "minute" | "second";
|
|
105
122
|
/** Whether the value is invalid. */
|
|
106
123
|
isInvalid: Accessor<boolean>;
|
|
124
|
+
/** Realtime validation results, including native and custom constraints. */
|
|
125
|
+
realtimeValidation: Accessor<ValidationResult>;
|
|
126
|
+
/** Currently displayed validation results. */
|
|
127
|
+
displayValidation: Accessor<ValidationResult>;
|
|
128
|
+
/** Updates the current validation result. */
|
|
129
|
+
updateValidation: (result: ValidationResult) => void;
|
|
130
|
+
/** Resets displayed validation to valid. */
|
|
131
|
+
resetValidation: () => void;
|
|
132
|
+
/** Commits realtime validation to displayed validation. */
|
|
133
|
+
commitValidation: () => void;
|
|
107
134
|
/** The locale. */
|
|
108
135
|
locale: string;
|
|
109
136
|
/** The hour cycle. */
|
|
110
137
|
hourCycle: 12 | 24;
|
|
111
138
|
}
|
|
112
139
|
|
|
113
|
-
// ============================================
|
|
114
|
-
// IMPLEMENTATION
|
|
115
|
-
// ============================================
|
|
116
|
-
|
|
117
140
|
/**
|
|
118
141
|
* Provides state management for a time field component.
|
|
119
142
|
*/
|
|
120
143
|
export function createTimeFieldState<T extends TimeValue = Time>(
|
|
121
|
-
props: TimeFieldStateProps<T> = {}
|
|
144
|
+
props: TimeFieldStateProps<T> = {},
|
|
122
145
|
): TimeFieldState<T> {
|
|
123
146
|
const timeZone = getLocalTimeZone();
|
|
124
|
-
const locale = props.locale ??
|
|
125
|
-
const granularity = props.granularity ??
|
|
147
|
+
const locale = props.locale ?? "en-US";
|
|
148
|
+
const granularity = props.granularity ?? "minute";
|
|
126
149
|
const hourCycle = props.hourCycle ?? 12;
|
|
127
150
|
|
|
151
|
+
const initialValue = props.defaultValue ?? null;
|
|
152
|
+
|
|
128
153
|
// State signals
|
|
129
|
-
const [internalValue, setInternalValue] = createSignal<T | null>(
|
|
130
|
-
props.defaultValue ?? null
|
|
131
|
-
);
|
|
154
|
+
const [internalValue, setInternalValue] = createSignal<T | null>(initialValue);
|
|
132
155
|
|
|
133
156
|
// Track partial values during editing
|
|
134
157
|
const [placeholderParts, setPlaceholderParts] = createSignal<Partial<TimeParts>>({});
|
|
@@ -143,21 +166,62 @@ export function createTimeFieldState<T extends TimeValue = Time>(
|
|
|
143
166
|
const isDisabled = createMemo(() => access(props.isDisabled) ?? false);
|
|
144
167
|
const isReadOnly = createMemo(() => access(props.isReadOnly) ?? false);
|
|
145
168
|
const isRequired = createMemo(() => access(props.isRequired) ?? false);
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
const isInvalid = createMemo(() => {
|
|
169
|
+
const explicitValidationState = createMemo(() => access(props.validationState));
|
|
170
|
+
const validationBehavior = () => props.validationBehavior ?? "native";
|
|
171
|
+
const builtinValidation = createMemo<ValidationResult>(() => {
|
|
150
172
|
const v = value();
|
|
151
|
-
if (!v) return
|
|
173
|
+
if (!v) return DEFAULT_VALIDATION_RESULT;
|
|
152
174
|
|
|
153
175
|
const minValue = access(props.minValue);
|
|
154
176
|
const maxValue = access(props.maxValue);
|
|
177
|
+
const validationDetails: ValidityState = { ...VALID_VALIDITY_STATE };
|
|
178
|
+
const validationErrors: string[] = [];
|
|
155
179
|
|
|
156
|
-
if (minValue && compareTime(v, minValue) < 0)
|
|
157
|
-
|
|
180
|
+
if (minValue && compareTime(v, minValue) < 0) {
|
|
181
|
+
validationDetails.rangeUnderflow = true;
|
|
182
|
+
validationErrors.push("Value is below the minimum time.");
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (maxValue && compareTime(v, maxValue) > 0) {
|
|
186
|
+
validationDetails.rangeOverflow = true;
|
|
187
|
+
validationErrors.push("Value is above the maximum time.");
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (validationErrors.length === 0) {
|
|
191
|
+
return DEFAULT_VALIDATION_RESULT;
|
|
192
|
+
}
|
|
158
193
|
|
|
159
|
-
|
|
194
|
+
validationDetails.valid = false;
|
|
195
|
+
return {
|
|
196
|
+
isInvalid: true,
|
|
197
|
+
validationDetails,
|
|
198
|
+
validationErrors,
|
|
199
|
+
};
|
|
200
|
+
});
|
|
201
|
+
const validation = createFormValidationState<T>({
|
|
202
|
+
get value() {
|
|
203
|
+
return value();
|
|
204
|
+
},
|
|
205
|
+
get isInvalid() {
|
|
206
|
+
return props.isInvalid;
|
|
207
|
+
},
|
|
208
|
+
get validationState() {
|
|
209
|
+
return explicitValidationState();
|
|
210
|
+
},
|
|
211
|
+
get validationBehavior() {
|
|
212
|
+
return validationBehavior();
|
|
213
|
+
},
|
|
214
|
+
get validate() {
|
|
215
|
+
return props.validate;
|
|
216
|
+
},
|
|
217
|
+
get builtinValidation() {
|
|
218
|
+
return builtinValidation();
|
|
219
|
+
},
|
|
160
220
|
});
|
|
221
|
+
const isInvalid = createMemo(() => validation.displayValidation().isInvalid);
|
|
222
|
+
const validationState = createMemo<ValidationState | undefined>(
|
|
223
|
+
() => explicitValidationState() ?? (isInvalid() ? "invalid" : undefined),
|
|
224
|
+
);
|
|
161
225
|
|
|
162
226
|
// Generate segments based on granularity
|
|
163
227
|
const segments = createMemo<TimeSegment[]>(() => {
|
|
@@ -166,14 +230,15 @@ export function createTimeFieldState<T extends TimeValue = Time>(
|
|
|
166
230
|
const segs: TimeSegment[] = [];
|
|
167
231
|
|
|
168
232
|
// Determine format options
|
|
233
|
+
const shouldForceLeadingZeros = props.shouldForceLeadingZeros === true;
|
|
169
234
|
const formatOptions: Intl.DateTimeFormatOptions = {
|
|
170
|
-
hour:
|
|
171
|
-
minute:
|
|
172
|
-
hourCycle: hourCycle === 12 ?
|
|
235
|
+
hour: shouldForceLeadingZeros ? "2-digit" : "numeric",
|
|
236
|
+
minute: "2-digit",
|
|
237
|
+
hourCycle: hourCycle === 12 ? "h12" : "h23",
|
|
173
238
|
};
|
|
174
239
|
|
|
175
|
-
if (granularity ===
|
|
176
|
-
formatOptions.second =
|
|
240
|
+
if (granularity === "second") {
|
|
241
|
+
formatOptions.second = "2-digit";
|
|
177
242
|
}
|
|
178
243
|
|
|
179
244
|
// Use a base date for formatting
|
|
@@ -188,9 +253,9 @@ export function createTimeFieldState<T extends TimeValue = Time>(
|
|
|
188
253
|
for (const part of formattedParts) {
|
|
189
254
|
const type = mapTimePartType(part.type);
|
|
190
255
|
|
|
191
|
-
if (type ===
|
|
256
|
+
if (type === "literal") {
|
|
192
257
|
segs.push({
|
|
193
|
-
type:
|
|
258
|
+
type: "literal",
|
|
194
259
|
text: part.value,
|
|
195
260
|
isEditable: false,
|
|
196
261
|
isPlaceholder: false,
|
|
@@ -236,7 +301,7 @@ export function createTimeFieldState<T extends TimeValue = Time>(
|
|
|
236
301
|
// Set a specific segment value
|
|
237
302
|
const setSegment = (type: TimeSegmentType, newValue: number) => {
|
|
238
303
|
if (isDisabled() || isReadOnly()) return;
|
|
239
|
-
if (type ===
|
|
304
|
+
if (type === "literal") return;
|
|
240
305
|
|
|
241
306
|
const v = value();
|
|
242
307
|
|
|
@@ -254,20 +319,18 @@ export function createTimeFieldState<T extends TimeValue = Time>(
|
|
|
254
319
|
// Increment a segment
|
|
255
320
|
const incrementSegment = (type: TimeSegmentType) => {
|
|
256
321
|
if (isDisabled() || isReadOnly()) return;
|
|
257
|
-
if (type ===
|
|
322
|
+
if (type === "literal") return;
|
|
258
323
|
|
|
259
324
|
const v = value();
|
|
260
|
-
const current = v
|
|
261
|
-
? getTimeSegmentValue(v, type)
|
|
262
|
-
: placeholderParts()[type as keyof TimeParts];
|
|
325
|
+
const current = v ? getTimeSegmentValue(v, type) : placeholderParts()[type as keyof TimeParts];
|
|
263
326
|
const max = getTimeMaxValue(type, hourCycle);
|
|
264
327
|
const min = getTimeMinValue(type, hourCycle);
|
|
265
328
|
|
|
266
|
-
if (type ===
|
|
329
|
+
if (type === "dayPeriod") {
|
|
267
330
|
// Toggle AM/PM
|
|
268
331
|
const currentHour = v ? getHour(v) : (placeholderParts().hour ?? 0);
|
|
269
332
|
const newHour = currentHour >= 12 ? currentHour - 12 : currentHour + 12;
|
|
270
|
-
setSegment(
|
|
333
|
+
setSegment("hour", newHour);
|
|
271
334
|
return;
|
|
272
335
|
}
|
|
273
336
|
|
|
@@ -278,20 +341,18 @@ export function createTimeFieldState<T extends TimeValue = Time>(
|
|
|
278
341
|
// Decrement a segment
|
|
279
342
|
const decrementSegment = (type: TimeSegmentType) => {
|
|
280
343
|
if (isDisabled() || isReadOnly()) return;
|
|
281
|
-
if (type ===
|
|
344
|
+
if (type === "literal") return;
|
|
282
345
|
|
|
283
346
|
const v = value();
|
|
284
|
-
const current = v
|
|
285
|
-
? getTimeSegmentValue(v, type)
|
|
286
|
-
: placeholderParts()[type as keyof TimeParts];
|
|
347
|
+
const current = v ? getTimeSegmentValue(v, type) : placeholderParts()[type as keyof TimeParts];
|
|
287
348
|
const max = getTimeMaxValue(type, hourCycle);
|
|
288
349
|
const min = getTimeMinValue(type, hourCycle);
|
|
289
350
|
|
|
290
|
-
if (type ===
|
|
351
|
+
if (type === "dayPeriod") {
|
|
291
352
|
// Toggle AM/PM
|
|
292
353
|
const currentHour = v ? getHour(v) : (placeholderParts().hour ?? 0);
|
|
293
354
|
const newHour = currentHour >= 12 ? currentHour - 12 : currentHour + 12;
|
|
294
|
-
setSegment(
|
|
355
|
+
setSegment("hour", newHour);
|
|
295
356
|
return;
|
|
296
357
|
}
|
|
297
358
|
|
|
@@ -317,6 +378,7 @@ export function createTimeFieldState<T extends TimeValue = Time>(
|
|
|
317
378
|
|
|
318
379
|
return {
|
|
319
380
|
value,
|
|
381
|
+
defaultValue: initialValue,
|
|
320
382
|
setValue,
|
|
321
383
|
segments,
|
|
322
384
|
setSegment,
|
|
@@ -329,15 +391,16 @@ export function createTimeFieldState<T extends TimeValue = Time>(
|
|
|
329
391
|
validationState,
|
|
330
392
|
granularity,
|
|
331
393
|
isInvalid,
|
|
394
|
+
realtimeValidation: validation.realtimeValidation,
|
|
395
|
+
displayValidation: validation.displayValidation,
|
|
396
|
+
updateValidation: validation.updateValidation,
|
|
397
|
+
resetValidation: validation.resetValidation,
|
|
398
|
+
commitValidation: validation.commitValidation,
|
|
332
399
|
locale,
|
|
333
400
|
hourCycle,
|
|
334
401
|
};
|
|
335
402
|
}
|
|
336
403
|
|
|
337
|
-
// ============================================
|
|
338
|
-
// HELPER TYPES & FUNCTIONS
|
|
339
|
-
// ============================================
|
|
340
|
-
|
|
341
404
|
interface TimeParts {
|
|
342
405
|
hour?: number;
|
|
343
406
|
minute?: number;
|
|
@@ -346,56 +409,53 @@ interface TimeParts {
|
|
|
346
409
|
|
|
347
410
|
function mapTimePartType(type: Intl.DateTimeFormatPartTypes): TimeSegmentType | null {
|
|
348
411
|
switch (type) {
|
|
349
|
-
case
|
|
350
|
-
return
|
|
351
|
-
case
|
|
352
|
-
return
|
|
353
|
-
case
|
|
354
|
-
return
|
|
355
|
-
case
|
|
356
|
-
return
|
|
357
|
-
case
|
|
358
|
-
return
|
|
412
|
+
case "hour":
|
|
413
|
+
return "hour";
|
|
414
|
+
case "minute":
|
|
415
|
+
return "minute";
|
|
416
|
+
case "second":
|
|
417
|
+
return "second";
|
|
418
|
+
case "dayPeriod":
|
|
419
|
+
return "dayPeriod";
|
|
420
|
+
case "literal":
|
|
421
|
+
return "literal";
|
|
359
422
|
default:
|
|
360
423
|
return null;
|
|
361
424
|
}
|
|
362
425
|
}
|
|
363
426
|
|
|
364
427
|
function getHour(time: TimeValue): number {
|
|
365
|
-
if (
|
|
428
|
+
if ("hour" in time) {
|
|
366
429
|
return time.hour;
|
|
367
430
|
}
|
|
368
431
|
return 0;
|
|
369
432
|
}
|
|
370
433
|
|
|
371
434
|
function getMinute(time: TimeValue): number {
|
|
372
|
-
if (
|
|
435
|
+
if ("minute" in time) {
|
|
373
436
|
return time.minute;
|
|
374
437
|
}
|
|
375
438
|
return 0;
|
|
376
439
|
}
|
|
377
440
|
|
|
378
441
|
function getSecond(time: TimeValue): number {
|
|
379
|
-
if (
|
|
442
|
+
if ("second" in time) {
|
|
380
443
|
return time.second;
|
|
381
444
|
}
|
|
382
445
|
return 0;
|
|
383
446
|
}
|
|
384
447
|
|
|
385
|
-
function getTimeSegmentValue(
|
|
386
|
-
time: TimeValue | null,
|
|
387
|
-
type: TimeSegmentType
|
|
388
|
-
): number | undefined {
|
|
448
|
+
function getTimeSegmentValue(time: TimeValue | null, type: TimeSegmentType): number | undefined {
|
|
389
449
|
if (!time) return undefined;
|
|
390
450
|
|
|
391
451
|
switch (type) {
|
|
392
|
-
case
|
|
452
|
+
case "hour":
|
|
393
453
|
return getHour(time);
|
|
394
|
-
case
|
|
454
|
+
case "minute":
|
|
395
455
|
return getMinute(time);
|
|
396
|
-
case
|
|
456
|
+
case "second":
|
|
397
457
|
return getSecond(time);
|
|
398
|
-
case
|
|
458
|
+
case "dayPeriod":
|
|
399
459
|
return getHour(time) >= 12 ? 1 : 0;
|
|
400
460
|
default:
|
|
401
461
|
return undefined;
|
|
@@ -404,13 +464,13 @@ function getTimeSegmentValue(
|
|
|
404
464
|
|
|
405
465
|
function getTimeMinValue(type: TimeSegmentType, hourCycle: number): number {
|
|
406
466
|
switch (type) {
|
|
407
|
-
case
|
|
467
|
+
case "hour":
|
|
408
468
|
return hourCycle === 12 ? 1 : 0;
|
|
409
|
-
case
|
|
469
|
+
case "minute":
|
|
410
470
|
return 0;
|
|
411
|
-
case
|
|
471
|
+
case "second":
|
|
412
472
|
return 0;
|
|
413
|
-
case
|
|
473
|
+
case "dayPeriod":
|
|
414
474
|
return 0;
|
|
415
475
|
default:
|
|
416
476
|
return 0;
|
|
@@ -419,13 +479,14 @@ function getTimeMinValue(type: TimeSegmentType, hourCycle: number): number {
|
|
|
419
479
|
|
|
420
480
|
function getTimeMaxValue(type: TimeSegmentType, hourCycle: number): number {
|
|
421
481
|
switch (type) {
|
|
422
|
-
case
|
|
482
|
+
case "hour":
|
|
483
|
+
// In 12-hour mode, the spinbutton displays 1–12, so aria-valuemax is 12.
|
|
423
484
|
return hourCycle === 12 ? 12 : 23;
|
|
424
|
-
case
|
|
485
|
+
case "minute":
|
|
425
486
|
return 59;
|
|
426
|
-
case
|
|
487
|
+
case "second":
|
|
427
488
|
return 59;
|
|
428
|
-
case
|
|
489
|
+
case "dayPeriod":
|
|
429
490
|
return 1;
|
|
430
491
|
default:
|
|
431
492
|
return 0;
|
|
@@ -434,16 +495,16 @@ function getTimeMaxValue(type: TimeSegmentType, hourCycle: number): number {
|
|
|
434
495
|
|
|
435
496
|
function getTimePlaceholderText(type: TimeSegmentType): string {
|
|
436
497
|
switch (type) {
|
|
437
|
-
case
|
|
438
|
-
return
|
|
439
|
-
case
|
|
440
|
-
return
|
|
441
|
-
case
|
|
442
|
-
return
|
|
443
|
-
case
|
|
444
|
-
return
|
|
498
|
+
case "hour":
|
|
499
|
+
return "––";
|
|
500
|
+
case "minute":
|
|
501
|
+
return "––";
|
|
502
|
+
case "second":
|
|
503
|
+
return "––";
|
|
504
|
+
case "dayPeriod":
|
|
505
|
+
return "AM";
|
|
445
506
|
default:
|
|
446
|
-
return
|
|
507
|
+
return "";
|
|
447
508
|
}
|
|
448
509
|
}
|
|
449
510
|
|
|
@@ -451,21 +512,39 @@ function updateTimePart(
|
|
|
451
512
|
time: TimeValue,
|
|
452
513
|
type: TimeSegmentType,
|
|
453
514
|
value: number,
|
|
454
|
-
|
|
515
|
+
_hourCycle: number,
|
|
455
516
|
): TimeValue {
|
|
456
|
-
if (
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
517
|
+
if (!time) return time;
|
|
518
|
+
|
|
519
|
+
// All TimeValue types in @internationalized/date have a .set() method
|
|
520
|
+
const settable = time as { set(props: Record<string, number>): TimeValue };
|
|
521
|
+
if (typeof settable.set !== "function") {
|
|
522
|
+
return time;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
switch (type) {
|
|
526
|
+
case "hour":
|
|
527
|
+
return settable.set({ hour: value });
|
|
528
|
+
case "minute":
|
|
529
|
+
return settable.set({ minute: value });
|
|
530
|
+
case "second":
|
|
531
|
+
return settable.set({ second: value });
|
|
532
|
+
case "dayPeriod": {
|
|
533
|
+
// dayPeriod 0 = AM, 1 = PM. Adjust hour by +/- 12 based on desired period.
|
|
534
|
+
const currentHour = getHour(time);
|
|
535
|
+
const isPM = value === 1;
|
|
536
|
+
const isCurrentlyPM = currentHour >= 12;
|
|
537
|
+
if (isPM && !isCurrentlyPM) {
|
|
538
|
+
return settable.set({ hour: currentHour + 12 });
|
|
539
|
+
}
|
|
540
|
+
if (!isPM && isCurrentlyPM) {
|
|
541
|
+
return settable.set({ hour: currentHour - 12 });
|
|
542
|
+
}
|
|
543
|
+
return time;
|
|
466
544
|
}
|
|
545
|
+
default:
|
|
546
|
+
return time;
|
|
467
547
|
}
|
|
468
|
-
return time;
|
|
469
548
|
}
|
|
470
549
|
|
|
471
550
|
function compareTime(a: TimeValue, b: TimeValue): number {
|
package/src/calendar/index.d.ts
CHANGED
|
@@ -1,7 +1,80 @@
|
|
|
1
|
-
export {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
export {
|
|
2
|
+
createCalendarState,
|
|
3
|
+
type CalendarStateProps,
|
|
4
|
+
type CalendarState,
|
|
5
|
+
type ValidationState,
|
|
6
|
+
type CalendarPageBehavior,
|
|
7
|
+
type CalendarSelectionAlignment,
|
|
8
|
+
type CalendarDayOfWeek,
|
|
9
|
+
} from "./createCalendarState";
|
|
10
|
+
export {
|
|
11
|
+
createRangeCalendarState,
|
|
12
|
+
type RangeCalendarStateProps,
|
|
13
|
+
type RangeCalendarState,
|
|
14
|
+
type DateRange,
|
|
15
|
+
type RangeValue,
|
|
16
|
+
} from "./createRangeCalendarState";
|
|
17
|
+
export {
|
|
18
|
+
createDateFieldState,
|
|
19
|
+
type DateFieldStateProps,
|
|
20
|
+
type DateFieldState,
|
|
21
|
+
type DateSegment,
|
|
22
|
+
type DateSegmentType,
|
|
23
|
+
} from "./createDateFieldState";
|
|
24
|
+
export {
|
|
25
|
+
createTimeFieldState,
|
|
26
|
+
type TimeFieldStateProps,
|
|
27
|
+
type TimeFieldState,
|
|
28
|
+
type TimeSegment,
|
|
29
|
+
type TimeSegmentType,
|
|
30
|
+
type TimeValue,
|
|
31
|
+
} from "./createTimeFieldState";
|
|
32
|
+
export type {
|
|
33
|
+
CalendarDate,
|
|
34
|
+
CalendarDateTime,
|
|
35
|
+
ZonedDateTime,
|
|
36
|
+
DateValue,
|
|
37
|
+
Time,
|
|
38
|
+
Calendar,
|
|
39
|
+
CalendarIdentifier,
|
|
40
|
+
AnyCalendarDate,
|
|
41
|
+
} from "@internationalized/date";
|
|
42
|
+
export {
|
|
43
|
+
createCalendar,
|
|
44
|
+
GregorianCalendar,
|
|
45
|
+
today,
|
|
46
|
+
now,
|
|
47
|
+
getLocalTimeZone,
|
|
48
|
+
parseDate,
|
|
49
|
+
parseDateTime,
|
|
50
|
+
parseTime,
|
|
51
|
+
parseAbsolute,
|
|
52
|
+
parseAbsoluteToLocal,
|
|
53
|
+
parseZonedDateTime,
|
|
54
|
+
toCalendarDate,
|
|
55
|
+
toCalendarDateTime,
|
|
56
|
+
toZoned,
|
|
57
|
+
toTime,
|
|
58
|
+
CalendarDate as CalendarDateClass,
|
|
59
|
+
CalendarDateTime as CalendarDateTimeClass,
|
|
60
|
+
ZonedDateTime as ZonedDateTimeClass,
|
|
61
|
+
Time as TimeClass,
|
|
62
|
+
DateFormatter,
|
|
63
|
+
isSameDay,
|
|
64
|
+
isSameMonth,
|
|
65
|
+
isSameYear,
|
|
66
|
+
isToday,
|
|
67
|
+
isWeekend,
|
|
68
|
+
startOfMonth,
|
|
69
|
+
endOfMonth,
|
|
70
|
+
startOfWeek,
|
|
71
|
+
endOfWeek,
|
|
72
|
+
startOfYear,
|
|
73
|
+
endOfYear,
|
|
74
|
+
getWeeksInMonth,
|
|
75
|
+
getDayOfWeek,
|
|
76
|
+
toCalendar,
|
|
77
|
+
minDate,
|
|
78
|
+
maxDate,
|
|
79
|
+
} from "@internationalized/date";
|
|
80
|
+
//# sourceMappingURL=index.d.ts.map
|
package/src/calendar/index.ts
CHANGED
|
@@ -1,30 +1,29 @@
|
|
|
1
|
-
// Calendar State
|
|
2
1
|
export {
|
|
3
2
|
createCalendarState,
|
|
4
3
|
type CalendarStateProps,
|
|
5
4
|
type CalendarState,
|
|
6
5
|
type ValidationState,
|
|
7
|
-
|
|
6
|
+
type CalendarPageBehavior,
|
|
7
|
+
type CalendarSelectionAlignment,
|
|
8
|
+
type CalendarDayOfWeek,
|
|
9
|
+
} from "./createCalendarState";
|
|
8
10
|
|
|
9
|
-
// Range Calendar State
|
|
10
11
|
export {
|
|
11
12
|
createRangeCalendarState,
|
|
12
13
|
type RangeCalendarStateProps,
|
|
13
14
|
type RangeCalendarState,
|
|
14
15
|
type DateRange,
|
|
15
16
|
type RangeValue,
|
|
16
|
-
} from
|
|
17
|
+
} from "./createRangeCalendarState";
|
|
17
18
|
|
|
18
|
-
// Date Field State
|
|
19
19
|
export {
|
|
20
20
|
createDateFieldState,
|
|
21
21
|
type DateFieldStateProps,
|
|
22
22
|
type DateFieldState,
|
|
23
23
|
type DateSegment,
|
|
24
24
|
type DateSegmentType,
|
|
25
|
-
} from
|
|
25
|
+
} from "./createDateFieldState";
|
|
26
26
|
|
|
27
|
-
// Time Field State
|
|
28
27
|
export {
|
|
29
28
|
createTimeFieldState,
|
|
30
29
|
type TimeFieldStateProps,
|
|
@@ -32,19 +31,28 @@ export {
|
|
|
32
31
|
type TimeSegment,
|
|
33
32
|
type TimeSegmentType,
|
|
34
33
|
type TimeValue,
|
|
35
|
-
} from
|
|
34
|
+
} from "./createTimeFieldState";
|
|
35
|
+
|
|
36
|
+
export {
|
|
37
|
+
createDatePickerState,
|
|
38
|
+
type DatePickerStateOptions,
|
|
39
|
+
type DatePickerState,
|
|
40
|
+
} from "./createDatePickerState";
|
|
36
41
|
|
|
37
|
-
// Re-export date types from @internationalized/date
|
|
38
42
|
export type {
|
|
39
43
|
CalendarDate,
|
|
40
44
|
CalendarDateTime,
|
|
41
45
|
ZonedDateTime,
|
|
42
46
|
DateValue,
|
|
43
47
|
Time,
|
|
44
|
-
|
|
48
|
+
Calendar,
|
|
49
|
+
CalendarIdentifier,
|
|
50
|
+
AnyCalendarDate,
|
|
51
|
+
} from "@internationalized/date";
|
|
45
52
|
|
|
46
|
-
// Re-export commonly used date utilities
|
|
47
53
|
export {
|
|
54
|
+
createCalendar,
|
|
55
|
+
GregorianCalendar,
|
|
48
56
|
today,
|
|
49
57
|
now,
|
|
50
58
|
getLocalTimeZone,
|
|
@@ -76,6 +84,7 @@ export {
|
|
|
76
84
|
endOfYear,
|
|
77
85
|
getWeeksInMonth,
|
|
78
86
|
getDayOfWeek,
|
|
87
|
+
toCalendar,
|
|
79
88
|
minDate,
|
|
80
89
|
maxDate,
|
|
81
|
-
} from
|
|
90
|
+
} from "@internationalized/date";
|