@techsio/ui-kit 0.44.0 → 0.44.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/internal/date-picker-coordinator.js +166 -0
- package/dist/molecules/date-picker.js +1374 -0
- package/dist/src/internal/date-picker-coordinator.d.ts +42 -0
- package/dist/src/internal/date-picker-coordinator.d.ts.map +1 -0
- package/dist/src/molecules/date-picker.d.ts +159 -0
- package/dist/src/molecules/date-picker.d.ts.map +1 -0
- package/dist/stories/changelog/changelog.stories.d.ts.map +1 -1
- package/dist/stories/molecules/date-picker.stories.d.ts +141 -0
- package/dist/stories/molecules/date-picker.stories.d.ts.map +1 -0
- package/dist/test/date-picker.behavior.spec.d.ts +2 -0
- package/dist/test/date-picker.behavior.spec.d.ts.map +1 -0
- package/package.json +10 -3
- package/src/tokens/components/components.css +1 -0
- package/src/tokens/components/molecules/_date-picker.css +225 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { Time, toCalendarDateTime } from "@internationalized/date";
|
|
2
|
+
function createDatePickerCoordinatorState() {
|
|
3
|
+
return {
|
|
4
|
+
draft: null
|
|
5
|
+
};
|
|
6
|
+
}
|
|
7
|
+
function datePickerCoordinatorReducer(state, action) {
|
|
8
|
+
if ("open" === action.type || "resync" === action.type) return {
|
|
9
|
+
draft: {
|
|
10
|
+
baseline: action.value,
|
|
11
|
+
candidate: action.value
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
if ("edit" === action.type && state.draft) return {
|
|
15
|
+
draft: {
|
|
16
|
+
...state.draft,
|
|
17
|
+
candidate: action.value,
|
|
18
|
+
timeParts: null === action.value ? void 0 : state.draft.timeParts
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
if ("edit-time" === action.type && state.draft?.candidate) {
|
|
22
|
+
const index = action.index ?? 0;
|
|
23
|
+
const hourCycle = action.hourCycle ?? 24;
|
|
24
|
+
const timeParts = {
|
|
25
|
+
...getDatePickerDraftTimeParts(state, hourCycle, index),
|
|
26
|
+
[action.part]: action.value
|
|
27
|
+
};
|
|
28
|
+
const requiredParts = getRequiredTimeParts(action.granularity, hourCycle);
|
|
29
|
+
const isComplete = requiredParts.every((part)=>"number" == typeof timeParts[part]);
|
|
30
|
+
let candidate = state.draft.candidate;
|
|
31
|
+
const endpoint = getCandidateAt(candidate, index);
|
|
32
|
+
if (isComplete && endpoint) {
|
|
33
|
+
const time = {
|
|
34
|
+
hour: 12 === hourCycle ? (timeParts.hour ?? 12) % 12 + (timeParts.dayPeriod ?? 0) : timeParts.hour ?? 0,
|
|
35
|
+
minute: timeParts.minute ?? 0,
|
|
36
|
+
second: timeParts.second ?? 0
|
|
37
|
+
};
|
|
38
|
+
const timedEndpoint = isTimedDateValue(endpoint) ? endpoint.set(time) : toCalendarDateTime(endpoint, new Time(time.hour, time.minute, time.second));
|
|
39
|
+
candidate = setCandidateAt(candidate, index, timedEndpoint);
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
draft: {
|
|
43
|
+
...state.draft,
|
|
44
|
+
candidate,
|
|
45
|
+
timeGranularity: action.granularity,
|
|
46
|
+
timeHourCycle: hourCycle,
|
|
47
|
+
timeParts: {
|
|
48
|
+
...state.draft.timeParts,
|
|
49
|
+
[index]: timeParts
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
if ("discard" === action.type) return createDatePickerCoordinatorState();
|
|
55
|
+
return state;
|
|
56
|
+
}
|
|
57
|
+
function getCandidateAt(candidate, index) {
|
|
58
|
+
if (isCandidateArray(candidate)) return candidate[index];
|
|
59
|
+
return 0 === index ? candidate : void 0;
|
|
60
|
+
}
|
|
61
|
+
function setCandidateAt(candidate, index, value) {
|
|
62
|
+
if (!isCandidateArray(candidate)) return 0 === index ? value : candidate;
|
|
63
|
+
const nextCandidate = [
|
|
64
|
+
...candidate
|
|
65
|
+
];
|
|
66
|
+
nextCandidate[index] = value;
|
|
67
|
+
return nextCandidate;
|
|
68
|
+
}
|
|
69
|
+
function candidateValues(candidate) {
|
|
70
|
+
if (isCandidateArray(candidate)) return candidate;
|
|
71
|
+
return candidate ? [
|
|
72
|
+
candidate
|
|
73
|
+
] : [];
|
|
74
|
+
}
|
|
75
|
+
function isCandidateArray(candidate) {
|
|
76
|
+
return Array.isArray(candidate);
|
|
77
|
+
}
|
|
78
|
+
function dateValuesEqual(left, right) {
|
|
79
|
+
const leftValues = candidateValues(left);
|
|
80
|
+
const rightValues = candidateValues(right);
|
|
81
|
+
return leftValues.length === rightValues.length && leftValues.every((value, index)=>value.toString() === rightValues[index]?.toString());
|
|
82
|
+
}
|
|
83
|
+
function isDatePickerDraftDirty(state) {
|
|
84
|
+
if (!state.draft) return false;
|
|
85
|
+
return !dateValuesEqual(state.draft.baseline, state.draft.candidate) || areDraftTimePartsDirty(state.draft);
|
|
86
|
+
}
|
|
87
|
+
function canConfirmDatePickerDraft(state, isTimeUnavailable) {
|
|
88
|
+
if (!isDatePickerDraftDirty(state)) return false;
|
|
89
|
+
const candidate = state.draft?.candidate;
|
|
90
|
+
if (null === candidate) return true;
|
|
91
|
+
const values = candidateValues(candidate);
|
|
92
|
+
if (isCandidateArray(candidate) && 2 !== values.length) return false;
|
|
93
|
+
const [startValue, endValue] = values;
|
|
94
|
+
if (startValue && endValue && startValue.compare(endValue) > 0) return false;
|
|
95
|
+
const timedValues = values.filter(isTimedDateValue);
|
|
96
|
+
if (timedValues.length !== values.length) return false;
|
|
97
|
+
const firstTimedValue = timedValues[0];
|
|
98
|
+
const secondTimedValue = timedValues[1];
|
|
99
|
+
if (firstTimedValue && secondTimedValue && "timeZone" in firstTimedValue !== "timeZone" in secondTimedValue) return false;
|
|
100
|
+
return timedValues.every((value, index)=>{
|
|
101
|
+
if (index > 1) return false;
|
|
102
|
+
const candidateIndex = 0 === index ? 0 : 1;
|
|
103
|
+
if (state.draft?.timeGranularity && !getRequiredTimeParts(state.draft.timeGranularity, state.draft.timeHourCycle).every((part)=>"number" == typeof getDatePickerDraftTimeParts(state, state.draft?.timeHourCycle, candidateIndex)[part])) return false;
|
|
104
|
+
return !(isTimeUnavailable?.(value, candidateIndex) ?? false);
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
function isTimedDateValue(value) {
|
|
108
|
+
return null != value && "hour" in value;
|
|
109
|
+
}
|
|
110
|
+
function getDatePickerDraftTimeParts(state, hourCycle = 24, index = 0) {
|
|
111
|
+
const candidate = getCandidateAt(state.draft?.candidate, index);
|
|
112
|
+
const candidateParts = isTimedDateValue(candidate) ? {
|
|
113
|
+
dayPeriod: 12 === hourCycle ? candidate.hour >= 12 ? 12 : 0 : void 0,
|
|
114
|
+
hour: 12 === hourCycle ? candidate.hour % 12 || 12 : candidate.hour,
|
|
115
|
+
minute: candidate.minute,
|
|
116
|
+
second: candidate.second
|
|
117
|
+
} : {};
|
|
118
|
+
return {
|
|
119
|
+
...candidateParts,
|
|
120
|
+
...state.draft?.timeParts?.[index]
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
function getRequiredTimeParts(granularity, hourCycle = 24) {
|
|
124
|
+
const hourParts = 12 === hourCycle ? [
|
|
125
|
+
"hour",
|
|
126
|
+
"dayPeriod"
|
|
127
|
+
] : [
|
|
128
|
+
"hour"
|
|
129
|
+
];
|
|
130
|
+
if ("hour" === granularity) return hourParts;
|
|
131
|
+
if ("minute" === granularity) return [
|
|
132
|
+
...hourParts,
|
|
133
|
+
"minute"
|
|
134
|
+
];
|
|
135
|
+
return [
|
|
136
|
+
...hourParts,
|
|
137
|
+
"minute",
|
|
138
|
+
"second"
|
|
139
|
+
];
|
|
140
|
+
}
|
|
141
|
+
function areDraftTimePartsDirty(draft) {
|
|
142
|
+
if (!draft.timeParts) return false;
|
|
143
|
+
const indices = [
|
|
144
|
+
0,
|
|
145
|
+
1
|
|
146
|
+
];
|
|
147
|
+
const parts = [
|
|
148
|
+
"hour",
|
|
149
|
+
"minute",
|
|
150
|
+
"second",
|
|
151
|
+
"dayPeriod"
|
|
152
|
+
];
|
|
153
|
+
return indices.some((index)=>{
|
|
154
|
+
const timeParts = draft.timeParts?.[index];
|
|
155
|
+
if (!timeParts) return false;
|
|
156
|
+
const baseline = getCandidateAt(draft.baseline, index);
|
|
157
|
+
const baselineParts = isTimedDateValue(baseline) ? {
|
|
158
|
+
dayPeriod: 12 === draft.timeHourCycle ? baseline.hour >= 12 ? 12 : 0 : void 0,
|
|
159
|
+
hour: 12 === draft.timeHourCycle ? baseline.hour % 12 || 12 : baseline.hour,
|
|
160
|
+
minute: baseline.minute,
|
|
161
|
+
second: baseline.second
|
|
162
|
+
} : {};
|
|
163
|
+
return parts.some((part)=>Object.hasOwn(timeParts, part) && baselineParts[part] !== timeParts[part]);
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
export { canConfirmDatePickerDraft, createDatePickerCoordinatorState, datePickerCoordinatorReducer, getDatePickerDraftTimeParts, isDatePickerDraftDirty, isTimedDateValue };
|