Skip to content

Commit 1579fe0

Browse files
committed
Make NativeCheckbox functional
1 parent 01e925a commit 1579fe0

File tree

1 file changed

+16
-29
lines changed

1 file changed

+16
-29
lines changed

src/js/components/NativeCheckbox.jsx

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,24 @@
11
import PropTypes from 'prop-types';
2-
import React from 'react';
2+
import React, { useEffect, useRef } from 'react';
33

4-
class NativeCheckbox extends React.PureComponent {
5-
static propTypes = {
6-
indeterminate: PropTypes.bool,
7-
};
4+
const propTypes = {
5+
indeterminate: PropTypes.bool,
6+
};
7+
const defaultProps = {
8+
indeterminate: false,
9+
};
810

9-
static defaultProps = {
10-
indeterminate: false,
11-
};
11+
function NativeCheckbox({ indeterminate, ...otherProps }) {
12+
const checkbox = useRef(null);
1213

13-
componentDidMount() {
14-
this.updateDeterminateProperty();
15-
}
14+
useEffect(() => {
15+
checkbox.current.indeterminate = indeterminate;
16+
});
1617

17-
componentDidUpdate() {
18-
this.updateDeterminateProperty();
19-
}
20-
21-
updateDeterminateProperty() {
22-
const { indeterminate } = this.props;
23-
24-
this.checkbox.indeterminate = indeterminate;
25-
}
26-
27-
render() {
28-
const props = { ...this.props };
29-
30-
// Remove property that does not exist in HTML
31-
delete props.indeterminate;
32-
33-
return <input {...props} ref={(c) => { this.checkbox = c; }} type="checkbox" />;
34-
}
18+
return <input {...otherProps} ref={checkbox} type="checkbox" />;
3519
}
3620

21+
NativeCheckbox.propTypes = propTypes;
22+
NativeCheckbox.defaultProps = defaultProps;
23+
3724
export default NativeCheckbox;

0 commit comments

Comments
 (0)