Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
narrowizard committed Mar 17, 2020
1 parent 3365215 commit d1491e7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 29 deletions.
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

0 comments on commit d1491e7

Please sign in to comment.