@samitouri / QOS-React / commits / 309215fc40

[flow] isTextInputElement (#7075)

Summary: I had to cast into any because flow doesn't think that checking the lowercase version of nodeName is a valid way to refine the variable from HTMLElement to HTMLInputElement. I'm also not confident enough in changing the implementation to an instanceof HTMLInputElement to please flow. It also takes care of the null check in the process. The `nodeName &&` condition wasn't useful since the two branches are checking it against concrete values and actually makes the type different since nodeName is not a boolean per se. I replaced them with if conditions to make it clearer what it actually did instead of doing boolean logic tricks. It is unclear why I had to type supportedInputTypes, see this internal post for a discussion: https://www.facebook.com/groups/flowtype/permalink/1084168611631753/ The only difference in behavior is that I now explicitely convert to boolean the object dereference via `!!`. Test Plan: npm run flow Careful inspection of the code Reviewers: @zpao @spicyj

Christopher Chedeau committed Jul 5, 2016 at 10:22 UTC 309215fc40e81354bd06d11e108866f87156fb55
1 file changed +13 -6
src/renderers/shared/utils/isTextInputElement.js
+13 -6
@@ -7,6 +7,7 @@
7 * of patent rights can be found in the PATENTS file in the same directory.
8 *
9 * @providesModule isTextInputElement
10 + * @flow
11 */
12
13 'use strict';
@@ -14,7 +15,7 @@
15 /**
16 * @see http://www.whatwg.org/specs/web-apps/current-work/multipage/the-input-element.html#input-type-attr-summary
17 */
17 -var supportedInputTypes = {
18 +var supportedInputTypes: {[key: string]: true | void} = {
19 'color': true,
20 'date': true,
21 'datetime': true,
@@ -32,12 +33,18 @@ var supportedInputTypes = {
33 'week': true,
34 };
35
35 -function isTextInputElement(elem) {
36 +function isTextInputElement(elem: ?HTMLElement): bool {
37 var nodeName = elem && elem.nodeName && elem.nodeName.toLowerCase();
37 - return nodeName && (
38 - (nodeName === 'input' && supportedInputTypes[elem.type]) ||
39 - nodeName === 'textarea'
40 - );
38 +
39 + if (nodeName === 'input') {
40 + return !!supportedInputTypes[((elem: any): HTMLInputElement).type];
41 + }
42 +
43 + if (nodeName === 'textarea') {
44 + return true;
45 + }
46 +
47 + return false;
48 }
49
50 module.exports = isTextInputElement;