Merge pull request #1018 from 330595524/master

针对月份选择器,添加年份可点列表功能
This commit is contained in:
liaoxuezhi 2020-10-29 09:23:59 +08:00 committed by GitHub
commit 1c56e087bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 11 deletions

View File

@ -577,12 +577,24 @@ export default {
{ {
type: 'divider' type: 'divider'
}, },
{
type: 'month',
name: 'year-month',
inline: true,
label: '年月',
"value": "-1month",
"inputFormat": "YYYY-MM"
},
{
type: 'divider'
},
{ {
type: 'month', type: 'month',
name: 'month', name: 'month',
inline: true, inline: true,
label: '月份', label: '月份',
"value": "-1month" "value": "-1month",
"inputFormat": "MM"
}, },
{ {
type: 'divider' type: 'divider'

View File

@ -609,6 +609,7 @@ export class DatePicker extends React.Component<DateProps, DatePickerState> {
onChange={this.handleChange} onChange={this.handleChange}
requiredConfirm={!!(dateFormat && timeFormat)} requiredConfirm={!!(dateFormat && timeFormat)}
dateFormat={dateFormat} dateFormat={dateFormat}
inputFormat={inputFormat}
timeFormat={timeFormat} timeFormat={timeFormat}
isValidDate={this.checkIsValidDate} isValidDate={this.checkIsValidDate}
viewMode={viewMode} viewMode={viewMode}

View File

@ -8,6 +8,7 @@ import cx from 'classnames';
import moment from 'moment'; import moment from 'moment';
interface BaseDatePickerProps extends ReactDatePicker.DatetimepickerProps { interface BaseDatePickerProps extends ReactDatePicker.DatetimepickerProps {
inputFormat: string;
onViewModeChange?: (type: string) => void; onViewModeChange?: (type: string) => void;
requiredConfirm?: boolean; requiredConfirm?: boolean;
onClose?: () => void; onClose?: () => void;
@ -29,6 +30,7 @@ class BaseDatePicker extends ReactDatePicker {
props.setDateTimeState = this.setState.bind(this); props.setDateTimeState = this.setState.bind(this);
[ [
'inputFormat',
'onChange', 'onChange',
'onClose', 'onClose',
'requiredConfirm', 'requiredConfirm',
@ -43,9 +45,10 @@ class BaseDatePicker extends ReactDatePicker {
})((this as any).getComponentProps); })((this as any).getComponentProps);
setDate = (type: 'month' | 'year') => { setDate = (type: 'month' | 'year') => {
const currentShould = this.props.viewMode === 'months' && this.props.inputFormat.toUpperCase() !== 'MM'
const nextViews = { const nextViews = {
month: 'days', month: currentShould?'months':'days',
year: 'days' year: currentShould?'months':'days'
}; };
return (e: any) => { return (e: any) => {

View File

@ -2,7 +2,11 @@
import MonthsView from 'react-datetime/src/MonthsView'; import MonthsView from 'react-datetime/src/MonthsView';
import moment from 'moment'; import moment from 'moment';
import React from 'react'; import React from 'react';
import {LocaleProps, localeable} from '../../locale'; import {LocaleProps, localeable, TranslateFn} from '../../locale';
export interface OtherProps {
inputFormat: string;
}
export class CustomMonthsView extends MonthsView { export class CustomMonthsView extends MonthsView {
props: { props: {
@ -17,7 +21,8 @@ export class CustomMonthsView extends MonthsView {
type: string, type: string,
toSelected?: moment.Moment toSelected?: moment.Moment
) => () => void; ) => () => void;
} & LocaleProps; showView: (view: string) => () => void;
} & LocaleProps & OtherProps;
renderMonths: () => JSX.Element; renderMonths: () => JSX.Element;
renderMonth = (props: any, month: number) => { renderMonth = (props: any, month: number) => {
var localMoment = this.props.viewDate; var localMoment = this.props.viewDate;
@ -36,11 +41,12 @@ export class CustomMonthsView extends MonthsView {
}; };
render() { render() {
const __ = this.props.translate; const __ = this.props.translate;
const showYearHead = this.props.inputFormat.toUpperCase() !== 'MM'
const canClick = this.props.inputFormat.toUpperCase() === 'YYYY-MM'
return ( return (
<div className="rdtMonths"> <div className="rdtMonths">
<table> <table>
<thead> {showYearHead&&<thead>
<tr> <tr>
<th <th
className="rdtPrev" className="rdtPrev"
@ -48,14 +54,23 @@ export class CustomMonthsView extends MonthsView {
> >
« «
</th> </th>
<th className="rdtSwitch"> {
{this.props.viewDate.format(__('YYYY年'))} canClick
</th> ?
<th className="rdtSwitch" onClick={this.props.showView('years')}>
{this.props.viewDate.format(__('YYYY年'))}
</th>
:
<th className="rdtSwitch">
{this.props.viewDate.format(__('YYYY年'))}
</th>
}
<th className="rdtNext" onClick={this.props.addTime(1, 'years')}> <th className="rdtNext" onClick={this.props.addTime(1, 'years')}>
» »
</th> </th>
</tr> </tr>
</thead> </thead>}
</table> </table>
<table> <table>
<tbody>{this.renderMonths()}</tbody> <tbody>{this.renderMonths()}</tbody>