2021SC@SDUSC
概述
本周,我主要阅读和分析了calendar部分的代码。代码目录如下: 主要实现的不同功能组件包括:
- 日期选择器
- 日期时间选择器
- 日期范围选择器
- 日期时间范围选择器
- 月份选择器
amis提供了非常完备的通过json生成日期选择器的组件API,可通过参数进行自定义配置,实现强大的功能。
示例
{
"type": "page",
"body": {
"type": "form",
"api": "/api/mock2/form/saveForm",
"body": [
{
"type": "input-date",
"name": "date",
"label": "日期"
}
]
}
}
效果
代码分析
Year|Month 选择的实现
如下图所示: 选择年份和月份的基本逻辑是一样的,代码结构也很类似。以下,我使用月份选择的代码进行分析:
MonthsView.js
该文件使用create-react-class 创建了基本月份选择的react 组件模板
'use strict';
var React = require('react'),
createClass = require('create-react-class')
;
var DateTimePickerMonths = createClass({
render: function() {
return React.createElement('div', { className: 'rdtMonths' }, [
React.createElement('table', { key: 'a' }, React.createElement('thead', {}, React.createElement('tr', {}, [
React.createElement('th', { key: 'prev', className: 'rdtPrev', onClick: this.props.subtractTime( 1, 'years' )}, React.createElement('span', {}, '?' )),
React.createElement('th', { key: 'year', className: 'rdtSwitch', onClick: this.props.showView( 'years' ), colSpan: 2, 'data-value': this.props.viewDate.year() }, this.props.viewDate.year() ),
React.createElement('th', { key: 'next', className: 'rdtNext', onClick: this.props.addTime( 1, 'years' )}, React.createElement('span', {}, '?' ))
]))),
React.createElement('table', { key: 'months' }, React.createElement('tbody', { key: 'b' }, this.renderMonths()))
]);
},
renderMonths: function() {
var date = this.props.selectedDate,
month = this.props.viewDate.month(),
year = this.props.viewDate.year(),
rows = [],
i = 0,
months = [],
renderer = this.props.renderMonth || this.renderMonth,
isValid = this.props.isValidDate || this.alwaysValidDate,
classes, props, currentMonth, isDisabled, noOfDaysInMonth, daysInMonth, validDay,
irrelevantDate = 1
;
while (i < 12) {
classes = 'rdtMonth';
currentMonth =
this.props.viewDate.clone().set({ year: year, month: i, date: irrelevantDate });
noOfDaysInMonth = currentMonth.endOf( 'month' ).format( 'D' );
daysInMonth = Array.from({ length: noOfDaysInMonth }, function( e, i ) {
return i + 1;
});
validDay = daysInMonth.find(function( d ) {
var day = currentMonth.clone().set( 'date', d );
return isValid( day );
});
isDisabled = ( validDay === undefined );
if ( isDisabled )
classes += ' rdtDisabled';
if ( date && i === date.month() && year === date.year() )
classes += ' rdtActive';
props = {
key: i,
'data-value': i,
className: classes
};
if ( !isDisabled )
props.onClick = ( this.props.updateOn === 'months' ?
this.updateSelectedMonth : this.props.setDate( 'month' ) );
months.push( renderer( props, i, year, date && date.clone() ) );
if ( months.length === 4 ) {
rows.push( React.createElement('tr', { key: month + '_' + rows.length }, months ) );
months = [];
}
i++;
}
return rows;
},
updateSelectedMonth: function( event ) {
this.props.updateSelectedDate( event );
},
renderMonth: function( props, month ) {
var localMoment = this.props.viewDate;
var monthStr = localMoment.localeData().monthsShort( localMoment.month( month ) );
var strLength = 3;
var monthStrFixedLength = monthStr.substring( 0, strLength );
return React.createElement('td', props, capitalize( monthStrFixedLength ) );
},
alwaysValidDate: function() {
return 1;
},
});
function capitalize( str ) {
return str.charAt( 0 ).toUpperCase() + str.slice( 1 );
}
module.exports = DateTimePickerMonths;
MonthsView.jsx
该文件继承自MonthsView.js
import MonthsView from 'react-datetime/src/MonthsView';
import moment from 'moment';
import React from 'react';
import {LocaleProps, localeable, TranslateFn} from '../../locale';
export interface OtherProps {
inputFormat?: string;
}
export class CustomMonthsView extends MonthsView {
props: {
viewDate: moment.Moment;
subtractTime: (
amount: number,
type: string,
toSelected?: moment.Moment
) => () => void;
addTime: (
amount: number,
type: string,
toSelected?: moment.Moment
) => () => void;
showView: (view: string) => () => void;
} & LocaleProps &
OtherProps;
renderMonths: () => JSX.Element;
renderMonth = (props: any, month: number) => {
var localMoment = this.props.viewDate;
var monthStr = localMoment
.localeData()
.monthsShort(localMoment.month(month));
var strLength = 3;
var monthStrFixedLength = monthStr.substring(0, strLength);
return (
<td {...props}>
<span>{monthStrFixedLength}</span>
</td>
);
};
render() {
const __ = this.props.translate;
const showYearHead = !/^mm$/i.test(this.props.inputFormat || '');
const canClick = /yy/i.test(this.props.inputFormat || '');
return (
<div className="rdtMonths">
{showYearHead && (
<table>
<thead>
<tr>
<th
className="rdtPrev"
onClick={this.props.subtractTime(1, 'years')}
>
«
</th>
{canClick ? (
<th
className="rdtSwitch"
onClick={this.props.showView('years')}
>
{this.props.viewDate.format(__('dateformat.year'))}
</th>
) : (
<th className="rdtSwitch">
{this.props.viewDate.format(__('dateformat.year'))}
</th>
)}
<th
className="rdtNext"
onClick={this.props.addTime(1, 'years')}
>
»
</th>
</tr>
</thead>
</table>
)}
<table>
<tbody>{this.renderMonths()}</tbody>
</table>
</div>
);
}
}
export default localeable(CustomMonthsView as any);
|