Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/clamp-time-whole-minute.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': patch
---

[fix] `clampTime` without seconds now gives the nearest whole minute inside a bound that falls between minutes. Before, `clampTime('08:00', '09:00:30')` gave `09:00`, a time outside the range.

@vjeux
12 changes: 12 additions & 0 deletions .changeset/time-wheels-follow-uidatepicker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@astryxdesign/core': patch
---

[fix] Under a finger, the time wheels in the TimeInput and DateTimeInput bottom sheets now follow iOS's date picker:

- The AM/PM column follows the hour. Turning the hour from 11 to 12 now gives 12 PM, not 12 AM, and the AM/PM column turns onto PM. From 12 PM to 1 it stays PM.
- A bound that falls between whole minutes, such as `min="09:00:30"` without a seconds column, now commits the nearest whole minute inside it, with a finger as with a mouse.

With a mouse, each column still turns on its own.

@vjeux
7 changes: 7 additions & 0 deletions .changeset/wheel-spin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': patch
---

[chore] Internal: the picker wheel turns from script, for the time columns that use it. Under a finger, `spinTo` on the wheel's ref turns it onto a row on the tap spin, then commits it as a rest would; under a fine pointer it commits the row at once. Either way it reports whether that row was committed: it refuses a disabled row and an index outside the rows, and a turn the wheel's unmounting cuts short reports that it did not arrive. Nothing public changes: the wheel is not exported from the package entry point.

@vjeux
17 changes: 17 additions & 0 deletions apps/storybook/stories/TimeInput.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,23 @@ export const BottomSheetWithMinMax: Story = {
},
};

/**
* The bottom-sheet wheels as iOS's date picker turns them: from 11:58 AM, one
* row down on the hour gives 12:58 PM and the AM/PM column turns with it.
*/
export const BottomSheetWheels: Story = {
render: args => {
const [value, setValue] = useState<ISOTimeString | undefined>(
'11:58' as ISOTimeString,
);
return <TimeInput {...args} value={value} onChange={setValue} />;
},
args: {
label: 'Alarm',
presentation: 'bottom-sheet',
},
};

export const WithIncrement: Story = {
render: args => {
const [value, setValue] = useState<ISOTimeString | undefined>(
Expand Down
55 changes: 28 additions & 27 deletions docs/specs/components/TimeInput/TimeInput.spec.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -1110,10 +1110,8 @@
"spec:AST-086"
],
"packages/core/src/TimeInput/TouchTimeField.tsx": [
"DEC-3",
"FR2",
"FR8",
"spec:AST-043",
"spec:AST-086"
],
"packages/core/src/Timestamp/Timestamp.tsx": [
Expand Down
109 changes: 108 additions & 1 deletion packages/core/src/DateInput/PickerWheel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/

import {act, fireEvent, render, screen} from '@testing-library/react';
import {Profiler, useState} from 'react';
import {Profiler, createRef, useState} from 'react';
import {
afterAll,
afterEach,
Expand All @@ -31,6 +31,7 @@ import {fontWeightVars} from '../theme/tokens.stylex';
import {PickerWheel} from './PickerWheel';
import {
WHEEL_ROW_HEIGHT,
type WheelHandle,
type WheelOption,
type WheelProps,
} from './wheelOptions';
Expand Down Expand Up @@ -1133,6 +1134,21 @@ describe('PickerWheel — under reduced motion', () => {
expect(onChange).toHaveBeenLastCalledWith(9);
});

it('fades for a spin', async () => {
const ref = createRef<WheelHandle>();
const {onChange} = renderWheel({ref});
let arrived: boolean | undefined;
act(() => {
void ref.current!.spinTo(15).then(value => {
arrived = value;
});
});
await advanceAsync(FADE_MS);
expect(arrived).toBe(true);
expect(onChange).toHaveBeenLastCalledWith(15);
expectFadesOnly();
});

it('fades for a tap and a key', async () => {
const {column, onChange} = renderWheel();
const below = CENTRE + ROW;
Expand All @@ -1150,6 +1166,97 @@ describe('PickerWheel — under reduced motion', () => {
});

describe('PickerWheel — mechanics for its owners', () => {
it('spins to a row on request and commits it there', async () => {
const ref = createRef<WheelHandle>();
const {onChange} = renderWheel({ref});
let arrived: boolean | undefined;
act(() => {
void ref.current!.spinTo(15).then(value => {
arrived = value;
});
});
advance(310);
await act(async () => {});
expect(arrived).toBe(true);
expect(onChange).toHaveBeenCalledWith(15);
});

it('turns nothing and reports false for a spin onto a disabled row or outside the rows', async () => {
// As the scrolling column answers.
const ref = createRef<WheelHandle>();
const {option, onChange} = renderWheel({ref});
for (const index of [3, 24, -1]) {
let arrived: boolean | undefined;
act(() => {
void ref.current!.spinTo(index).then(value => {
arrived = value;
});
});
await act(async () => {});
expect(arrived, `spinTo(${index})`).toBe(false);
}
advance(1000);
expect(onChange).not.toHaveBeenCalled();
expect(distanceOf(option('10'))).toBeCloseTo(0, 4);
});

it('reports false for a spin still turning when the wheel goes away', async () => {
const ref = createRef<WheelHandle>();
const {unmount} = renderWheel({ref});
const spin = ref.current!.spinTo(15);
advance(100);
unmount();
await expect(spin).resolves.toBe(false);
});

it('reports a spin a finger interrupted', async () => {
const ref = createRef<WheelHandle>();
const {column} = renderWheel({ref});
let arrived: boolean | undefined;
act(() => {
void ref.current!.spinTo(15).then(value => {
arrived = value;
});
});
advance(100);
touch('touchstart', column, 100);
await act(async () => {});
expect(arrived).toBe(false);
});

it('keeps a spin going when its owner re-renders the rows mid-turn', async () => {
const ref = createRef<WheelHandle>();
const {rerender, onChange} = renderWheel({
ref,
options: MINUTES,
value: 15,
label: 'Minute',
});
let arrived: boolean | undefined;
act(() => {
void ref.current!.spinTo(30).then(value => {
arrived = value;
});
});
advance(100);
// The owner commits a time on its bound: the minutes before 30 are
// disabled now. The same row nodes come back, so the turn goes on.
rerender(
<PickerWheel
ref={ref}
label="Minute"
options={MINUTES.map(option => ({
...option,
isDisabled: option.value < 30,
}))}
value={30}
onChange={onChange}
/>,
);
advance(250);
await act(async () => {});
expect(arrived).toBe(true);
});
it('keeps a finger that lands on it from an enclosing sheet', () => {
// A sheet arms its pull-down on its body's pointerdown.
const onPointerDown = vi.fn();
Expand Down
39 changes: 37 additions & 2 deletions packages/core/src/DateInput/PickerWheel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

/**
* @file PickerWheel.tsx
* @input Wheel's props (wheelOptions.ts): options, value and change callback
* @input Wheel's props (wheelOptions.ts): options, value and change callback;
* optionally a ref for programmatic turns
* @output PickerWheel: one picker column that moves like UIPickerView
* @position Internal component; the Wheel under a finger (Wheel.tsx picks it)
*
Expand All @@ -15,7 +16,14 @@
* DateInputTouch.test.tsx, DateInput.doc.mjs and TimeInput.doc.mjs.
*/

import {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import {
useCallback,
useEffect,
useImperativeHandle,
useMemo,
useRef,
useState,
} from 'react';
import * as stylex from '@stylexjs/stylex';
import {scrollTypeOf} from '../gestures/GestureHost';
import {PanGestureRecognizer} from '../gestures/PanGestureRecognizer';
Expand Down Expand Up @@ -50,6 +58,7 @@ import {
type WheelLayout,
} from './wheelPhysics';
import {
canSpinTo,
endIndex,
nearestEnabledIndex,
steppedIndex,
Expand Down Expand Up @@ -197,6 +206,7 @@ export function PickerWheel({
value,
onChange,
isActive = true,
ref,
}: WheelProps) {
const columnRef = useRef<HTMLDivElement | null>(null);
const rowsRef = useRef<HTMLDivElement | null>(null);
Expand Down Expand Up @@ -584,6 +594,31 @@ export function PickerWheel({
isPointerKept: true,
});

// A spin turns only onto an enabled row (canSpinTo). So its arrival commits
// that row, and nothing turns on past it. It reports the row it was asked
// for, as the scrolling column does.
Comment on lines +597 to +599

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: spinTo resolves a boolean, not a row. Suggested: "It resolves true only when it arrives on that row, as the scrolling column does."

Comment on lines +597 to +599

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional (comment accuracy): spinTo resolves a boolean, not a row. Suggested:

  // A spin turns only onto an enabled row (canSpinTo). So its arrival commits
  // that row, and nothing turns on past it. It resolves true only when it
  // arrives on that row, as the scrolling column does.

const spinTo = useCallback(
async (index: number): Promise<boolean> =>
new Promise(resolve => {
if (!canSpinTo(latestRef.current.options, index)) {
resolve(false);
return;
}
const offset = motion.offset();
move(turnCurve(columnRef.current, offset, index * ROW_HEIGHT), {
onEnd: (outcome, end) => {
if (outcome === 'arrived') {
arrive(end);
}
resolve(outcome === 'arrived');
},
});
}),
[motion, move, arrive],
);

useImperativeHandle(ref, () => ({spinTo}), [spinTo]);

const turnToPosition = useCallback(
(position: number, offset: number) => {
const current = latestRef.current;
Expand Down
25 changes: 22 additions & 3 deletions packages/core/src/DateInput/Wheel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
* touch suites
*/

import {useState} from 'react';
import {createRef, useState} from 'react';
import {act, fireEvent, render, screen} from '@testing-library/react';
import {afterEach, beforeAll, describe, expect, it, vi} from 'vitest';
import {fakeAnimate} from '../motion/__tests__/fakeWebAnimation';
import {
nearestEnabledIndex,
ScrollWheel,
Wheel,
type WheelHandle,
type WheelOption,
} from './Wheel';
import {SCROLL_QUIET_MS} from './useScrollSettle';
Expand Down Expand Up @@ -650,11 +651,18 @@ describe('Wheel — by pointer', () => {
}

function renderPicker() {
const ref = createRef<WheelHandle>();
const onChange = vi.fn();
const view = render(
<Wheel label="Hour" options={HOURS} value={12} onChange={onChange} />,
<Wheel
ref={ref}
label="Hour"
options={HOURS}
value={12}
onChange={onChange}
/>,
);
return {...view, onChange};
return {...view, ref, onChange};
}

it.each([
Expand Down Expand Up @@ -682,6 +690,17 @@ describe('Wheel — by pointer', () => {
screen.getByRole('listbox', {name: 'Hour'}).parentElement?.parentElement,
).toBe(container);
});

it('commits a spin at once under a fine pointer', async () => {
stubDevice({ios: false, coarse: false});
expect(window.matchMedia('(pointer: coarse)').matches).toBe(false);
const {ref, onChange} = renderPicker();
await expect(ref.current?.spinTo(14)).resolves.toBe(true);
expect(onChange).toHaveBeenCalledWith(14);
// A disabled row is not a place to rest.
await expect(ref.current?.spinTo(3)).resolves.toBe(false);
expect(onChange).toHaveBeenCalledTimes(1);
});
});

describe('Wheel — markup', () => {
Expand Down
32 changes: 30 additions & 2 deletions packages/core/src/DateInput/Wheel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

/**
* @file Wheel.tsx
* @input Option list, selected value and change callback
* @input Option list, selected value and change callback; a ref for
* programmatic turns
* @output Exports Wheel (one picker column), ScrollWheel (the scrolling
* column) and the shared types of wheelOptions.ts
* @position Internal component; consumed by MonthYearWheels.tsx,
Expand All @@ -27,7 +28,13 @@
* - /packages/core/src/DateInput/DateInputTouch.test.tsx
*/

import {useCallback, useEffect, useRef, useState} from 'react';
import {
useCallback,
useEffect,
useImperativeHandle,
useRef,
useState,
} from 'react';
import * as stylex from '@stylexjs/stylex';
import {
colorVars,
Expand All @@ -51,6 +58,7 @@ import {usePointerDragScroll} from './usePointerDragScroll';
import {useScrollSettle} from './useScrollSettle';
import {useWheelListbox} from './useWheelListbox';
import {
canSpinTo,
endIndex,
nearestEnabledIndex,
steppedIndex,
Expand All @@ -59,6 +67,7 @@ import {

export {
nearestEnabledIndex,
type WheelHandle,
type WheelOption,
type WheelProps,
} from './wheelOptions';
Expand Down Expand Up @@ -299,7 +308,26 @@ export function ScrollWheel({
value,
onChange,
isActive = true,
ref,
}: WheelProps) {
// A spin commits the row at once. The new value then scrolls the column
// there, as any new value does.
useImperativeHandle(
ref,
() => ({
spinTo: async index => {
if (!canSpinTo(options, index)) {
return false;
}
const option = options[index];
if (option.value !== value) {
onChange(option.value);
}
return true;
},
}),
[options, value, onChange],
);
const scrollerRef = useRef<HTMLDivElement | null>(null);
const selectedIndex = Math.max(
0,
Expand Down
Loading
Loading