Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(core): adjustTime emits "14:30.5" for a fractional increment, on both the public util and TimeInput/DateTimeInput (#7823)

Aashish Mahato677d96c8b267
diff --git a/.changeset/adjusttime-returns-the-time-unchanged-for-a-frac-7bdh.md b/.changeset/adjusttime-returns-the-time-unchanged-for-a-frac-7bdh.md
new file mode 100644
index 0000000000..1a4df05ce0
--- /dev/null
+++ b/.changeset/adjusttime-returns-the-time-unchanged-for-a-frac-7bdh.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] adjustTime returns the time unchanged for a fractional minute delta instead of a malformed value such as "14:30.5", so TimeInput's arrow keys ignore a fractional increment (#7823)
+@aashish254
diff --git a/packages/core/src/TimeInput/TimeInput.doc.mjs b/packages/core/src/TimeInput/TimeInput.doc.mjs
index 8c14cf20ac..5622fa0623 100644
--- a/packages/core/src/TimeInput/TimeInput.doc.mjs
+++ b/packages/core/src/TimeInput/TimeInput.doc.mjs
@@ -212,7 +212,7 @@ export const docs = {
name: 'increment',
type: 'number',
description:
- 'Number of minutes to add or subtract when the user presses the up or down arrow key.',
+ 'Number of minutes to add or subtract when the user presses the up or down arrow key. Steps are whole minutes; a fractional value leaves the time unchanged.',
default: '1',
},
{
@@ -392,7 +392,8 @@ export const docsZh = {
{
name: 'increment',
type: 'number',
- description: '用户按上或下方向键时增加或减少的分钟数。',
+ description:
+ '用户按上或下方向键时增加或减少的分钟数。步长为整分钟;传入小数时时间保持不变。',
default: '1',
},
{
@@ -623,7 +624,8 @@ export const docsDense = {
hasClear: 'Shows clear button when value set+not disabled.',
hourFormat:
"Display format. '12h' shows AM/PM; '24h' uses 24-hour notation.",
- increment: 'Minutes to add/subtract on arrow up/down.',
+ increment:
+ 'Minutes to add/subtract on arrow up/down. Whole minutes only; a fractional value leaves the time unchanged.',
presentation:
"picker surface: 'adaptive-native' (default) = typed field fine / browser/OS coarse (hasSeconds or increment!=1 retains typed); 'native' = browser/OS always, no fallback; 'adaptive-bottom-sheet' = typed fine / Astryx wheel sheet coarse; 'bottom-sheet' = sheet always; 'text-input'/'popover' = typed field only.",
nativePicker:
diff --git a/packages/core/src/TimeInput/TimeInput.test.tsx b/packages/core/src/TimeInput/TimeInput.test.tsx
index ab6af605a5..99449d21c7 100644
--- a/packages/core/src/TimeInput/TimeInput.test.tsx
+++ b/packages/core/src/TimeInput/TimeInput.test.tsx
@@ -65,6 +65,26 @@ describe('TimeInput', () => {
expect(onChange).toHaveBeenCalledWith('14:31');
});
+ it('does not step into a fractional minute when increment is fractional', () => {
+ const onChange = vi.fn();
+ render(
+ <TimeInput
+ label="Time"
+ value={'14:30' as ISOTimeString}
+ increment={0.5}
+ onChange={onChange}
+ />,
+ );
+ const input = screen.getByLabelText('Time');
+
+ // `increment` is documented as minutes and typed `number`. A fractional one
+ // used to reach formatISOTime unrounded, which pads "30.5" to two
+ // characters and leaves it, so the arrow key handed the consumer a value
+ // that is no longer a time at all.
+ fireEvent.keyDown(input, {key: 'ArrowUp'});
+ expect(onChange).toHaveBeenCalledWith('14:30');
+ });
+
it('displays formatted time in 12h format', () => {
render(
<TimeInput
diff --git a/packages/core/src/TimeInput/TimeInput.tsx b/packages/core/src/TimeInput/TimeInput.tsx
index 5062a7ea3f..45968422f8 100644
--- a/packages/core/src/TimeInput/TimeInput.tsx
+++ b/packages/core/src/TimeInput/TimeInput.tsx
@@ -286,7 +286,8 @@ export interface TimeInputProps extends Omit<
hourFormat?: TimeInputHourFormat;
/**
- * Increment in minutes when using arrow keys.
+ * Increment in minutes when using arrow keys. Steps are whole minutes: a
+ * fractional value leaves the time unchanged.
* @default 1
*/
increment?: number;
diff --git a/packages/core/src/utils/timeParser.test.ts b/packages/core/src/utils/timeParser.test.ts
index 64d61808a8..56cda4d3fa 100644
--- a/packages/core/src/utils/timeParser.test.ts
+++ b/packages/core/src/utils/timeParser.test.ts
@@ -260,6 +260,17 @@ describe('adjustTime', () => {
expect(adjustTime('10:00' as ISOTimeString, NaN)).toBe('10:00');
});
+ it('returns the input unchanged for a fractional delta', () => {
+ // `increment` is documented as minutes and typed `number`, so a caller can
+ // hand ArrowUp a fraction. A fractional minute reaching formatISOTime is
+ // not rounded: it pads to "30.5" and the field is left holding a string
+ // that is no longer a parseable time.
+ expect(adjustTime('10:30' as ISOTimeString, 0.5)).toBe('10:30');
+ expect(adjustTime('10:30:45' as ISOTimeString, -0.5, true)).toBe(
+ '10:30:45',
+ );
+ });
+
it('wraps large negative deltas in constant time', () => {
expect(adjustTime('10:00' as ISOTimeString, -1440 * 1e7)).toBe('10:00');
expect(adjustTime('10:00' as ISOTimeString, -1440 * 1e7 - 30)).toBe(
diff --git a/packages/core/src/utils/timeParser.ts b/packages/core/src/utils/timeParser.ts
index f8803c5c95..b52f2ddc59 100644
--- a/packages/core/src/utils/timeParser.ts
+++ b/packages/core/src/utils/timeParser.ts
@@ -372,6 +372,10 @@ export function clampTime(
/**
* Adjusts a time by adding/subtracting minutes.
+ *
+ * `deltaMinutes` is a whole number of minutes. A fractional delta leaves the
+ * time unchanged rather than returning a value that is no longer a time —
+ * `HH:MM` has no place for a fraction of a minute.
*/
export function adjustTime(
time: ISOTimeString,
@@ -384,8 +388,11 @@ export function adjustTime(
}
// A non-finite delta would spin the wrap-around forever (-Infinity) or
- // produce "NaN:NaN" (NaN) — return the input unchanged instead.
- if (!Number.isFinite(deltaMinutes)) {
+ // produce "NaN:NaN" (NaN), and a fractional one is never rounded on the way
+ // out — formatISOTime pads "30.5" to two characters and leaves it, so the
+ // caller gets back a value that is no longer a time. Steps are whole
+ // minutes: return the input unchanged instead.
+ if (!Number.isInteger(deltaMinutes)) {
return time;
}