Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ui): DateRanger allowClear should work #929

Merged
merged 3 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions packages/ui/src/DateRanger/PickerPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@ import {
Space,
TimePicker,
} from '@oceanbase/design';
import type { FormItemProps } from '@oceanbase/design';
import { noop } from 'lodash';
import moment from 'moment';
import dayjs from 'dayjs';
import { useUpdate } from 'ahooks';
import { toArray } from '@oceanbase/util';
import { getPrefix } from '../_util';
import { DATE_TIME_MONTH_FORMAT, DATE_TIME_MONTH_FORMAT_CN } from './constant';
type RangeValue = [Moment, Moment] | [Dayjs, Dayjs];
import type { RangeValue } from './Ranger';
type ValidateTrigger = 'submit' | 'valueChange';

type MaybeArray<T> = T | T[];
Expand Down Expand Up @@ -58,7 +55,7 @@ const TIME_FORMAT = 'HH:mm:ss';

const InternalPickerPanel = (props: PickerPanelProps) => {
const {
defaultValue = [],
defaultValue,
isMoment,
locale,
tip,
Expand Down Expand Up @@ -259,6 +256,7 @@ const InternalPickerPanel = (props: PickerPanelProps) => {
label={locale.startDate}
validateStatus={errorTypeMap['startDate']}
style={{ marginBottom: 8 }}
rules={[{ required: true }]}
>
<Input
size="middle"
Expand Down Expand Up @@ -289,6 +287,7 @@ const InternalPickerPanel = (props: PickerPanelProps) => {
style={{ marginBottom: 8 }}
validateStatus={errorTypeMap['startTime']}
initialValue={defaultS || defaultTime}
rules={[{ required: true }]}
>
<TimePicker
allowClear={false}
Expand Down Expand Up @@ -319,6 +318,7 @@ const InternalPickerPanel = (props: PickerPanelProps) => {
label={locale.endDate}
style={{ marginBottom: 0 }}
validateStatus={errorTypeMap['endDate']}
rules={[{ required: true }]}
>
<Input
onBlur={e => {
Expand Down Expand Up @@ -348,6 +348,7 @@ const InternalPickerPanel = (props: PickerPanelProps) => {
style={{ marginBottom: 0 }}
validateStatus={errorTypeMap['endTime']}
initialValue={defaultE || defaultTime}
rules={[{ required: true }]}
>
<TimePicker
allowClear={false}
Expand Down
9 changes: 4 additions & 5 deletions packages/ui/src/DateRanger/Ranger.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import './index.less';

export type RangeName = 'customize' | string;

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

export type RangeDateValue = {
name: RangeName;
Expand Down Expand Up @@ -212,7 +212,6 @@ const Ranger = React.forwardRef((props: DateRangerProps, ref) => {
rangeChange(range);
setRangeName(CUSTOMIZE);
};

const disabledFuture = (current: Moment | Dayjs) => {
const futureDay = moment.isMoment(current) ? moment().endOf('day') : dayjs().endOf('day');
// 禁止选择未来日期
Expand Down Expand Up @@ -304,7 +303,6 @@ const Ranger = React.forwardRef((props: DateRangerProps, ref) => {

const currentRange = selects.find(_item => _item.name === rangeName);
const rangeLabel = rangeName === CUSTOMIZE ? getCustomizeRangeLabel() : currentRange?.rangeLabel;

const label =
rangeName === CUSTOMIZE
? getCustomizeLabel()
Expand Down Expand Up @@ -360,7 +358,7 @@ const Ranger = React.forwardRef((props: DateRangerProps, ref) => {
{originNode}
<Divider type="vertical" style={{ height: 'auto', margin: '0px 4px 0px 0px' }} />
<InternalPickerPanel
defaultValue={innerValue}
defaultValue={innerValue || []}
// @ts-ignore
locale={locale}
disabledDate={pastOnly ? disabledFuture : disabledDate}
Expand Down Expand Up @@ -443,7 +441,7 @@ const Ranger = React.forwardRef((props: DateRangerProps, ref) => {
<DatePicker.RangePicker
className={classNames(`${prefix}-picker`)}
style={{
pointerEvents: 'none',
// pointerEvents: 'none',
border: 0,
}}
format={v => {
Expand All @@ -467,6 +465,7 @@ const Ranger = React.forwardRef((props: DateRangerProps, ref) => {
suffixIcon={null}
// 透传 props 到 antd Ranger
{...omit(rest, 'value', 'onChange')}
open={false}
/>
</span>
)}
Expand Down
8 changes: 8 additions & 0 deletions packages/ui/src/DateRanger/demo/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ export default () => {
</Flex>
<Divider children="preview" />
<DateRanger
onChange={value => {
if (value) {
console.log(value[0].format(), value[1].format());
} else {
console.log(value);
}
}}
allowClear={true}
hasForward={getValue('hasForward')}
hasRewind={getValue('hasRewind')}
hasNow={getValue('hasNow')}
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/DateRanger/demo/locale.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default () => {
</div>
<ConfigProvider locale={locale}>
<Space direction="vertical">
<DateRanger />
<DateRanger allowClear />
linhf123 marked this conversation as resolved.
Show resolved Hide resolved
<DateRanger simpleMode />
</Space>
</ConfigProvider>
Expand Down
10 changes: 9 additions & 1 deletion packages/ui/src/DateRanger/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,19 @@
// FIXED: 存在不生效情况
.@{prefix}-picker {
padding-left: 0px;
padding-right: 16px;
border: 0px;
pointer-events: none;
input {
text-align: center;
}
// 覆盖默认 antd 样式,定制出 RangerPicker 整体的感觉
.@{prefixCls}-picker-active-bar {
height: 0;
}

.@{prefixCls}-picker-input > input {
cursor: pointer;
}
}

&:hover {
Expand Down
Loading