aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/module/Header.jsx
diff options
context:
space:
mode:
author崖鹰 <zhao.wuz@alipay.com>2015-11-13 11:33:48 +0800
committer崖鹰 <zhao.wuz@alipay.com>2015-11-13 11:33:48 +0800
commit02de449a0474765a4796fa607e7e3922252f574f (patch)
treedc37faf2f610343112ea1fc3707ad188092bd031 /src/module/Header.jsx
parent1f336fabc9135ac971e53d9c2ae407db69b8f096 (diff)
downloadtime-picker-02de449a0474765a4796fa607e7e3922252f574f.tar.gz
time-picker-02de449a0474765a4796fa607e7e3922252f574f.tar.zst
time-picker-02de449a0474765a4796fa607e7e3922252f574f.zip
release 0.1.0
Diffstat (limited to 'src/module/Header.jsx')
-rw-r--r--src/module/Header.jsx139
1 files changed, 139 insertions, 0 deletions
diff --git a/src/module/Header.jsx b/src/module/Header.jsx
new file mode 100644
index 0000000..f7e443f
--- /dev/null
+++ b/src/module/Header.jsx
@@ -0,0 +1,139 @@
1import React, {PropTypes} from 'react';
2
3const Header = React.createClass({
4 propTypes: {
5 formatter: PropTypes.object,
6 prefixCls: PropTypes.string,
7 gregorianTimepickerLocale: PropTypes.object,
8 locale: PropTypes.object,
9 disabledDate: PropTypes.func,
10 placeholder: PropTypes.string,
11 value: PropTypes.object,
12 hourOptions: PropTypes.array,
13 minuteOptions: PropTypes.array,
14 secondOptions: PropTypes.array,
15 onChange: PropTypes.func,
16 onClear: PropTypes.func,
17 showClear: PropTypes.bool,
18 },
19
20 getInitialState() {
21 const value = this.props.value;
22 return {
23 str: value && this.props.formatter.format(value) || '',
24 invalid: false,
25 };
26 },
27
28 componentWillReceiveProps(nextProps) {
29 const value = this.formatValue(nextProps.value);
30 this.setState({
31 str: value && nextProps.formatter.format(value) || '',
32 invalid: false,
33 });
34 },
35
36 onInputChange(event) {
37 const str = event.target.value;
38 this.setState({
39 str,
40 });
41 let value = null;
42 const {formatter, gregorianTimepickerLocale, hourOptions, minuteOptions, secondOptions, onChange} = this.props;
43
44 if (str) {
45 const originalValue = this.props.value;
46 try {
47 value = formatter.parse(str, {
48 locale: gregorianTimepickerLocale,
49 obeyCount: true,
50 });
51 value = this.formatValue(value);
52 } catch (ex) {
53 this.setState({
54 invalid: true,
55 });
56 return;
57 }
58
59 if (value) {
60 if (
61 hourOptions.indexOf(value.fields[4]) < 0 ||
62 minuteOptions.indexOf(value.fields[5]) < 0 ||
63 secondOptions.indexOf(value.fields[6]) < 0
64 ) {
65 this.setState({
66 invalid: true,
67 });
68 return;
69 }
70
71 if (originalValue && value) {
72 if (
73 originalValue.fields[4] !== value.fields[4] ||
74 originalValue.fields[5] !== value.fields[5] ||
75 originalValue.fields[6] !== value.fields[6]
76 ) {
77 onChange(value);
78 }
79 } else if (originalValue !== value) {
80 onChange(value);
81 }
82 } else {
83 this.setState({
84 invalid: true,
85 });
86 return;
87 }
88 } else {
89 onChange(null);
90 }
91
92 this.setState({
93 invalid: false,
94 });
95 },
96
97 onClear() {
98 this.setState({str: ''});
99 this.props.onClear();
100 },
101
102 getClearButton() {
103 const { locale, prefixCls, showClear } = this.props;
104 if (!showClear) {
105 return null;
106 }
107 return <a className={`${prefixCls}-clear-btn`} role="button" title={locale.clear} onMouseDown={this.onClear} />;
108 },
109
110 getInput() {
111 const { prefixCls, placeholder } = this.props;
112 const { invalid, str } = this.state;
113 const invalidClass = invalid ? `${prefixCls}-input-invalid` : '';
114 return <input className={`${prefixCls}-input ${invalidClass}`} value={str} placeholder={placeholder} onChange={this.onInputChange} />;
115 },
116
117 formatValue(value) {
118 const newValue = this.props.value.clone();
119 if (!value) {
120 return newValue;
121 }
122 newValue.fields[4] = value.fields[4];
123 newValue.fields[5] = value.fields[5];
124 newValue.fields[6] = value.fields[6];
125 return newValue;
126 },
127
128 render() {
129 const { prefixCls } = this.props;
130 return (
131 <div className={`${prefixCls}-input-wrap`}>
132 {this.getInput()}
133 {this.getClearButton()}
134 </div>
135 );
136 },
137});
138
139export default Header;