1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
import React, {PropTypes} from 'react';
import ReactDOM from 'react-dom';
import Trigger from 'rc-trigger';
import {createChainedFunction} from 'rc-util';
import Panel from './module/Panel';
import placements from './util/placements';
import CommonMixin from './mixin/CommonMixin';
function noop() {
}
function refFn(field, component) {
this[field] = component;
}
const Picker = React.createClass({
propTypes: {
prefixCls: PropTypes.string,
inputClassName: PropTypes.string,
locale: PropTypes.object,
children: PropTypes.func,
disabled: PropTypes.bool,
defaultValue: PropTypes.object,
open: PropTypes.bool,
align: PropTypes.object,
placement: PropTypes.any,
transitionName: PropTypes.string,
placeholder: PropTypes.string,
formatter: PropTypes.object,
hourOptions: PropTypes.array,
minuteOptions: PropTypes.array,
secondOptions: PropTypes.array,
onChange: PropTypes.func,
onOpen: PropTypes.func,
onClose: PropTypes.func,
},
mixins: [CommonMixin],
getDefaultProps() {
return {
open: false,
align: {},
placement: 'bottomLeft',
onChange: noop,
onOpen: noop,
onClose: noop,
};
},
getInitialState() {
this.savePanelRef = refFn.bind(this, 'panelInstance');
const { open, value } = this.props;
return { open, value };
},
componentWillReceiveProps(nextProps) {
const { defaultValue, open } = nextProps;
if (defaultValue !== undefined) {
this.setState({value: defaultValue});
}
if (open !== undefined) {
this.setState({open});
}
},
onPanelChange(value) {
this.setState({
value: value,
});
this.props.onChange(value);
},
onPanelClear() {
this.setState({
value: '',
});
this.setOpen(false);
},
onVisibleChange(open) {
this.setOpen(open, () => {
if (open) {
ReactDOM.findDOMNode(this.refs.picker).blur();
ReactDOM.findDOMNode(this.panelInstance).focus();
}
});
},
getPanel() {
const { prefixCls, defaultValue, locale, formatter, placeholder, hourOptions, minuteOptions, secondOptions } = this.props;
return (
<Panel
prefixCls={prefixCls}
defaultValue={defaultValue}
locale={locale}
formatter={formatter}
placeholder={placeholder}
hourOptions={hourOptions}
minuteOptions={minuteOptions}
secondOptions={secondOptions}
/>
);
},
getPanelElement() {
const panel = this.getPanel();
const extraProps = {
ref: this.savePanelRef,
defaultValue: this.state.value || panel.props.defaultValue,
onChange: createChainedFunction(panel.props.onChange, this.onPanelChange),
onClear: createChainedFunction(panel.props.onClear, this.onPanelClear),
};
return React.cloneElement(panel, extraProps);
},
setOpen(open, callback) {
const {onOpen, onClose} = this.props;
if (this.state.open !== open) {
this.setState({
open: open,
}, callback);
const event = {
open: open,
};
if (open) {
onOpen(event);
} else {
onClose(event);
}
}
},
render() {
const { prefixCls, placeholder, placement, align, disabled, transitionName, children, formatter, inputClassName } = this.props;
const { open, value } = this.state;
return (
<Trigger
prefixCls={`${prefixCls}-picker-container`}
popup={this.getPanelElement()}
popupAlign={align}
builtinPlacements={placements}
popupPlacement={placement}
action={disabled ? [] : ['click']}
destroyPopupOnHide
popupTransitionName={transitionName}
popupVisible={open}
onPopupVisibleChange={this.onVisibleChange}
>
<span className={`${prefixCls}-picker`}>
<input className={inputClassName} ref="picker" type="text" placeholder={placeholder} readOnly disabled={disabled} value={value && formatter.format(value)} />
<span className={`${prefixCls}-picker-icon`} />
</span>
</Trigger>
);
},
});
export default Picker;
|