Skip to content

Commit

Permalink
feat: Update the latest time through the ref attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
wb-gp529739 committed Nov 28, 2024
1 parent 6540de1 commit b30ba64
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
29 changes: 21 additions & 8 deletions packages/ui/src/DateRanger/Ranger.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useRef, useState } from 'react';
import React, { useEffect, useRef, useState, useImperativeHandle } from 'react';
import { Button, DatePicker, Divider, Dropdown, Radio, Space, theme } from '@oceanbase/design';
import type { TooltipProps, FormItemProps } from '@oceanbase/design';
import {
Expand Down Expand Up @@ -86,7 +86,7 @@ export interface DateRangerProps

const prefix = getPrefix('date-ranger');

const Ranger = (props: DateRangerProps) => {
const Ranger = React.forwardRef((props: DateRangerProps, ref) => {
const {
selects = [
NEAR_1_MINUTES,
Expand Down Expand Up @@ -138,13 +138,18 @@ const Ranger = (props: DateRangerProps) => {
value || defaultValue ? CUSTOMIZE : (defaultQuickValue ?? selects?.[0]?.name);
const [rangeName, setRangeName] = useState(defaultRangeName);

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

const [open, setOpen] = useState(false);
const [tooltipOpen, setTooltipOpen] = useState(false);
Expand Down Expand Up @@ -291,6 +296,10 @@ const Ranger = (props: DateRangerProps) => {
}
};

useImperativeHandle(ref, () => ({
updateCurrentTime: setNow,
}));

const rangeLabel =
rangeName === CUSTOMIZE
? getCustomizeRangeLabel()
Expand Down Expand Up @@ -455,7 +464,11 @@ const Ranger = (props: DateRangerProps) => {
{hasRewind && (
<Radio.Button
value="stepBack"
style={{ paddingInline: 8, borderInlineStart: 0, borderRadius: 0 }}
style={{
paddingInline: 8,
borderInlineStart: 0,
borderRadius: 0,
}}
onClick={() => {
if (isPlay) {
setIsPlay(false);
Expand Down Expand Up @@ -523,7 +536,7 @@ const Ranger = (props: DateRangerProps) => {
)}
</Space>
);
};
});

export default LocaleWrapper({
componentName: 'DateRanger',
Expand Down
44 changes: 44 additions & 0 deletions packages/ui/src/DateRanger/demo/updateCurrentTime.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { useState, useRef } from 'react';
import { DateRanger } from '@oceanbase/ui';
import { Divider, Flex, Button } from '@oceanbase/design';
import dayjs from 'dayjs';

type TimeRangeRef = {
updateCurrentTime: () => void;
};

export default () => {
const ref = useRef<TimeRangeRef>();
const [timeRange, setTimeRange] = useState([]);
return (
<div>
<Flex gap="middle" align="center">
<Button
onClick={() => {
console.log('ref: ', ref);
ref.current.updateCurrentTime();
}}
>
更新最新时间
</Button>
{timeRange.length && (
<span>
{`${dayjs(timeRange[0]).format('YYYY-MM-DD HH:mm:ss')} ~ ${dayjs(timeRange[1]).format(
'YYYY-MM-DD HH:mm:ss'
)}`}
</span>
)}
</Flex>

<Divider children="preview" />
<DateRanger
ref={ref}
onChange={setTimeRange}
defaultQuickValue={DateRanger.NEAR_30_MINUTES.name}
hasRewind={false}
hasForward={false}
hasSync={false}
/>
</div>
);
};
4 changes: 4 additions & 0 deletions packages/ui/src/DateRanger/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ nav:

<code src="./demo/default-value.tsx" title="指定默认值"></code>

<code src="./demo/updateCurrentTime.tsx" title="更新当前时间"></code>

## API

| 参数 | 说明 | 类型 | 默认值 | 版本 |
Expand All @@ -34,6 +36,8 @@ nav:
| hideYear | 当时间范围在本年时,隐藏年份 | boolean | false | - |
| hideSecond | 隐藏"秒” | boolean | false | - |
| autoCalcRange | 自动计算时间范围并回显到选择器tag | boolean | false | - |
| ref | updateCurrentTime 手动更新当前时间 | function | - | - |

| 其他 antd/RangePicker 的 `props` | [antd-RangePicker](https://ant.design/components/date-picker-cn/#RangePicker) | - | - | - |

### RangeOption
Expand Down

0 comments on commit b30ba64

Please sign in to comment.