Skip to content

Commit b6d6b5e

Browse files
committedMay 12, 2018
✨ [feature] map month number to month name(#26)
1 parent ff51317 commit b6d6b5e

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed
 

‎examples/basic/index.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ import DatePicker from '../../lib/index';
2525
}
2626

2727
render() {
28+
const monthMap = {
29+
'01': 'Jan',
30+
'02': 'Feb',
31+
'03': 'Mar',
32+
'04': 'Apr',
33+
'05': 'May',
34+
'06': 'Jun',
35+
'07': 'Jul',
36+
'08': 'Aug',
37+
'09': 'Sep',
38+
'10': 'Oct',
39+
'11': 'Nov',
40+
'12': 'Dec',
41+
};
42+
2843
return (
2944
<div className="App">
3045
<p className="select-time ">
@@ -60,7 +75,7 @@ import DatePicker from '../../lib/index';
6075
<DatePicker
6176
value={this.state.time}
6277
dateSteps={[1, 1, 5]}
63-
dateFormat={['hh', 'mm', 'ss']}
78+
dateFormat={['YYYY', ['MM', (month) => monthMap[month]], 'DD']}
6479
theme={this.state.theme}
6580
isOpen={this.state.isOpen}
6681
onSelect={this.handleSelect}

‎lib/DatePickerItem.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const MIDDLE_INDEX = Math.floor(DATE_LENGTH / 2); // 日期数组中间值
1313
const MIDDLE_Y = - DATE_HEIGHT * MIDDLE_INDEX; // translateY值
1414

1515
const isUndefined = val => typeof val === 'undefined';
16+
const isArray = val => Object.prototype.toString.apply(val) === '[object Array]';
17+
const isFunction = val => Object.prototype.toString.apply(val) === '[object Function]';
1618

1719
/**
1820
* 根据格式获取时间滑动的类别
@@ -44,7 +46,7 @@ type Props = {
4446
value: Object,
4547
min: Object,
4648
max: Object,
47-
format: string,
49+
format: string | Array<*>,
4850
step: number,
4951
onSelect: Function,
5052
}
@@ -73,7 +75,19 @@ class DatePickerItem extends Component<void, Props, State> {
7375
};
7476

7577
// 设置时间选择器单元的类别
76-
this.typeName = getTimeType(props.format);
78+
if (isArray(props.format)) {
79+
this.typeName = getTimeType(props.format[0]);
80+
this.format = props.format[0];
81+
if (isFunction(props.format[1])) {
82+
this.formatTransform = props.format[1];
83+
}
84+
}
85+
86+
else {
87+
this.format = props.format;
88+
this.typeName = getTimeType(props.format);
89+
}
90+
7791
this.renderDatepickerItem = this.renderDatepickerItem.bind(this);
7892
this.handleContentTouch = this.handleContentTouch.bind(this);
7993
this.handleContentMouseDown = this.handleContentMouseDown.bind(this);
@@ -311,11 +325,16 @@ class DatePickerItem extends Component<void, Props, State> {
311325
(date < this.props.min || date > this.props.max) ?
312326
'disabled' : '';
313327

328+
let formatDate = TimeUtil.convertDate(date, this.format);
329+
if (this.formatTransform) {
330+
formatDate = this.formatTransform(formatDate);
331+
}
332+
314333
return (
315334
<li
316335
key={index}
317336
className={className}>
318-
{TimeUtil.convertDate(date, this.props.format)}
337+
{formatDate}
319338
</li>
320339
);
321340
}

0 commit comments

Comments
 (0)
Please sign in to comment.