Skip to content

Commit

Permalink
Eliminate unnecessary numeric equality checks
Browse files Browse the repository at this point in the history
This commit changes the way numeric equality for number inputs works
such that it compares against `input.valueAsNumber`. This eliminates
quite a bit of branching around numeric equality.
  • Loading branch information
nhunzaker committed Dec 2, 2017
1 parent a44c34f commit eb51e67
Showing 1 changed file with 5 additions and 20 deletions.
25 changes: 5 additions & 20 deletions packages/react-dom/src/client/ReactDOMFiberInput.js
Expand Up @@ -16,7 +16,6 @@ import * as DOMPropertyOperations from './DOMPropertyOperations';
import {getFiberCurrentPropsFromNode} from './ReactDOMComponentTree';
import ReactControlledValuePropTypes from '../shared/ReactControlledValuePropTypes';
import * as inputValueTracking from './inputValueTracking';
import {shouldSetAttribute} from '../shared/DOMProperty.js';

type InputWithWrapperState = HTMLInputElement & {
_wrapperState: {
Expand Down Expand Up @@ -182,19 +181,9 @@ export function updateWrapper(element: Element, props: Object) {
var value = getSafeValue(props.value);

if (value != null) {
if (value === 0 && node.value === '') {
node.value = '0';
// Note: IE9 reports a number inputs as 'text', so check props instead.
} else if (props.type === 'number') {
// Simulate `input.valueAsNumber`. IE9 does not support it
var valueAsNumber = parseFloat(node.value) || 0;

if (
// eslint-disable-next-line
value != valueAsNumber ||
// eslint-disable-next-line
(value == valueAsNumber && node.value != value)
) {
if (props.type === 'number') {
// eslint-disable-next-line
if (node.valueAsNumber !== value && node.value != value) {
node.value = '' + value;
}
} else if (node.value !== '' + value) {
Expand Down Expand Up @@ -307,13 +296,9 @@ function updateNamedCousins(rootNode, props) {
// when the user is inputting text
//
// https://github.com/facebook/react/issues/7253
export function setDefaultValue(
node: InputWithWrapperState,
type: ?string,
value: *,
) {
export function setDefaultValue(node: InputWithWrapperState, type: ?string, value: *) {
if (
// Focused number inputs synchronize on blur. See ChangeEventPlugin.js
// Focused number inputs set on blur. See ChangeEventPlugin.js
type !== 'number' ||
node.ownerDocument.activeElement !== node
) {
Expand Down

0 comments on commit eb51e67

Please sign in to comment.