Skip to content

Commit

Permalink
✨ [feature] map month number to month name(lanjingling0510#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
lanjingling0510 committed May 12, 2018
1 parent ff51317 commit b6d6b5e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
17 changes: 16 additions & 1 deletion examples/basic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ import DatePicker from '../../lib/index';
}

render() {
const monthMap = {
'01': 'Jan',
'02': 'Feb',
'03': 'Mar',
'04': 'Apr',
'05': 'May',
'06': 'Jun',
'07': 'Jul',
'08': 'Aug',
'09': 'Sep',
'10': 'Oct',
'11': 'Nov',
'12': 'Dec',
};

return (
<div className="App">
<p className="select-time ">
Expand Down Expand Up @@ -60,7 +75,7 @@ import DatePicker from '../../lib/index';
<DatePicker
value={this.state.time}
dateSteps={[1, 1, 5]}
dateFormat={['hh', 'mm', 'ss']}
dateFormat={['YYYY', ['MM', (month) => monthMap[month]], 'DD']}
theme={this.state.theme}
isOpen={this.state.isOpen}
onSelect={this.handleSelect}
Expand Down
25 changes: 22 additions & 3 deletions lib/DatePickerItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const MIDDLE_INDEX = Math.floor(DATE_LENGTH / 2); // 日期数组中间值
const MIDDLE_Y = - DATE_HEIGHT * MIDDLE_INDEX; // translateY值

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

/**
* 根据格式获取时间滑动的类别
Expand Down Expand Up @@ -44,7 +46,7 @@ type Props = {
value: Object,
min: Object,
max: Object,
format: string,
format: string | Array<*>,
step: number,
onSelect: Function,
}
Expand Down Expand Up @@ -73,7 +75,19 @@ class DatePickerItem extends Component<void, Props, State> {
};

// 设置时间选择器单元的类别
this.typeName = getTimeType(props.format);
if (isArray(props.format)) {
this.typeName = getTimeType(props.format[0]);
this.format = props.format[0];
if (isFunction(props.format[1])) {
this.formatTransform = props.format[1];
}
}

else {
this.format = props.format;
this.typeName = getTimeType(props.format);
}

this.renderDatepickerItem = this.renderDatepickerItem.bind(this);
this.handleContentTouch = this.handleContentTouch.bind(this);
this.handleContentMouseDown = this.handleContentMouseDown.bind(this);
Expand Down Expand Up @@ -311,11 +325,16 @@ class DatePickerItem extends Component<void, Props, State> {
(date < this.props.min || date > this.props.max) ?
'disabled' : '';

let formatDate = TimeUtil.convertDate(date, this.format);
if (this.formatTransform) {
formatDate = this.formatTransform(formatDate);
}

return (
<li
key={index}
className={className}>
{TimeUtil.convertDate(date, this.props.format)}
{formatDate}
</li>
);
}
Expand Down

0 comments on commit b6d6b5e

Please sign in to comment.