@human-kit/ui 1.3.0 → 1.4.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.
@@ -60,7 +60,15 @@ export type TimeSelectionState = {
60
60
  */
61
61
  readonly isInvalidDraft: boolean;
62
62
  getSegmentValue: (type: TimePickerEditableSegmentType) => string;
63
- setSegmentValue: (type: TimePickerEditableSegmentType, nextValue: string) => void;
63
+ /**
64
+ * Writes one segment and commits. The draft is held inside min/max unless `clamp` is
65
+ * `false`: a first digit still waiting for its pair is not a time yet.
66
+ */
67
+ setSegmentValue: (type: TimePickerEditableSegmentType, nextValue: string, options?: {
68
+ clamp?: boolean;
69
+ }) => void;
70
+ /** Holds the draft inside min/max and commits it: a pending digit that was left alone. */
71
+ clampDraft: () => void;
64
72
  /** Re-commits the current draft (publishes `null` when it is not committeable). */
65
73
  commitDraft: () => void;
66
74
  /**
@@ -166,12 +166,44 @@ export function createTimeSelectionState(options) {
166
166
  return segmentDraft.second;
167
167
  return segmentDraft.dayPeriod;
168
168
  }
169
- function setSegmentValue(type, nextValue) {
169
+ function setSegmentValue(type, nextValue, { clamp = true } = {}) {
170
170
  if (!options.isEditable())
171
171
  return;
172
172
  writeSegmentValue(type, nextValue);
173
+ if (clamp)
174
+ clampDraftToRange();
173
175
  commitFromDraft();
174
176
  }
177
+ function clampDraft() {
178
+ if (!options.isEditable())
179
+ return;
180
+ clampDraftToRange();
181
+ commitFromDraft();
182
+ }
183
+ /**
184
+ * Holds a typed or stepped draft inside `minValue`/`maxValue`, the way React Aria's
185
+ * segments hold a number inside its bounds: a complete time past the max becomes the
186
+ * max, one before the min becomes the min, at once rather than on blur. A first digit
187
+ * that waits for its pair is skipped (`1` on the way to `19` under a `10:00` min), and
188
+ * held once it is completed or left alone.
189
+ */
190
+ function clampDraftToRange() {
191
+ const granularity = options.granularity();
192
+ const parts = buildTimePartsFromDraft(segmentDraft, granularity, resolvedHourCycle);
193
+ if (!parts)
194
+ return;
195
+ const candidate = formatTimePickerValue(parts, granularity);
196
+ let boundary;
197
+ if (isTimeOutOfRange(candidate, undefined, normalizedMaxValue, granularity)) {
198
+ boundary = normalizedMaxValue;
199
+ }
200
+ else if (isTimeOutOfRange(candidate, normalizedMinValue, undefined, granularity)) {
201
+ boundary = normalizedMinValue;
202
+ }
203
+ if (!boundary)
204
+ return;
205
+ segmentDraft = toDraftFromTimeValue(boundary, resolvedHourCycle);
206
+ }
175
207
  /** Normalizes and writes a segment into the draft without committing it. */
176
208
  function writeSegmentValue(type, nextValue) {
177
209
  options.onBeforeSegmentMutation?.(type);
@@ -385,6 +417,7 @@ export function createTimeSelectionState(options) {
385
417
  },
386
418
  getSegmentValue,
387
419
  setSegmentValue,
420
+ clampDraft,
388
421
  commitDraft: commitFromDraft,
389
422
  setValue,
390
423
  selectWheelValue,
@@ -28,7 +28,9 @@ export type TimePickerContext = {
28
28
  setValue: (nextValue: TimePickerTimeValue | null) => void;
29
29
  getSegments: () => TimePickerSegmentPart[];
30
30
  getSegmentValue: (type: Exclude<TimePickerSegmentType, 'literal'>) => string;
31
- setSegmentValue: (type: Exclude<TimePickerSegmentType, 'literal'>, nextValue: string) => void;
31
+ setSegmentValue: (type: Exclude<TimePickerSegmentType, 'literal'>, nextValue: string, options?: {
32
+ clamp?: boolean;
33
+ }) => void;
32
34
  typeSegmentDigit: (type: Exclude<TimePickerSegmentType, 'literal'>, digit: string) => boolean;
33
35
  adjustSegmentValue: (type: Exclude<TimePickerSegmentType, 'literal'>, step: number) => void;
34
36
  getSegmentLabel: (type: TimePickerEditableSegmentType) => string;
@@ -197,6 +197,7 @@
197
197
  if (!nextWithin && activeSegment !== null) {
198
198
  activeSegment = null;
199
199
  segmentTypeBuffer = createEmptyTimePickerDraft();
200
+ timeSelection.clampDraft();
200
201
  }
201
202
  if (focusWithin === nextWithin) return;
202
203
  focusWithin = nextWithin;
@@ -206,6 +207,8 @@
206
207
  if (activeSegment === segment) return;
207
208
  activeSegment = segment;
208
209
  segmentTypeBuffer = createEmptyTimePickerDraft();
210
+ // A digit left waiting, or one erased, is held to min/max once the segment is left.
211
+ timeSelection.clampDraft();
209
212
  }
210
213
 
211
214
  function openPopover(reason: TimePickerOpenChangeReason = 'imperative-action', event?: Event) {
@@ -246,14 +249,11 @@
246
249
 
247
250
  const threshold = getTypingThreshold(type);
248
251
  if (candidate.length === 1) {
249
- timeSelection.setSegmentValue(type, candidate);
250
- segmentTypeBuffer[type] = candidate;
251
- const numeric = Number(candidate);
252
- if (Number.isFinite(numeric) && numeric >= threshold) {
253
- segmentTypeBuffer[type] = '';
254
- return true;
255
- }
256
- return false;
252
+ // A digit that waits for its pair is not held to min/max yet: `1` on the way to `19`.
253
+ const completes = Number(candidate) >= threshold;
254
+ timeSelection.setSegmentValue(type, candidate, { clamp: completes });
255
+ segmentTypeBuffer[type] = completes ? '' : candidate;
256
+ return completes;
257
257
  }
258
258
 
259
259
  const numericCandidate = Number(candidate);
@@ -271,14 +271,10 @@
271
271
 
272
272
  if (numericCandidate < min || numericCandidate > max) {
273
273
  candidate = digit;
274
- timeSelection.setSegmentValue(type, candidate);
275
- segmentTypeBuffer[type] = candidate;
276
- const fallbackNumeric = Number(candidate);
277
- if (fallbackNumeric >= threshold) {
278
- segmentTypeBuffer[type] = '';
279
- return true;
280
- }
281
- return false;
274
+ const completes = Number(candidate) >= threshold;
275
+ timeSelection.setSegmentValue(type, candidate, { clamp: completes });
276
+ segmentTypeBuffer[type] = completes ? '' : candidate;
277
+ return completes;
282
278
  }
283
279
 
284
280
  timeSelection.setSegmentValue(type, candidate);
@@ -308,7 +308,8 @@
308
308
  }
309
309
  return;
310
310
  }
311
- timePicker.setSegmentValue(segment.type, currentValue.slice(0, -1));
311
+ // Erasing is on the way to typing: held to min/max once the segment is left.
312
+ timePicker.setSegmentValue(segment.type, currentValue.slice(0, -1), { clamp: false });
312
313
  return;
313
314
  }
314
315
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@human-kit/ui",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "Accessible, typed UI components for Svelte 5 — shipped as native ESM with per-component subpath exports so bundlers only include what you import.",
5
5
  "license": "MIT",
6
6
  "author": "Agustin Delgado <agustindariodelgado@gmail.com>",