-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathnativeChangeFieldValue.js
54 lines (45 loc) · 1.56 KB
/
nativeChangeFieldValue.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
/* Programmatically change a field value and trigger the onChange event so that external state can be updated...
* input ref - reference object
* focus - boolean - send focus to the field after clearing.
* value - new value for the field.
* triggerChange - defaults true - whether or not to trigger the change event.
*/
function deletePropertySafe(elem, prop) {
const desc = Object.getOwnPropertyDescriptor(elem, prop);
if (desc && desc.configurable) {
delete elem[prop];
}
}
export default (inputRef, focus, value = '', triggerChange = true) => {
const node = inputRef.current;
let event;
// grab the whole current descriptor for value (set and all...);
const descriptor = Object.getOwnPropertyDescriptor(node, 'value');
// maybe trigger focus...may be required...
if (focus) {
event = document.createEvent('UIEvents');
event.initEvent('focus', false, false);
node.dispatchEvent(event);
}
const initialValue = node.value;
node.value = initialValue + '#';
deletePropertySafe(node, 'value');
node.value = value;
if (triggerChange) {
event = document.createEvent('HTMLEvents');
event.initEvent('propertychange', false, false);
event.propertyName = 'value';
node.dispatchEvent(event);
event = document.createEvent('HTMLEvents');
event.initEvent('input', true, false);
node.dispatchEvent(event);
}
// Restore artificial value property descriptor.
if (descriptor) {
Object.defineProperty(node, 'value', descriptor);
}
// if focus was demanded, finish with that.
if (focus) {
node.focus();
}
};