Skip to content

Commit

Permalink
fix: Default value
Browse files Browse the repository at this point in the history
  • Loading branch information
linhf123 committed Mar 26, 2024
1 parent c02a21a commit 70b4937
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
13 changes: 6 additions & 7 deletions packages/ui/src/DateRanger/PickerPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Button, Col, Divider, Form, Input, Row, Space, TimePicker } from '@ocea
import { noop } from 'lodash';
import moment from 'moment';
import dayjs from 'dayjs';
import { useUpdate, useMount } from 'ahooks';
import { useUpdate } from 'ahooks';

type RangeValue = [Moment, Moment] | [Dayjs, Dayjs];

Expand All @@ -36,17 +36,16 @@ const InternalPickerPanel = (props: PickerPanelProps) => {
const [defaultS, defaultE] = defaultValue;
const [calendarValue, setCalendarValue] = React.useState(defaultValue);
const [internalHoverValues, setInternalHoverValues] = React.useState(null);
const [activeIndex, setActiveIndex] = React.useState(0);

const hoverValues = React.useMemo(() => {
return internalHoverValues || calendarValue;
}, [internalHoverValues, calendarValue]);

const [activeIndex, setActiveIndex] = React.useState(0);

// ======================== Change ========================
const fillCalendarValue = (date, index: number) =>
// Trigger change only when date changed
fillIndex(hoverValues, index, date);
fillIndex(calendarValue, index, date);

function fillIndex<T extends any[]>(ori: T, index: number, value: T[number]): T {
const clone = [...ori] as T;
Expand All @@ -58,7 +57,7 @@ const InternalPickerPanel = (props: PickerPanelProps) => {
setInternalHoverValues(date ? fillCalendarValue(date, activeIndex) : null);
};

const formatValues = [...hoverValues]
const formatValues = [...calendarValue]
.sort((a, b) => {
return a?.valueOf() - b?.valueOf();
})
Expand All @@ -67,13 +66,13 @@ const InternalPickerPanel = (props: PickerPanelProps) => {
});

const [form] = Form.useForm();
const [s, e] = formatValues;
useEffect(() => {
const [s, e] = formatValues;
form.setFieldsValue({
startDate: s,
endDate: e,
});
}, [...formatValues]);
}, [s, e]);

const defaultTime = useMemo(() => {
return isMoment ? moment() : dayjs();
Expand Down
20 changes: 13 additions & 7 deletions packages/ui/src/DateRanger/Ranger.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,16 @@ const Ranger = (props: DateRangerProps) => {
moment.isMoment(value?.[0]) ||
moment.isMoment(value?.[1]);

const [innerValue, setInnerValue] = useState<RangeValue>(value ?? defaultValue);

const [rangeName, setRangeName] = useState(
value || defaultValue ? CUSTOMIZE : defaultQuickValue ?? selects?.[0]?.name
const defaultRangeName =
value || defaultValue ? CUSTOMIZE : defaultQuickValue ?? selects?.[0]?.name;
const [rangeName, setRangeName] = useState(defaultRangeName);

const [innerValue, setInnerValue] = useState<RangeValue>(
value ??
defaultValue ??
(selects
.find(item => item.name === defaultRangeName)
?.range(isMoment ? moment() : dayjs()) as RangeValue)
);

const isStepMode = mode === 'step';
Expand Down Expand Up @@ -152,6 +158,7 @@ const Ranger = (props: DateRangerProps) => {
setOpen(false);
setTooltipOpen(false);
};

const handleNameChange = (name: string) => {
if (name !== CUSTOMIZE) {
closeTooltip();
Expand Down Expand Up @@ -351,8 +358,6 @@ const Ranger = (props: DateRangerProps) => {
}}
menu={{
onClick: ({ key, domEvent }) => {
handleNameChange(key);

if (key === CUSTOMIZE && isStepMode) {
// updateState 的修改会慢于 openChange 的判断,所以修改 refState.current.step 让 Dropdown 不要关闭
refState.current.step = STEP_CUSTOMIZE;
Expand All @@ -362,6 +367,7 @@ const Ranger = (props: DateRangerProps) => {
const selected = NEAR_TIME_LIST.find(_item => _item.name === key);
// 存在快捷选项切换为极简模式
if (selected?.range) {
handleNameChange(key);
setIsPlay(true);
rangeChange(selected.range(isMoment ? moment() : dayjs()) as RangeValue);
}
Expand Down Expand Up @@ -459,6 +465,7 @@ const Ranger = (props: DateRangerProps) => {
className={`${prefix}-picker`}
style={{
pointerEvents: 'none',
border: 0,
}}
disabledDate={pastOnly ? disabledFuture : disabledDate}
format={v => {
Expand All @@ -468,7 +475,6 @@ const Ranger = (props: DateRangerProps) => {
// @ts-ignore
value={innerValue}
onChange={datePickerChange}
// showTime={true}
allowClear={false}
size={size}
// 透传 props 到 antd Ranger
Expand Down

0 comments on commit 70b4937

Please sign in to comment.