2022-12-15 04:03:23 +08:00
|
|
|
import React, { createRef, PureComponent } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
checked: PropTypes.bool,
|
|
|
|
onChange: PropTypes.func.isRequired,
|
|
|
|
ariaLabelledBy: PropTypes.string,
|
|
|
|
ariaLabel: PropTypes.string,
|
|
|
|
ariaDescribedBy: PropTypes.string,
|
|
|
|
ariaDesc: PropTypes.string,
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
disabled: false,
|
|
|
|
checked: false,
|
|
|
|
ariaLabelledBy: null,
|
|
|
|
ariaLabel: null,
|
|
|
|
ariaDescribedBy: null,
|
|
|
|
ariaDesc: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default class Base extends PureComponent {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.onChange = props.onChange;
|
|
|
|
this.handleChange = this.handleChange.bind(this);
|
|
|
|
this.handleKeyDown = this.handleKeyDown.bind(this);
|
|
|
|
|
|
|
|
this.element = createRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2024-06-13 22:57:49 +08:00
|
|
|
const element = this.element.current;
|
2022-12-15 04:03:23 +08:00
|
|
|
if (element) element.addEventListener('keydown', this.handleKeyDown);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2024-06-13 22:57:49 +08:00
|
|
|
const element = this.element.current;
|
2022-12-15 04:03:23 +08:00
|
|
|
if (element) element.removeEventListener('keydown', this.handleKeyDown);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleKeyDown(event) {
|
|
|
|
const { key } = event;
|
2024-06-13 22:57:49 +08:00
|
|
|
const node = this.element.current;
|
2022-12-15 04:03:23 +08:00
|
|
|
if (key === 'Enter' && node) {
|
|
|
|
const input = node.getElementsByTagName('input')[0];
|
|
|
|
input?.click();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleChange() {
|
|
|
|
const { disabled, keyValue } = this.props;
|
|
|
|
if (disabled) return;
|
|
|
|
this.onChange(keyValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Base.propTypes = propTypes;
|
|
|
|
Base.defaultProps = defaultProps;
|