Skip to content

Commit

Permalink
fix locale of default value
Browse files Browse the repository at this point in the history
  • Loading branch information
wuzhao committed Nov 19, 2015
1 parent dbb8eaa commit 8ad6479
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 6 deletions.
60 changes: 54 additions & 6 deletions components/timepicker/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import React from 'react';
import DateTimeFormat from 'gregorian-calendar-format';
import TimePicker from 'rc-time-picker/lib/TimePicker';

// import defaultLocale from './locale';
import objectAssign from 'object-assign';
import defaultLocale from './locale/zh_CN';
import GregorianCalendar from 'gregorian-calendar';
import TimePickerLocale from 'rc-time-picker/lib/locale/zh_CN';

const AntTimepicker = React.createClass({
Expand All @@ -27,6 +29,20 @@ const AntTimepicker = React.createClass({
};
},

getInitialState() {
return {
value: this.parseTimeFromValue(this.props.value || this.props.defaultValue)
};
},

componentWillReceiveProps(nextProps) {
if ('value' in nextProps) {
this.setState({
value: this.parseTimeFromValue(nextProps.value)
});
}
},

/**
* 获得输入框的 className
*/
Expand All @@ -47,33 +63,65 @@ const AntTimepicker = React.createClass({
const defaultValue = this.props.defaultValue;
if (defaultValue) {
return formatter.parse(defaultValue, {
locale: defaultValue.locale,
locale: this.getLocale(),
obeyCount: true,
});
}
return undefined;
},

getLocale() {
// 统一合并为完整的 Locale
let locale = objectAssign({}, defaultLocale, this.props.locale);
locale.lang = objectAssign({}, defaultLocale.lang, this.props.locale.lang);
return locale;
},

getFormatter() {
const formats = this.formats = this.formats || {};
const format = this.props.format;
if (formats[format]) {
return formats[format];
}
formats[format] = new DateTimeFormat(format, this.getLocale().lang.format);
return formats[format];
},

parseTimeFromValue(value) {
if (value) {
if (typeof value === 'string') {
return this.getFormatter().parse(value, {locale: this.getLocale()});
} else if (value instanceof Date) {
let date = new GregorianCalendar(this.getLocale());
date.setTime(value);
return date;
}
} else if (value === null) {
return value;
}
return undefined;
},

render() {
const { format, placeholder, align, disabled, hourOptions, minuteOptions, secondOptions, placement, transitionName } = this.props;
const { placeholder, align, disabled, hourOptions, minuteOptions, secondOptions, placement, transitionName, onChange } = this.props;
const prefixCls = 'ant-timepicker';
const formatter = new DateTimeFormat(format);

return (
<TimePicker
prefixCls={prefixCls}
locale={TimePickerLocale}
formatter={formatter}
formatter={this.getFormatter()}
hourOptions={hourOptions}
minuteOptions={minuteOptions}
secondOptions={secondOptions}
disabled={disabled}
align={align}
placeholder={placeholder}
inputClassName={`ant-input ${this.getSizeClass()}`}
defaultValue={this.getDefaultValue(formatter)}
defaultValue={this.state.value}
placement={placement}
transitionName={transitionName}
onChange={onChange}
/>
);
}
Expand Down
12 changes: 12 additions & 0 deletions components/timepicker/locale/en_US.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import objectAssign from 'object-assign';
import GregorianCalendarLocale from 'gregorian-calendar/lib/locale/en_US';
import TimepickerLocale from 'rc-time-picker/lib/locale/en_US';

// 统一合并为完整的 Locale
let locale = objectAssign({}, GregorianCalendarLocale);
locale.lang = objectAssign({}, TimepickerLocale);

// All settings at:
// https://github.com/ant-design/ant-design/issues/424

export default locale;
12 changes: 12 additions & 0 deletions components/timepicker/locale/zh_CN.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import objectAssign from 'object-assign';
import GregorianCalendarLocale from 'gregorian-calendar/lib/locale/zh_CN';
import TimepickerLocale from 'rc-time-picker/lib/locale/zh_CN';

// 统一合并为完整的 Locale
let locale = objectAssign({}, GregorianCalendarLocale);
locale.lang = objectAssign({}, TimepickerLocale);

// All settings at:
// https://github.com/ant-design/ant-design/issues/424

export default locale;

0 comments on commit 8ad6479

Please sign in to comment.