]>
Commit | Line | Data |
---|---|---|
3ab3a128 | 1 | import React, { Component } from 'react'; |
2 | import PropTypes from 'prop-types'; | |
4984ed85 | 3 | import moment from 'moment'; |
4 | ||
3ab3a128 | 5 | class Header extends Component { |
6 | static propTypes = { | |
4984ed85 | 7 | format: PropTypes.string, |
8 | prefixCls: PropTypes.string, | |
9 | disabledDate: PropTypes.func, | |
10 | placeholder: PropTypes.string, | |
11 | clearText: PropTypes.string, | |
12 | value: PropTypes.object, | |
13 | hourOptions: PropTypes.array, | |
14 | minuteOptions: PropTypes.array, | |
15 | secondOptions: PropTypes.array, | |
16 | disabledHours: PropTypes.func, | |
17 | disabledMinutes: PropTypes.func, | |
18 | disabledSeconds: PropTypes.func, | |
19 | onChange: PropTypes.func, | |
20 | onClear: PropTypes.func, | |
21 | onEsc: PropTypes.func, | |
22 | allowEmpty: PropTypes.bool, | |
23 | defaultOpenValue: PropTypes.object, | |
24 | currentSelectPanel: PropTypes.string, | |
d18ecfb6 | 25 | focusOnOpen: PropTypes.bool, |
0e4fd162 | 26 | onKeyDown: PropTypes.func, |
3ab3a128 | 27 | }; |
4984ed85 | 28 | |
3ab3a128 | 29 | constructor(props) { |
30 | super(props); | |
31 | const { value, format } = props; | |
32 | this.state = { | |
4984ed85 | 33 | str: value && value.format(format) || '', |
34 | invalid: false, | |
35 | }; | |
3ab3a128 | 36 | } |
4984ed85 | 37 | |
d18ecfb6 LL |
38 | componentDidMount() { |
39 | if (this.props.focusOnOpen) { | |
40 | // Wait one frame for the panel to be positioned before focusing | |
41 | const requestAnimationFrame = (window.requestAnimationFrame || window.setTimeout); | |
1c72daf0 LL |
42 | requestAnimationFrame(() => { |
43 | this.refs.input.focus(); | |
44 | this.refs.input.select(); | |
45 | }); | |
d18ecfb6 LL |
46 | } |
47 | } | |
48 | ||
4984ed85 | 49 | componentWillReceiveProps(nextProps) { |
50 | const { value, format } = nextProps; | |
51 | this.setState({ | |
52 | str: value && value.format(format) || '', | |
53 | invalid: false, | |
54 | }); | |
3ab3a128 | 55 | } |
4984ed85 | 56 | |
3ab3a128 | 57 | onInputChange = (event) => { |
4984ed85 | 58 | const str = event.target.value; |
59 | this.setState({ | |
60 | str, | |
61 | }); | |
62 | const { | |
63 | format, hourOptions, minuteOptions, secondOptions, | |
64 | disabledHours, disabledMinutes, | |
65 | disabledSeconds, onChange, allowEmpty, | |
66 | } = this.props; | |
67 | ||
68 | if (str) { | |
69 | const originalValue = this.props.value; | |
70 | const value = this.getProtoValue().clone(); | |
71 | const parsed = moment(str, format, true); | |
72 | if (!parsed.isValid()) { | |
73 | this.setState({ | |
74 | invalid: true, | |
75 | }); | |
76 | return; | |
77 | } | |
78 | value.hour(parsed.hour()).minute(parsed.minute()).second(parsed.second()); | |
79 | ||
80 | // if time value not allowed, response warning. | |
81 | if ( | |
82 | hourOptions.indexOf(value.hour()) < 0 || | |
83 | minuteOptions.indexOf(value.minute()) < 0 || | |
84 | secondOptions.indexOf(value.second()) < 0 | |
85 | ) { | |
86 | this.setState({ | |
87 | invalid: true, | |
88 | }); | |
89 | return; | |
90 | } | |
91 | ||
92 | // if time value is disabled, response warning. | |
93 | const disabledHourOptions = disabledHours(); | |
94 | const disabledMinuteOptions = disabledMinutes(value.hour()); | |
95 | const disabledSecondOptions = disabledSeconds(value.hour(), value.minute()); | |
96 | if ( | |
97 | (disabledHourOptions && disabledHourOptions.indexOf(value.hour()) >= 0) || | |
98 | (disabledMinuteOptions && disabledMinuteOptions.indexOf(value.minute()) >= 0) || | |
99 | (disabledSecondOptions && disabledSecondOptions.indexOf(value.second()) >= 0) | |
100 | ) { | |
101 | this.setState({ | |
102 | invalid: true, | |
103 | }); | |
104 | return; | |
105 | } | |
106 | ||
107 | if (originalValue) { | |
108 | if ( | |
109 | originalValue.hour() !== value.hour() || | |
110 | originalValue.minute() !== value.minute() || | |
111 | originalValue.second() !== value.second() | |
112 | ) { | |
113 | // keep other fields for rc-calendar | |
114 | const changedValue = originalValue.clone(); | |
115 | changedValue.hour(value.hour()); | |
116 | changedValue.minute(value.minute()); | |
117 | changedValue.second(value.second()); | |
118 | onChange(changedValue); | |
119 | } | |
120 | } else if (originalValue !== value) { | |
121 | onChange(value); | |
122 | } | |
123 | } else if (allowEmpty) { | |
124 | onChange(null); | |
125 | } else { | |
126 | this.setState({ | |
127 | invalid: true, | |
128 | }); | |
129 | return; | |
130 | } | |
131 | ||
132 | this.setState({ | |
133 | invalid: false, | |
134 | }); | |
3ab3a128 | 135 | } |
4984ed85 | 136 | |
3ab3a128 | 137 | onKeyDown = (e) => { |
0e4fd162 | 138 | const { onEsc, onKeyDown } = this.props; |
4984ed85 | 139 | if (e.keyCode === 27) { |
0e4fd162 | 140 | onEsc(); |
4984ed85 | 141 | } |
0e4fd162 | 142 | |
143 | onKeyDown(e); | |
3ab3a128 | 144 | } |
4984ed85 | 145 | |
3ab3a128 | 146 | onClear = () => { |
4984ed85 | 147 | this.setState({ str: '' }); |
148 | this.props.onClear(); | |
3ab3a128 | 149 | } |
4984ed85 | 150 | |
151 | getClearButton() { | |
152 | const { prefixCls, allowEmpty } = this.props; | |
153 | if (!allowEmpty) { | |
154 | return null; | |
155 | } | |
156 | return (<a | |
157 | className={`${prefixCls}-clear-btn`} | |
158 | role="button" | |
159 | title={this.props.clearText} | |
160 | onMouseDown={this.onClear} | |
161 | />); | |
3ab3a128 | 162 | } |
4984ed85 | 163 | |
164 | getProtoValue() { | |
165 | return this.props.value || this.props.defaultOpenValue; | |
3ab3a128 | 166 | } |
4984ed85 | 167 | |
168 | getInput() { | |
169 | const { prefixCls, placeholder } = this.props; | |
170 | const { invalid, str } = this.state; | |
171 | const invalidClass = invalid ? `${prefixCls}-input-invalid` : ''; | |
f429b4a7 | 172 | return ( |
173 | <input | |
174 | className={`${prefixCls}-input ${invalidClass}`} | |
175 | ref="input" | |
176 | onKeyDown={this.onKeyDown} | |
177 | value={str} | |
178 | placeholder={placeholder} | |
179 | onChange={this.onInputChange} | |
180 | /> | |
181 | ); | |
3ab3a128 | 182 | } |
4984ed85 | 183 | |
184 | render() { | |
185 | const { prefixCls } = this.props; | |
186 | return ( | |
187 | <div className={`${prefixCls}-input-wrap`}> | |
188 | {this.getInput()} | |
189 | {this.getClearButton()} | |
190 | </div> | |
191 | ); | |
3ab3a128 | 192 | } |
193 | } | |
4984ed85 | 194 | |
195 | export default Header; |