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

45 lines
1.3 KiB
JavaScript
Raw Normal View History

2020-03-27 13:59:38 -07:00
import PropTypes from 'prop-types';
import React from 'react';
2020-03-27 13:59:38 -07:00
import ImmutablePropTypes from 'react-immutable-proptypes';
import ImmutablePureComponent from 'react-immutable-pure-component';
2020-03-27 13:59:38 -07:00
import Toggle from 'react-toggle';
export default class SettingToggle extends ImmutablePureComponent {
2020-03-27 13:59:38 -07:00
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,
]),
ariaLabel: PropTypes.string,
2020-03-27 13:59:38 -07:00
}
onChange = ({ target }) => {
this.props.onChange(this.props.settingPath, target.checked);
}
render() {
const { prefix, settings, settingPath, label, icons, 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={settings.getIn(settingPath)}
onChange={this.onChange}
icons={icons}
onKeyDown={this.onKeyDown}
/>
2020-07-19 14:50:16 -07:00
{label && (<label htmlFor={id} className='setting-toggle__label'>{label}</label>)}
2020-03-27 13:59:38 -07:00
</div>
);
}
}