element-web/src/components/views/elements/SettingsFlag.tsx

105 lines
3.3 KiB
TypeScript
Raw Normal View History

/*
Copyright 2017 Travis Ralston
Copyright 2019 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import React from "react";
import SettingsStore from "../../../settings/SettingsStore";
import { _t } from '../../../languageHandler';
import ToggleSwitch from "./ToggleSwitch";
2020-06-08 18:48:32 +03:00
import StyledCheckbox from "./StyledCheckbox";
2020-07-29 20:03:43 +03:00
import { SettingLevel } from "../../../settings/SettingLevel";
2020-06-08 18:48:32 +03:00
interface IProps {
// The setting must be a boolean
2020-06-10 17:57:28 +03:00
name: string;
2020-07-29 20:03:43 +03:00
level: SettingLevel;
2020-06-10 17:57:28 +03:00
roomId?: string; // for per-room settings
label?: string; // untranslated
isExplicit?: boolean;
2020-06-08 18:48:32 +03:00
// XXX: once design replaces all toggles make this the default
2020-06-10 17:57:28 +03:00
useCheckbox?: boolean;
2020-06-24 18:06:50 +03:00
disabled?: boolean;
2020-06-10 17:57:28 +03:00
onChange?(checked: boolean): void;
2020-06-08 18:48:32 +03:00
}
2020-06-08 18:48:32 +03:00
interface IState {
value: boolean;
2020-06-08 18:48:32 +03:00
}
export default class SettingsFlag extends React.Component<IProps, IState> {
constructor(props: IProps) {
super(props);
this.state = {
value: SettingsStore.getValueAt(
this.props.level,
this.props.name,
this.props.roomId,
this.props.isExplicit,
),
2020-06-18 16:32:43 +03:00
};
2020-06-08 18:48:32 +03:00
}
2020-07-29 20:03:43 +03:00
private onChange = async (checked: boolean) => {
await this.save(checked);
this.setState({ value: checked });
if (this.props.onChange) this.props.onChange(checked);
2020-06-18 16:32:43 +03:00
};
2020-06-08 18:48:32 +03:00
private checkBoxOnChange = (e: React.ChangeEvent<HTMLInputElement>) => {
this.onChange(e.target.checked);
2020-06-18 16:32:43 +03:00
};
2020-07-29 20:03:43 +03:00
private save = async (val?: boolean) => {
await SettingsStore.setValue(
this.props.name,
this.props.roomId,
this.props.level,
val !== undefined ? val : this.state.value,
);
2020-06-18 16:32:43 +03:00
};
2020-06-08 18:48:32 +03:00
public render() {
const canChange = SettingsStore.canSetValue(this.props.name, this.props.roomId, this.props.level);
let label = this.props.label;
if (!label) label = SettingsStore.getDisplayName(this.props.name, this.props.level);
else label = _t(label);
2020-06-08 18:48:32 +03:00
if (this.props.useCheckbox) {
2020-06-24 18:06:50 +03:00
return <StyledCheckbox
checked={this.state.value}
onChange={this.checkBoxOnChange}
disabled={this.props.disabled || !canChange}
>
2020-06-08 18:48:32 +03:00
{label}
</StyledCheckbox>;
} else {
return (
<div className="mx_SettingsFlag">
<span className="mx_SettingsFlag_label">{label}</span>
2020-06-24 18:06:50 +03:00
<ToggleSwitch
checked={this.state.value}
onChange={this.onChange}
disabled={this.props.disabled || !canChange}
aria-label={label}
/>
2020-06-08 18:48:32 +03:00
</div>
);
}
}
}