闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
1.0 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import ImmutablePropTypes from 'react-immutable-proptypes';
  4. import Toggle from 'react-toggle';
  5. export default class SettingToggle extends React.PureComponent {
  6. static propTypes = {
  7. prefix: PropTypes.string,
  8. settings: ImmutablePropTypes.map.isRequired,
  9. settingPath: PropTypes.array.isRequired,
  10. label: PropTypes.node.isRequired,
  11. onChange: PropTypes.func.isRequired,
  12. defaultValue: PropTypes.bool,
  13. }
  14. onChange = ({ target }) => {
  15. this.props.onChange(this.props.settingPath, target.checked);
  16. }
  17. render () {
  18. const { prefix, settings, settingPath, label, defaultValue } = this.props;
  19. const id = ['setting-toggle', prefix, ...settingPath].filter(Boolean).join('-');
  20. return (
  21. <div className='setting-toggle'>
  22. <Toggle id={id} checked={settings.getIn(settingPath, defaultValue)} onChange={this.onChange} onKeyDown={this.onKeyDown} />
  23. <label htmlFor={id} className='setting-toggle__label'>{label}</label>
  24. </div>
  25. );
  26. }
  27. }