Skip to content

Commit 82443f7

Browse files
committed
Detect all valid selection-capable input types
1 parent 39a9589 commit 82443f7

File tree

2 files changed

+54
-28
lines changed

2 files changed

+54
-28
lines changed

packages/react-dom/src/__tests__/ReactInputSelection-test.js

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,37 +36,50 @@ describe('ReactInputSelection', () => {
3636
expect(ReactInputSelection.hasSelectionCapabilities(textarea)).toBe(true);
3737
});
3838

39-
it('returns true for text inputs', () => {
40-
var inputText = document.createElement('input');
39+
it('returns true for inputs that can support text selection ranges', () => {
40+
[
41+
'date',
42+
'datetime-local',
43+
'email',
44+
'month',
45+
'number',
46+
'password',
47+
'search',
48+
'tel',
49+
'text',
50+
'time',
51+
'url',
52+
'week',
53+
].forEach(type => {
54+
const input = document.createElement('input');
55+
input.type = type;
56+
expect(ReactInputSelection.hasSelectionCapabilities(input)).toBe(true);
57+
});
58+
4159
var inputReadOnly = document.createElement('input');
4260
inputReadOnly.readOnly = 'true';
43-
var inputNumber = document.createElement('input');
44-
inputNumber.type = 'number';
45-
var inputEmail = document.createElement('input');
46-
inputEmail.type = 'email';
47-
var inputPassword = document.createElement('input');
48-
inputPassword.type = 'password';
49-
var inputHidden = document.createElement('input');
50-
inputHidden.type = 'hidden';
51-
52-
expect(ReactInputSelection.hasSelectionCapabilities(inputText)).toBe(
53-
true,
54-
);
5561
expect(ReactInputSelection.hasSelectionCapabilities(inputReadOnly)).toBe(
5662
true,
5763
);
58-
expect(ReactInputSelection.hasSelectionCapabilities(inputNumber)).toBe(
59-
false,
60-
);
61-
expect(ReactInputSelection.hasSelectionCapabilities(inputEmail)).toBe(
62-
false,
63-
);
64-
expect(ReactInputSelection.hasSelectionCapabilities(inputPassword)).toBe(
65-
false,
66-
);
67-
expect(ReactInputSelection.hasSelectionCapabilities(inputHidden)).toBe(
68-
false,
69-
);
64+
});
65+
66+
it('returns false for non-text-selectable inputs', () => {
67+
[
68+
'button',
69+
'checkbox',
70+
'color',
71+
'file',
72+
'hidden',
73+
'image',
74+
'radio',
75+
'range',
76+
'reset',
77+
'submit',
78+
].forEach(type => {
79+
const input = document.createElement('input');
80+
input.type = type;
81+
expect(ReactInputSelection.hasSelectionCapabilities(input)).toBe(false);
82+
});
7083
});
7184

7285
it('returns true for contentEditable elements', () => {

packages/react-dom/src/client/ReactInputSelection.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,25 @@ function focusNodePreservingScroll(element) {
113113
* assume buttons have range selections allowed).
114114
* Input selection module for React.
115115
*/
116-
116+
const selectionCapableTypes = [
117+
'date',
118+
'datetime-local',
119+
'email',
120+
'month',
121+
'number',
122+
'password',
123+
'search',
124+
'tel',
125+
'text',
126+
'time',
127+
'url',
128+
'week',
129+
];
117130
export function hasSelectionCapabilities(elem) {
118131
const nodeName = elem && elem.nodeName && elem.nodeName.toLowerCase();
119132
return (
120133
nodeName &&
121-
((nodeName === 'input' && elem.type === 'text') ||
134+
((nodeName === 'input' && selectionCapableTypes.includes(elem.type)) ||
122135
nodeName === 'textarea' ||
123136
elem.contentEditable === 'true')
124137
);

0 commit comments

Comments
 (0)