From d18ecfb67877d8bdfc54e47a214d3afab52b7bda Mon Sep 17 00:00:00 2001 From: Levi Lansing Date: Wed, 27 Sep 2017 23:20:55 -0400 Subject: [PATCH] add focusOnOpen option (#61) --- README.md | 1 + examples/open.js | 7 ++++++- src/Header.jsx | 9 +++++++++ src/Panel.jsx | 4 +++- src/TimePicker.jsx | 5 ++++- 5 files changed, 23 insertions(+), 3 deletions(-) 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} /> ); } -- 2.41.0