Skip to content

Commit

Permalink
fix(ui:time-picker): fix time order
Browse files Browse the repository at this point in the history
  • Loading branch information
xiejay97 committed Jun 23, 2022
1 parent ddaa337 commit 912d557
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function Demo() {
return (
<>
<DCheckbox
dModel={state !== 'mixed' ? [state] : undefined}
dModel={state !== 'mixed' ? state : undefined}
dIndeterminate={state === 'mixed'}
onModelChange={(checked) => {
setValue(checked ? [1, 2, 3] : []);
Expand Down
2 changes: 2 additions & 0 deletions packages/ui/src/components/time-picker/TimePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ function TimePicker(props: DTimePickerProps, ref: React.ForwardedRef<DTimePicker
if (dataRef.current.time.every((v) => !isNull(v))) {
dataRef.current.focusAnother = orderTime(dataRef.current.time as [Date, Date], dOrder);
if (dataRef.current.focusAnother) {
dataRef.current.time.reverse();
dataRef.current.inputValue.reverse();
}
_changeValue(dataRef.current.time as [Date, Date]);
Expand All @@ -123,6 +124,7 @@ function TimePicker(props: DTimePickerProps, ref: React.ForwardedRef<DTimePicker
(draft as [Date, Date])[position === 'start' ? 0 : 1] = time;
dataRef.current.focusAnother = orderTime(draft as [Date, Date], dOrder);
if (dataRef.current.focusAnother) {
(draft as [Date, Date]).reverse();
dataRef.current.inputValue.reverse();
}
});
Expand Down
26 changes: 18 additions & 8 deletions packages/ui/src/components/time-picker/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,31 @@ dayjs.extend(customParseFormat);

export default dayjs;

export function deepCompareDate(a: Date | null | [Date | null, Date | null], b: Date | null | [Date | null, Date | null], format: string) {
const isSame = (t1: Date | null, t2: Date | null) =>
(isNull(t1) && isNull(t2)) || (isDate(t1) && isDate(t2) && dayjs(t1).format(format) === dayjs(t2).format(format));
export function deepCompareDate(a: Date | null | [Date, Date], b: Date | null | [Date, Date], format: string) {
const isSame = (t1: Date, t2: Date) => dayjs(t1).format(format) === dayjs(t2).format(format);

if (isDate(a) && isDate(b)) {
return isSame(a, b);
}
if (isNull(a) && isNull(b)) {
return true;
}
if (isArray(a) && isArray(b)) {
return isSame(a[0], b[0]) && isSame(a[1], b[1]);
} else if (!isArray(a) && !isArray(b)) {
return isSame(a, b);
}
return false;
}

export function orderTime(time: [Date, Date], order: 'ascend' | 'descend' | null): boolean {
if ((order === 'ascend' && dayjs(time[0]).isAfter(dayjs(time[1]))) || (order === 'descend' && dayjs(time[0]).isBefore(dayjs(time[1])))) {
time.reverse();
function _orderTime(time: [dayjs.Dayjs, dayjs.Dayjs], order: 'ascend' | 'descend' | null): boolean {
if ((order === 'ascend' && time[0].isAfter(time[1])) || (order === 'descend' && time[0].isBefore(time[1]))) {
return true;
}
return false;
}

export function orderTime(time: [Date, Date], order: 'ascend' | 'descend' | null): boolean {
const t1 = dayjs(time[0]).set('year', 2000).set('month', 0).set('date', 1);
const t2 = dayjs(time[1]).set('year', 2000).set('month', 0).set('date', 1);

return _orderTime([t1, t2], order);
}

0 comments on commit 912d557

Please sign in to comment.