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

79 lines
2.1 KiB
React
Raw Normal View History

import variables from 'modules/variables';
2021-08-27 22:08:32 +00:00
import { PureComponent, createRef } from 'react';
import { InputLabel, MenuItem, FormControl, Select } from '@mui/material';
2020-10-27 14:59:45 +00:00
2021-08-28 14:34:12 +00:00
import EventBus from 'modules/helpers/eventbus';
2021-08-14 19:10:48 +00:00
export default class Dropdown extends PureComponent {
2021-03-23 13:10:34 +00:00
constructor(props) {
super(props);
this.state = {
value: localStorage.getItem(this.props.name) || this.props.children[0].props.value,
title: '',
};
2021-08-27 22:08:32 +00:00
this.dropdown = createRef();
2020-10-27 14:59:45 +00:00
}
2021-03-23 13:10:34 +00:00
onChange = (e) => {
const { value } = e.target;
if (value === variables.getMessage('modals.main.loading')) {
return;
}
2021-09-28 22:04:04 +00:00
variables.stats.postEvent('setting', `${this.props.name} from ${this.state.value} to ${value}`);
2021-06-21 16:42:14 +00:00
this.setState({
value,
});
2021-09-18 19:51:00 +00:00
if (!this.props.noSetting) {
localStorage.setItem(this.props.name, value);
2022-11-06 11:59:59 +00:00
localStorage.setItem(this.props.name2, this.props.value2);
2021-09-18 19:51:00 +00:00
}
if (this.props.onChange) {
this.props.onChange(value);
}
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-10-27 14:59:45 +00:00
render() {
const id = 'dropdown' + this.props.name;
const label = this.props.label || '';
2020-10-27 14:59:45 +00:00
return (
<FormControl fullWidth className={id}>
<InputLabel id={id}>{label}</InputLabel>
<Select
labelId={id}
id={this.props.name}
value={this.state.value}
label={label}
onChange={this.onChange}
ref={this.dropdown}
key={id}
>
{this.props.manual
? this.props.children
: this.props.children.map((e, index) => {
return e ? (
<MenuItem key={index} value={e.props ? e.props.value : ''}>
{e.props ? e.props.children : ''}
</MenuItem>
) : null;
})}
</Select>
</FormControl>
2020-10-27 14:59:45 +00:00
);
}
}