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

fixes https://github.com/ant-design/ant-design/issues/22142 #656

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions examples/antd-range-calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ class Demo extends React.Component {
state = {
value: [],
hoverValue: [],
// control mode
mode: ['date', 'date'],
};

onChange = value => {
Expand All @@ -132,6 +134,13 @@ class Demo extends React.Component {
this.setState({ hoverValue });
};

onPanelChange = (_, mode) => {
console.log('onPanelChange', mode);
this.setState({
mode,
});
};

render() {
const { state } = this;
const calendar = (
Expand All @@ -144,6 +153,8 @@ class Demo extends React.Component {
locale={cn ? zhCN : enUS}
disabledTime={disabledTime}
timePicker={timePickerElement}
mode={this.state.mode}
onPanelChange={this.onPanelChange}
/>
);
return (
Expand Down Expand Up @@ -183,6 +194,9 @@ export default () => (
timePicker={timePickerElement}
disabledTime={disabledTime}
renderFooter={() => <span>extra footer</span>}
// onPanelChange={(_,mode) => {
// console.log("onPanelChange", mode);
// }}
/>
</div>
<br />
Expand Down
75 changes: 46 additions & 29 deletions src/RangeCalendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function onInputSelect(
}

interface RangeCalendarProps {
mode?: CalendarTypeMode;
mode?: [CalendarTypeMode, CalendarTypeMode];
selectedValue?: Moment[];
defaultSelectedValue?: Moment[];
type?: 'both' | 'start' | 'end';
Expand Down Expand Up @@ -129,7 +129,7 @@ interface RangeCalendarProps {

interface RangeCalendarState {
value?: Moment[];
mode?: CalendarTypeMode[];
mode?: [CalendarTypeMode, CalendarTypeMode];
selectedValue?: Moment[];
showTimePicker?: boolean;
hoverValue?: Moment[];
Expand All @@ -146,7 +146,7 @@ class RangeCalendar extends React.Component<RangeCalendarProps, RangeCalendarSta
defaultSelectedValue: [],
onValueChange: noop,
onHoverChange: noop,
onPanelChange: noop,
// onPanelChange: noop,
disabledTime: noop,
onInputSelect: noop,
showToday: true,
Expand Down Expand Up @@ -404,15 +404,25 @@ class RangeCalendar extends React.Component<RangeCalendarProps, RangeCalendarSta
};

onOpenTimePicker = () => {
this.setState({
showTimePicker: true,
});
const { props, state } = this;
if (props.onPanelChange) {
props.onPanelChange(state.value, ['time', 'time']);
} else {
this.setState({
showTimePicker: true,
});
}
};

onCloseTimePicker = () => {
this.setState({
showTimePicker: false,
});
const { props, state } = this;
if (props.onPanelChange) {
props.onPanelChange(state.value, ['date', 'date']);
} else {
this.setState({
showTimePicker: false,
});
}
};

onOk = () => {
Expand Down Expand Up @@ -454,32 +464,39 @@ class RangeCalendar extends React.Component<RangeCalendarProps, RangeCalendarSta
return this.fireValueChange(value);
};

onStartPanelChange = (value, mode) => {
onStartPanelChange = (value, mode: CalendarTypeMode) => {
const { props, state } = this;
const newMode = [mode, state.mode[1]];
const newState: RangeCalendarState = {
panelTriggerSource: 'start',
};
if (!('mode' in props)) {
newState.mode = newMode;
const newMode: [CalendarTypeMode, CalendarTypeMode] = [mode, state.mode[1]];
if (props.onPanelChange) {
// control mode
const newValue: Moment[] = [value || state.value[0], state.value[1]];
props.onPanelChange(newValue, newMode);
} else {
const newState: RangeCalendarState = {
panelTriggerSource: 'start',
};
if (!('mode' in props)) {
newState.mode = newMode;
}
this.setState(newState);
}
this.setState(newState);
const newValue: Moment[] = [value || state.value[0], state.value[1]];
props.onPanelChange(newValue, newMode);
};

onEndPanelChange = (value, mode) => {
onEndPanelChange = (value, mode: CalendarTypeMode) => {
const { props, state } = this;
const newMode = [state.mode[0], mode];
const newState: RangeCalendarState = {
panelTriggerSource: 'end',
};
if (!('mode' in props)) {
newState.mode = newMode;
const newMode: [CalendarTypeMode, CalendarTypeMode] = [state.mode[0], mode];
if (props.onPanelChange) {
const newValue: Moment[] = [state.value[0], value || state.value[1]];
props.onPanelChange(newValue, newMode);
} else {
const newState: RangeCalendarState = {
panelTriggerSource: 'end',
};
if (!('mode' in props)) {
newState.mode = newMode;
}
this.setState(newState);
}
this.setState(newState);
const newValue: Moment[] = [state.value[0], value || state.value[1]];
props.onPanelChange(newValue, newMode);
};

static getDerivedStateFromProps(nextProps, state) {
Expand Down