Skip to content

Commit

Permalink
chore: clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Nov 2, 2023
1 parent 723cd24 commit 72394ea
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 30 deletions.
30 changes: 13 additions & 17 deletions src/NewPicker/PickerInput/RangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ export default function Picker<DateType = any>(props: RangePickerProps<DateType>
classNames = {},

// Value
value,
defaultValue,
onChange,
onCalendarChange,
changeOnBlur,

disabled,
Expand Down Expand Up @@ -170,13 +166,12 @@ export default function Picker<DateType = any>(props: RangePickerProps<DateType>
const mergedAllowEmpty = separateConfig(allowEmpty, true);

// ======================== Value =========================
const [mergedValue, setMergedValue, submitValue, setSubmitValue, triggerChange, finishActive] =
useRangeValue({
...props,
formatList,
allowEmpty: mergedAllowEmpty,
focused,
});
const [mergedValue, triggerCalendarChange, triggerSubmitChange] = useRangeValue({
...props,
formatList,
allowEmpty: mergedAllowEmpty,
focused,
});

// ===================== Picker Value =====================
const [mergedStartPickerValue, setStartPickerValue] = useMergedState(
Expand Down Expand Up @@ -217,7 +212,7 @@ export default function Picker<DateType = any>(props: RangePickerProps<DateType>
const onSelectorChange = (date: DateType, index: number) => {
const clone = fillMergedValue(date, index);

triggerChange(clone);
triggerCalendarChange(clone);
};

// ==================== Selector Focus ====================
Expand All @@ -232,7 +227,7 @@ export default function Picker<DateType = any>(props: RangePickerProps<DateType>
setFocused(false);

// Always trigger submit since input is always means confirm
triggerChange(mergedValue);
triggerCalendarChange(mergedValue);

onBlur?.(event);
};
Expand All @@ -248,14 +243,15 @@ export default function Picker<DateType = any>(props: RangePickerProps<DateType>
// Focus or blur the open panel
const activeLen = activeList?.length;
if (activeLen === 1) {
triggerChange(nextValue);
// Trigger
triggerCalendarChange(nextValue);

// Open to the next field
selectorRef.current.focus(activeList[0] === 0 ? 1 : 0);
} else if (activeLen > 1) {
// Close anyway
onSelectorOpenChange(false, activeIndex);
finishActive(nextValue);
triggerSubmitChange(nextValue);
}
};

Expand Down Expand Up @@ -285,13 +281,13 @@ export default function Picker<DateType = any>(props: RangePickerProps<DateType>
if (mergedMode === picker && !needConfirm) {
triggerChangeAndFocusNext(date);
} else {
triggerChange(clone);
triggerCalendarChange(clone);
}
};

const onPopupClose = () => {
if (changeOnBlur) {
triggerChange(mergedValue);
triggerCalendarChange(mergedValue);
}

// Close popup
Expand Down
19 changes: 6 additions & 13 deletions src/NewPicker/PickerInput/hooks/useRangeValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { useLockEffect } from './useLockState';
// * Submit by next input
// * None of the Picker has focused anymore

type SetValue<DateType> = (val: RangeValueType<DateType>) => void;

type TriggerChange<DateType> = ([start, end]: RangeValueType<DateType>, source?: 'submit') => void;

export default function useRangeValue<DateType = any>(
Expand All @@ -28,11 +26,8 @@ export default function useRangeValue<DateType = any>(
},
): [
mergedValue: RangeValueType<DateType>,
setMergedValue: SetValue<DateType>,
submitValue: RangeValueType<DateType>,
setSubmitValue: SetValue<DateType>,
triggerChange: TriggerChange<DateType>,
finishActive: (value: RangeValueType<DateType>) => void,
triggerCalendarChange: TriggerChange<DateType>,
triggerSubmitChange: (value: RangeValueType<DateType>) => void,
] {
const {
value,
Expand Down Expand Up @@ -78,7 +73,7 @@ export default function useRangeValue<DateType = any>(
return [isSameStart && isSameEnd, isSameStart, isSameEnd];
};

const triggerChange = ([start, end]: RangeValueType<DateType>) => {
const triggerCalendarChange = ([start, end]: RangeValueType<DateType>) => {
const clone: RangeValueType<DateType> = [start, end];

// Update merged value
Expand Down Expand Up @@ -106,7 +101,7 @@ export default function useRangeValue<DateType = any>(
}

// Sync `calendarValue`
triggerChange(clone);
triggerCalendarChange(clone);

// Sync state
setSubmitValue(clone);
Expand All @@ -130,18 +125,16 @@ export default function useRangeValue<DateType = any>(
}
});

const finishActive = (nextValue: RangeValueType<DateType>) => {
console.log('finishActive');
const triggerSubmitChange = (nextValue: RangeValueType<DateType>) => {
triggerSubmit(nextValue);
};

useLockEffect(focused, () => {
if (!focused) {
console.log('Effect!');
triggerSubmit();
}
});

// ============================ Return ============================
return [mergedValue, setMergedValue, submitValue, setSubmitValue, triggerChange, finishActive];
return [mergedValue, triggerCalendarChange, triggerSubmitChange];
}

0 comments on commit 72394ea

Please sign in to comment.