From 01b4b23118e6a55d5cbd93a27b7c99f49e7521e0 Mon Sep 17 00:00:00 2001 From: Andreas Svensson Date: Mon, 23 Dec 2013 23:47:21 +0100 Subject: [PATCH] Polyfill/normalize autoFocus across all browsers --- src/dom/DefaultDOMPropertyConfig.js | 3 ++- src/dom/components/AutoFocusMixin.js | 30 ++++++++++++++++++++++++++ src/dom/components/ReactDOMButton.js | 2 ++ src/dom/components/ReactDOMInput.js | 3 ++- src/dom/components/ReactDOMSelect.js | 3 ++- src/dom/components/ReactDOMTextarea.js | 3 ++- 6 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 src/dom/components/AutoFocusMixin.js diff --git a/src/dom/DefaultDOMPropertyConfig.js b/src/dom/DefaultDOMPropertyConfig.js index 26375be632..c9ea8d6771 100644 --- a/src/dom/DefaultDOMPropertyConfig.js +++ b/src/dom/DefaultDOMPropertyConfig.js @@ -45,7 +45,8 @@ var DefaultDOMPropertyConfig = { alt: null, async: HAS_BOOLEAN_VALUE, autoComplete: null, - autoFocus: HAS_BOOLEAN_VALUE, + // autoFocus is polyfilled/normalized by AutoFocusMixin + // autoFocus: HAS_BOOLEAN_VALUE, autoPlay: HAS_BOOLEAN_VALUE, cellPadding: null, cellSpacing: null, diff --git a/src/dom/components/AutoFocusMixin.js b/src/dom/components/AutoFocusMixin.js new file mode 100644 index 0000000000..b200a759a5 --- /dev/null +++ b/src/dom/components/AutoFocusMixin.js @@ -0,0 +1,30 @@ +/** + * Copyright 2013 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * @providesModule AutoFocusMixin + * @typechecks static-only + */ + +"use strict"; + +var AutoFocusMixin = { + componentDidMount: function() { + if (this.props.autoFocus) { + this.getDOMNode().focus(); + } + } +}; + +module.exports = AutoFocusMixin; diff --git a/src/dom/components/ReactDOMButton.js b/src/dom/components/ReactDOMButton.js index 348c838db1..593a9cf196 100644 --- a/src/dom/components/ReactDOMButton.js +++ b/src/dom/components/ReactDOMButton.js @@ -18,6 +18,7 @@ "use strict"; +var AutoFocusMixin = require('AutoFocusMixin'); var ReactCompositeComponent = require('ReactCompositeComponent'); var ReactDOM = require('ReactDOM'); @@ -44,6 +45,7 @@ var mouseListenerNames = keyMirror({ * when `disabled` is set. */ var ReactDOMButton = ReactCompositeComponent.createClass({ + mixins: [AutoFocusMixin], render: function() { var props = {}; diff --git a/src/dom/components/ReactDOMInput.js b/src/dom/components/ReactDOMInput.js index 53ede21bc9..f1778b9790 100644 --- a/src/dom/components/ReactDOMInput.js +++ b/src/dom/components/ReactDOMInput.js @@ -20,6 +20,7 @@ var DOMPropertyOperations = require('DOMPropertyOperations'); var LinkedValueMixin = require('LinkedValueMixin'); +var AutoFocusMixin = require('AutoFocusMixin'); var ReactCompositeComponent = require('ReactCompositeComponent'); var ReactDOM = require('ReactDOM'); var ReactMount = require('ReactMount'); @@ -49,7 +50,7 @@ var instancesByReactID = {}; * @see http://www.w3.org/TR/2012/WD-html5-20121025/the-input-element.html */ var ReactDOMInput = ReactCompositeComponent.createClass({ - mixins: [LinkedValueMixin], + mixins: [LinkedValueMixin, AutoFocusMixin], getInitialState: function() { var defaultValue = this.props.defaultValue; diff --git a/src/dom/components/ReactDOMSelect.js b/src/dom/components/ReactDOMSelect.js index f42894a3bb..fff960bfa1 100644 --- a/src/dom/components/ReactDOMSelect.js +++ b/src/dom/components/ReactDOMSelect.js @@ -19,6 +19,7 @@ "use strict"; var LinkedValueMixin = require('LinkedValueMixin'); +var AutoFocusMixin = require('AutoFocusMixin'); var ReactCompositeComponent = require('ReactCompositeComponent'); var ReactDOM = require('ReactDOM'); @@ -99,7 +100,7 @@ function updateOptions() { * selected. */ var ReactDOMSelect = ReactCompositeComponent.createClass({ - mixins: [LinkedValueMixin], + mixins: [LinkedValueMixin, AutoFocusMixin], propTypes: { defaultValue: selectValueType, diff --git a/src/dom/components/ReactDOMTextarea.js b/src/dom/components/ReactDOMTextarea.js index 7289666119..9871ac7470 100644 --- a/src/dom/components/ReactDOMTextarea.js +++ b/src/dom/components/ReactDOMTextarea.js @@ -20,6 +20,7 @@ var DOMPropertyOperations = require('DOMPropertyOperations'); var LinkedValueMixin = require('LinkedValueMixin'); +var AutoFocusMixin = require('AutoFocusMixin'); var ReactCompositeComponent = require('ReactCompositeComponent'); var ReactDOM = require('ReactDOM'); @@ -45,7 +46,7 @@ var textarea = ReactDOM.textarea; * `defaultValue` if specified, or the children content (deprecated). */ var ReactDOMTextarea = ReactCompositeComponent.createClass({ - mixins: [LinkedValueMixin], + mixins: [LinkedValueMixin, AutoFocusMixin], getInitialState: function() { var defaultValue = this.props.defaultValue;