import*as React from"react";import*as ReactDOM from"react-dom";import createReactClass from"create-react-class";import*as PropTypes from"prop-types";import createChainedFunction from"rc-util/es/createChainedFunction";import KeyCode from"rc-util/es/KeyCode";import placements from"./picker/placements";import Trigger from"rc-trigger";function noop(){}function refFn(e,n){this[e]=n}var Picker=createReactClass({displayName:"Picker",propTypes:{animation:PropTypes.oneOfType([PropTypes.func,PropTypes.string]),disabled:PropTypes.bool,transitionName:PropTypes.string,onChange:PropTypes.func,onOpenChange:PropTypes.func,children:PropTypes.func,getCalendarContainer:PropTypes.func,calendar:PropTypes.element,style:PropTypes.object,open:PropTypes.bool,defaultOpen:PropTypes.bool,prefixCls:PropTypes.string,placement:PropTypes.any,value:PropTypes.oneOfType([PropTypes.object,PropTypes.array]),defaultValue:PropTypes.oneOfType([PropTypes.object,PropTypes.array]),align:PropTypes.object},getDefaultProps:function(){return{prefixCls:"rc-calendar-picker",style:{},align:{},placement:"bottomLeft",defaultOpen:!1,onChange:noop,onOpenChange:noop}},getInitialState:function(){var e=this.props,n=void 0;n="open"in e?e.open:e.defaultOpen;var t=e.value||e.defaultValue;return this.saveCalendarRef=refFn.bind(this,"calendarInstance"),{open:n,value:t}},componentWillReceiveProps:function(e){var n=e.value,t=e.open;"value"in e&&this.setState({value:n}),void 0!==t&&this.setState({open:t})},componentDidUpdate:function(e,n){!n.open&&this.state.open&&(this.focusTimeout=setTimeout(this.focusCalendar,0,this))},componentWillUnmount:function(){clearTimeout(this.focusTimeout)},onCalendarKeyDown:function(e){e.keyCode===KeyCode.ESC&&(e.stopPropagation(),this.close(this.focus))},onCalendarSelect:function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},t=this.props;"value"in t||this.setState({value:e}),("keyboard"===n.source||!t.calendar.props.timePicker&&"dateInput"!==n.source||"todayButton"===n.source)&&this.close(this.focus),t.onChange(e)},onKeyDown:function(e){e.keyCode!==KeyCode.DOWN||this.state.open||(this.open(),e.preventDefault())},onCalendarOk:function(){this.close(this.focus)},onCalendarClear:function(){this.close(this.focus)},onVisibleChange:function(e){this.setOpen(e)},getCalendarElement:function(){var e=this.props,n=this.state,t=e.calendar.props,o=n.value,a=o,i={ref:this.saveCalendarRef,defaultValue:a||t.defaultValue,selectedValue:o,onKeyDown:this.onCalendarKeyDown,onOk:createChainedFunction(t.onOk,this.onCalendarOk),onSelect:createChainedFunction(t.onSelect,this.onCalendarSelect),onClear:createChainedFunction(t.onClear,this.onCalendarClear)};return React.cloneElement(e.calendar,i)},setOpen:function(e,n){var t=this.props.onOpenChange;this.state.open!==e&&("open"in this.props||this.setState({open:e},n),t(e))},open:function(e){this.setOpen(!0,e)},close:function(e){this.setOpen(!1,e)},focus:function(){this.state.open||ReactDOM.findDOMNode(this).focus()},focusCalendar:function(){this.state.open&&null!==this.calendarInstance&&this.calendarInstance.focus()},render:function(){var e=this.props,n=e.prefixCls,t=e.placement,o=e.style,a=e.getCalendarContainer,i=e.align,p=e.animation,s=e.disabled,r=e.transitionName,c=e.children,l=this.state;return React.createElement(Trigger,{popup:this.getCalendarElement(),popupAlign:i,builtinPlacements:placements,popupPlacement:t,action:s&&!l.open?[]:["click"],destroyPopupOnHide:!0,getPopupContainer:a,popupStyle:o,popupAnimation:p,popupTransitionName:r,popupVisible:l.open,onPopupVisibleChange:this.onVisibleChange,prefixCls:n},React.cloneElement(c(l,e),{onKeyDown:this.onKeyDown}))}});export default Picker;