Skip to content

Commit

Permalink
fix(TimePicker): timePicker confirm event is invalid (#79)
Browse files Browse the repository at this point in the history
* fix: timePicker confirm event is invalid

* fix: 优化

* fix: 优化

* fix: fixup type
  • Loading branch information
yaogengzhu authored Dec 25, 2021
1 parent 4605d70 commit 45bca64
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
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(() => {
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]);

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

0 comments on commit 45bca64

Please sign in to comment.