bigbuffet-rw/app/soapbox/features/notifications/components/setting_toggle.js

39 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-03-27 13:59:38 -07:00
import React from 'react';
import PropTypes from 'prop-types';
import ImmutablePropTypes from 'react-immutable-proptypes';
import Toggle from 'react-toggle';
export default class SettingToggle extends React.PureComponent {
static propTypes = {
prefix: PropTypes.string,
settings: ImmutablePropTypes.map.isRequired,
settingPath: PropTypes.array.isRequired,
2020-07-19 14:50:16 -07:00
label: PropTypes.node,
2020-03-27 13:59:38 -07:00
onChange: PropTypes.func.isRequired,
2020-07-19 14:50:16 -07:00
icons: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.object,
]),
condition: PropTypes.string,
ariaLabel: PropTypes.string,
2020-03-27 13:59:38 -07:00
}
onChange = ({ target }) => {
this.props.onChange(this.props.settingPath, target.checked);
}
render() {
2020-07-19 14:50:16 -07:00
const { prefix, settings, settingPath, label, icons, condition, ariaLabel } = this.props;
2020-03-27 13:59:38 -07:00
const id = ['setting-toggle', prefix, ...settingPath].filter(Boolean).join('-');
return (
2020-07-19 14:50:16 -07:00
<div className='setting-toggle' aria-label={ariaLabel}>
<Toggle id={id} checked={condition ? settings.getIn(settingPath) === condition : settings.getIn(settingPath)} onChange={this.onChange} icons={icons} onKeyDown={this.onKeyDown} />
{label && (<label htmlFor={id} className='setting-toggle__label'>{label}</label>)}
2020-03-27 13:59:38 -07:00
</div>
);
}
}