From: Levi Lansing Date: Thu, 28 Sep 2017 03:20:55 +0000 (-0400) Subject: add focusOnOpen option (#61) X-Git-Tag: 3.0.0~5^2~3 X-Git-Url: https://git.immae.eu/?a=commitdiff_plain;h=d18ecfb67877d8bdfc54e47a214d3afab52b7bda;p=github%2Ffretlink%2Ftime-picker.git add focusOnOpen option (#61) --- diff --git a/README.md b/README.md index a2758b9..0698d7d 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ API | name | String | - | sets the name of the generated input | | onOpen | Function({ open }) | | when TimePicker panel is opened | | onClose | Function({ open }) | | when TimePicker panel is opened | +| focusOnOpen | Boolean | false | automatically focus the input when the picker opens | ## Test Case diff --git a/examples/open.js b/examples/open.js index 8e4f5de..12d8a8d 100644 --- a/examples/open.js +++ b/examples/open.js @@ -22,7 +22,12 @@ class App extends React.Component { return (
- +
); } diff --git a/src/Header.jsx b/src/Header.jsx index 91e8549..9e80a9c 100644 --- a/src/Header.jsx +++ b/src/Header.jsx @@ -22,6 +22,7 @@ class Header extends Component { allowEmpty: PropTypes.bool, defaultOpenValue: PropTypes.object, currentSelectPanel: PropTypes.string, + focusOnOpen: PropTypes.bool, }; constructor(props) { @@ -33,6 +34,14 @@ class Header extends Component { }; } + componentDidMount() { + if (this.props.focusOnOpen) { + // Wait one frame for the panel to be positioned before focusing + const requestAnimationFrame = (window.requestAnimationFrame || window.setTimeout); + requestAnimationFrame(() => this.refs.input.focus()); + } + } + componentWillReceiveProps(nextProps) { const { value, format } = nextProps; this.setState({ diff --git a/src/Panel.jsx b/src/Panel.jsx index 1953ad4..deeba52 100644 --- a/src/Panel.jsx +++ b/src/Panel.jsx @@ -40,6 +40,7 @@ class Panel extends Component { onClear: PropTypes.func, use12Hours: PropTypes.bool, addon: PropTypes.func, + focusOnOpen: PropTypes.bool, }; static defaultProps = { @@ -89,7 +90,7 @@ class Panel extends Component { const { prefixCls, className, placeholder, disabledHours, disabledMinutes, disabledSeconds, hideDisabledOptions, allowEmpty, showHour, showMinute, showSecond, - format, defaultOpenValue, clearText, onEsc, addon, use12Hours, onClear, + format, defaultOpenValue, clearText, onEsc, addon, use12Hours, onClear, focusOnOpen, } = this.props; const { value, currentSelectPanel, @@ -122,6 +123,7 @@ class Panel extends Component { onChange={this.onChange} onClear={onClear} allowEmpty={allowEmpty} + focusOnOpen={focusOnOpen} /> ); }