Skip to content

Commit c6b619f

Browse files
committed
Improve type accuracy for value prop and all callbacks
1 parent 210a6d3 commit c6b619f

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

src/TimeRangePicker.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ describe('TimeRangePicker', () => {
602602
const onChange = vi.fn();
603603
const valueFrom = '10:00:00';
604604
const valueTo = '16:00:00';
605-
const value = [valueFrom, valueTo];
605+
const value = [valueFrom, valueTo] as [string, string];
606606

607607
const { container } = render(
608608
<TimeRangePicker format="H:m:s" maxDetail="second" onChange={onChange} value={value} />,
@@ -698,7 +698,7 @@ describe('TimeRangePicker', () => {
698698
const onChange = vi.fn();
699699
const valueFrom = '10:00:00';
700700
const valueTo = '16:00:00';
701-
const value = [valueFrom, valueTo];
701+
const value = [valueFrom, valueTo] as [string, string];
702702

703703
const { container } = render(
704704
<TimeRangePicker format="H:m:s" maxDetail="second" onChange={onChange} value={value} />,

src/TimeRangePicker.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import TimeInput from 'react-time-picker/dist/cjs/TimeInput';
1010

1111
import { isTime } from './shared/propTypes';
1212

13-
import type { ClassName, Detail, LooseValue } from './shared/types';
13+
import type { ClassName, Detail, LooseValue, Value } from './shared/types';
1414

1515
const baseClassName = 'react-timerange-picker';
1616
const outsideActionEvents = ['mousedown', 'focusin', 'touchstart'];
@@ -76,7 +76,7 @@ type TimeRangePickerProps = {
7676
minutePlaceholder?: string;
7777
name?: string;
7878
nativeInputAriaLabel?: string;
79-
onChange?: (value: null | (string | Date | null)[]) => void;
79+
onChange?: (value: Value) => void;
8080
onClockClose?: () => void;
8181
onClockOpen?: () => void;
8282
onFocus?: (event: React.FocusEvent<HTMLDivElement>) => void;
@@ -162,10 +162,7 @@ export default function TimeRangePicker(props: TimeRangePickerProps) {
162162
}
163163
}
164164

165-
function onChange(
166-
value: null | (string | Date | null)[],
167-
shouldCloseClock = shouldCloseClockProps,
168-
) {
165+
function onChange(value: Value, shouldCloseClock = shouldCloseClockProps) {
169166
if (shouldCloseClock) {
170167
closeClock();
171168
}

src/shared/types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@ export type ClassName = string | null | undefined | (string | null | undefined)[
44

55
export type Detail = 'hour' | 'minute' | 'second';
66

7-
export type LooseValue = string | Date | null | (string | Date | null)[];
7+
type LooseValuePiece = string | Date | null;
8+
9+
export type LooseValue = LooseValuePiece | [LooseValuePiece, LooseValuePiece];
10+
11+
type ValuePiece = string | Date | null;
12+
13+
export type Value = ValuePiece | [ValuePiece, ValuePiece];

0 commit comments

Comments
 (0)