BetterDiscordApp-rauenzi/renderer/src/ui/settings/group.jsx

82 lines
3.8 KiB
React
Raw Normal View History

import Logger from "common/logger";
import {React} from "modules";
2019-06-04 06:18:15 +02:00
import Title from "./title";
import Divider from "./divider";
2019-06-30 05:09:48 +02:00
import Switch from "./components/switch";
import Dropdown from "./components/dropdown";
2022-02-05 09:35:07 +01:00
import Number from "./components/number";
2019-06-30 05:09:48 +02:00
import Item from "./components/item";
2019-06-04 06:18:15 +02:00
2019-06-04 16:21:07 +02:00
const baseClassName = "bd-settings-group";
2019-06-04 06:18:15 +02:00
export default class Group extends React.Component {
constructor(props) {
super(props);
2019-06-04 16:21:07 +02:00
if (this.props.button && this.props.collapsible) {
const original = this.props.button.onClick;
this.props.button.onClick = (event) => {
event.stopPropagation();
original(...arguments);
};
}
2019-06-05 06:30:24 +02:00
if (!this.props.hasOwnProperty("shown")) this.props.shown = true;
2019-06-04 06:18:15 +02:00
this.container = React.createRef();
this.state = {
2019-06-05 06:30:24 +02:00
collapsed: this.props.collapsible && !this.props.shown
2019-06-04 06:18:15 +02:00
};
2019-06-04 21:17:23 +02:00
this.onChange = this.onChange.bind(this);
2019-06-30 05:09:48 +02:00
this.toggleCollapse = this.toggleCollapse.bind(this);
2019-06-04 06:18:15 +02:00
}
2019-06-04 16:21:07 +02:00
toggleCollapse() {
const container = this.container.current;
const timeout = this.state.collapsed ? 300 : 1;
2019-06-04 06:18:15 +02:00
container.style.setProperty("height", container.scrollHeight + "px");
2019-06-30 05:09:48 +02:00
container.classList.add("animating");
this.setState({collapsed: !this.state.collapsed}, () => setTimeout(() => {
container.style.setProperty("height", "");
container.classList.remove("animating");
}, timeout));
if (this.props.onDrawerToggle) this.props.onDrawerToggle(this.state.collapsed);
2019-06-04 06:18:15 +02:00
}
2019-06-04 21:17:23 +02:00
onChange(id, value) {
if (!this.props.onChange) return;
2019-06-05 06:30:24 +02:00
if (this.props.id) this.props.onChange(this.props.id, id, value);
else this.props.onChange(id, value);
2019-06-04 21:17:23 +02:00
this.forceUpdate();
}
2019-06-04 06:18:15 +02:00
render() {
const {settings} = this.props;
2019-06-30 05:09:48 +02:00
const collapseClass = this.props.collapsible ? `collapsible ${this.state.collapsed ? "collapsed" : "expanded"}` : "";
2019-06-04 16:21:07 +02:00
const groupClass = `${baseClassName} ${collapseClass}`;
2019-06-04 06:18:15 +02:00
return <div className={groupClass}>
2019-06-30 05:09:48 +02:00
<Title text={this.props.name} collapsible={this.props.collapsible} onClick={this.toggleCollapse} button={this.props.button} isGroup={true} />
2019-06-04 16:21:07 +02:00
<div className="bd-settings-container" ref={this.container}>
2019-06-30 05:09:48 +02:00
{settings.filter(s => !s.hidden).map((setting) => {
let component = null;
if (setting.type == "dropdown") component = <Dropdown disabled={setting.disabled} id={setting.id} options={setting.options} value={setting.value} onChange={this.onChange.bind(this, setting.id)} />;
2022-02-05 09:35:07 +01:00
if (setting.type == "number") component = <Number disabled={setting.disabled} id={setting.id} min={setting.min} max={setting.max} step={setting.step} value={setting.value} onChange={this.onChange.bind(this, setting.id)} />;
2019-06-30 05:09:48 +02:00
if (setting.type == "switch") component = <Switch disabled={setting.disabled} id={setting.id} checked={setting.value} onChange={this.onChange.bind(this, setting.id)} />;
if (!component) return null;
return <Item id={setting.id} key={setting.id} name={setting.name} note={setting.note}>{component}</Item>;
})}
2019-06-04 16:21:07 +02:00
</div>
2019-06-04 06:18:15 +02:00
{this.props.showDivider && <Divider />}
</div>;
}
2020-07-16 07:42:56 +02:00
}
const originalRender = Group.prototype.render;
Object.defineProperty(Group.prototype, "render", {
enumerable: false,
configurable: false,
2021-07-09 08:05:58 +02:00
set: function() {Logger.warn("Group", "Addon policy for plugins #5 https://github.com/BetterDiscord/BetterDiscord/wiki/Addon-Policies#plugins");},
2020-07-16 07:42:56 +02:00
get: () => originalRender
});