-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
36 lines (34 loc) · 1.08 KB
/
index.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
import React from 'react';
import SanitizeInput from 'sanitize-input';
export default class Sanitize extends React.PureComponent {
constructor(props) {
super(props);
// check if Sanitizer API exists in window
this.failedMount = false;
if (!('Sanitizer' in window)) {
console.warn('Sanitizer API does not exist in this browser');
this.failedMount = true;
}
this.Sanitizer = new window.Sanitizer();
}
render() {
return this.failedMount
? !this.props.children.length
? this.props.children
: this.props.children.map((child) => {
const newHandler = function (e) {
e.target.sanitizedValue = this.Sanitizer.sanitizeToString(
e.target.value
);
child.props.onChange(e);
}.bind(this);
const newProps = Object.assign(
{ ...child.props },
{ onChange: newHandler }
);
const newChild = React.createElement(child.type, newProps);
return newChild;
})
: this.props.children;
}
}