mue/src/components/modals/main/settings/Checkbox.jsx

62 lines
1.5 KiB
React
Raw Normal View History

2021-09-28 22:04:04 +00:00
import variables from 'modules/variables';
2021-08-14 19:10:48 +00:00
import { PureComponent } from 'react';
2021-09-10 18:00:45 +00:00
import { Checkbox as CheckboxUI, FormControlLabel } from '@mui/material';
2021-01-17 18:07:26 +00:00
2021-08-28 14:34:12 +00:00
import EventBus from 'modules/helpers/eventbus';
2021-01-17 18:07:26 +00:00
2021-08-14 19:10:48 +00:00
export default class Checkbox extends PureComponent {
2021-03-23 13:10:34 +00:00
constructor(props) {
super(props);
this.state = {
checked: localStorage.getItem(this.props.name) === 'true',
2020-09-15 21:38:32 +00:00
};
}
2021-03-23 13:10:34 +00:00
handleChange = () => {
const value = this.state.checked !== true;
localStorage.setItem(this.props.name, value);
this.setState({
checked: value,
2021-01-16 22:43:46 +00:00
});
if (this.props.onChange) {
this.props.onChange(value);
}
variables.stats.postEvent(
'setting',
`${this.props.name} ${this.state.checked === true ? 'enabled' : 'disabled'}`,
);
2021-06-21 16:42:14 +00:00
if (this.props.element) {
if (!document.querySelector(this.props.element)) {
document.querySelector('.reminder-info').style.display = 'flex';
return localStorage.setItem('showReminder', true);
}
}
EventBus.dispatch('refresh', this.props.category);
};
2020-09-15 11:40:20 +00:00
render() {
return (
<>
<FormControlLabel
control={
<CheckboxUI
name={this.props.name}
color="primary"
className="checkbox"
checked={this.state.checked}
onChange={this.handleChange}
disabled={this.props.disabled || false}
/>
}
2021-08-15 21:28:37 +00:00
label={this.props.text}
/>
</>
);
}
}