1 import React, {PropTypes} from 'react';
2 import ReactDOM from 'react-dom';
3 import Trigger from 'rc-trigger';
4 import {createChainedFunction} from 'rc-util';
5 import placements from './util/placements';
6 import CommonMixin from './mixin/CommonMixin';
11 function refFn(field, component) {
12 this[field] = component;
15 const Picker = React.createClass({
17 prefixCls: PropTypes.string,
18 panel: PropTypes.element,
19 children: PropTypes.func,
20 disabled: PropTypes.bool,
21 value: PropTypes.object,
23 align: PropTypes.object,
24 placement: PropTypes.any,
25 transitionName: PropTypes.string,
26 onChange: PropTypes.func,
27 onOpen: PropTypes.func,
28 onClose: PropTypes.func,
31 mixins: [CommonMixin],
37 placement: 'bottomLeft',
45 this.savePanelRef = refFn.bind(this, 'panelInstance');
46 const { open, value } = this.props;
47 return { open, value };
50 componentWillReceiveProps(nextProps) {
51 const { value, open } = nextProps;
52 if (value !== undefined) {
53 this.setState({value});
55 if (open !== undefined) {
56 this.setState({open});
60 onPanelChange(value) {
61 const props = this.props;
65 props.onChange(value);
69 this.setOpen(false, this.focus);
72 onVisibleChange(open) {
73 this.setOpen(open, () => {
75 ReactDOM.findDOMNode(this.panelInstance).focus();
81 const panel = this.props.panel;
83 ref: this.savePanelRef,
84 defaultValue: this.state.value || panel.props.defaultValue,
85 onChange: createChainedFunction(panel.props.onChange, this.onPanelChange),
86 onClear: createChainedFunction(panel.props.onClear, this.onPanelClear),
89 return React.cloneElement(panel, extraProps);
92 setOpen(open, callback) {
93 const {onOpen, onClose} = this.props;
94 if (this.state.open !== open) {
110 if (!this.state.open) {
111 ReactDOM.findDOMNode(this).focus();
116 const state = this.state;
117 const props = this.props;
118 const { prefixCls, placement, align, disabled, transitionName, children } = props;
121 prefixCls={prefixCls}
122 popup={this.getPanelElement()}
124 builtinPlacements={placements}
125 popupPlacement={placement}
126 action={disabled ? [] : ['click']}
128 popupTransitionName={transitionName}
129 popupVisible={state.open}
130 onPopupVisibleChange={this.onVisibleChange}
132 <span className={`${prefixCls}-picker`}>
133 {children(state, props)}
140 export default Picker;