-
Notifications
You must be signed in to change notification settings - Fork 126
/
use-onblur-not-onchange.js
57 lines (51 loc) · 1.81 KB
/
use-onblur-not-onchange.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import {
hiddenFromAT,
listensTo,
devices,
trueish,
fn,
warnRuleDeprecated
} from '../util';
export default [{
msg: '`onBlur` should be preferred over `onChange`, unless absolutely necessary '
+ 'and it has no negative consequences for keyboard only or screen-reader users.',
url: 'http://webaim.org/techniques/javascript/eventhandlers#onchange',
affects: [
devices.keyboardOnly,
devices.screenReaders
],
test(tagName, props) {
warnRuleDeprecated('use-onblur-not-onchange', 'no-onchange');
const hidden = hiddenFromAT(props);
const disabled = trueish(props, 'aria-disabled');
const readOnly = trueish(props, 'aria-readonly');
const onChange = listensTo(props, 'onChange');
const onBlur = listensTo(props, 'onBlur');
return hidden || disabled || readOnly || !onChange || (onChange && onBlur);
}
}];
export const pass = [{
when: 'there is no `onChange` prop',
render: React => <input />
}, {
when: 'the element is aria-hidden',
render: React => <input onChange={fn} aria-hidden />
}, {
when: 'the element is aria-disabled',
render: React => <input onChange={fn} aria-disabled />
}, {
when: 'the element is aria-readonly',
render: React => <input onChange={fn} aria-readonly />
}, {
when: 'the `onChange` prop is present along with an `onBlur` prop',
render: React => <input onChange={fn} onBlur={fn} />
}];
export const fail = [{
when: 'the `onChange` prop is present without an `onBlur` prop',
render: React => <input onChange={fn} />
}];
export const description = `
Enforce usage of onBlur over onChange for accessibility. onBlur must be used
instead of onChange, unless absolutely necessary and it causes no negative
consequences for keyboard only or screen reader users.
`;