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(TimePicker): timePicker confirm event is invalid #79

Merged
merged 4 commits into from
Dec 25, 2021
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
5 changes: 4 additions & 1 deletion src/time-picker/TimePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ const TimePicker = forwardRefWithStatics(
hideDisabledTime={hideDisabledTime}
isFooterDisplay={true}
onChange={onChange}
handleConfirmClick={() => setPanelShow(false)}
handleConfirmClick={(value) => {
onChange(dayjs(value).format(format));
setPanelShow(false);
}}
value={value}
/>
}
Expand Down
5 changes: 4 additions & 1 deletion src/time-picker/TimeRangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ const TimeRangePicker: FC<TimeRangePickerProps> = (props) => {
isFooterDisplay={true}
value={value}
onChange={onChange}
handleConfirmClick={() => setPanelShow(false)}
handleConfirmClick={(value) => {
onChange(value);
setPanelShow(false);
}}
/>
}
disabled={disabled as boolean}
Expand Down
23 changes: 20 additions & 3 deletions src/time-picker/panel/TimePickerPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC } from 'react';
import React, { FC, useMemo } from 'react';
import dayjs from 'dayjs';
import SinglePanel, { SinglePanelProps } from './SinglePanel';

Expand All @@ -10,7 +10,7 @@ import { DEFAULT_STEPS, DEFAULT_FORMAT, useTimePickerTextConfig } from '../const
export interface TimePickerPanelProps extends SinglePanelProps {
// 是否展示footer
isFooterDisplay?: boolean;
handleConfirmClick?: () => void;
handleConfirmClick: (defaultValue: dayjs.Dayjs) => void;
}

const TimePickerPanel: FC<TimePickerPanelProps> = (props) => {
Expand All @@ -29,14 +29,31 @@ const TimePickerPanel: FC<TimePickerPanelProps> = (props) => {
const panelClassName = `${classPrefix}-time-picker__panel`;
const showNowTimeBtn = !!steps.filter((v) => v > 1).length;

const defaultValue = useMemo(() => {
uyarn marked this conversation as resolved.
Show resolved Hide resolved
const isStepsSet = !!steps.filter((v) => v > 1).length;
if (value) {
return dayjs(value, format);
}
if (isStepsSet) {
return dayjs().hour(0).minute(0).second(0);
}
return dayjs();
}, [value, format, steps]);

return (
<div className={panelClassName}>
<div className={`${panelClassName}-section-body`}>
<SinglePanel {...props} format={format} steps={steps} value={value} />
</div>
{isFooterDisplay ? (
<div className={`${panelClassName}-section-footer`}>
<Button theme="primary" variant="base" onClick={handleConfirmClick}>
<Button
theme="primary"
variant="base"
onClick={() => {
handleConfirmClick(defaultValue);
}}
>
{TEXT_CONFIG.confirm}
</Button>
{!showNowTimeBtn ? (
Expand Down
21 changes: 17 additions & 4 deletions src/time-picker/panel/TimePickerRangePanel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC } from 'react';
import React, { FC, useMemo } from 'react';
import classNames from 'classnames';
import dayjs from 'dayjs';

Expand All @@ -9,12 +9,12 @@ import Button from '../../button';

import { DEFAULT_STEPS, DEFAULT_FORMAT, useTimePickerTextConfig } from '../consts';

import { TdTimeRangePickerProps } from '../type';
import { TdTimeRangePickerProps, TimeRangeValue } from '../type';

export interface TimeRangePickerPanelProps extends Omit<SinglePanelProps, 'value' | 'onChange'> {
// 是否展示footer
isFooterDisplay?: boolean;
handleConfirmClick?: () => void;
handleConfirmClick?: (value: TimeRangeValue) => void;
value: TdTimeRangePickerProps['value'];
onChange: TdTimeRangePickerProps['onChange'];
}
Expand Down Expand Up @@ -45,6 +45,13 @@ const TimePickerPanel: FC<TimeRangePickerPanelProps> = (props) => {
}
};

const defaultValue = useMemo(() => {
if (value && value.length === 0) {
return [dayjs().format(format), dayjs().format(format)];
}
return value;
}, [value, format]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useMemo 是否非必须?


return (
<div className={classNames(panelClassName, `${panelClassName}-section`)}>
<div className={`${panelClassName}-section-body`}>
Expand All @@ -65,7 +72,13 @@ const TimePickerPanel: FC<TimeRangePickerPanelProps> = (props) => {
</div>
{isFooterDisplay ? (
<div className={`${panelClassName}-section-footer`}>
<Button theme="primary" variant="base" onClick={handleConfirmClick}>
<Button
theme="primary"
variant="base"
onClick={() => {
handleConfirmClick(defaultValue);
}}
>
{TEXT_CONFIG.confirm}
</Button>
</div>
Expand Down