The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

41 lines
1.2 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. settingKey: PropTypes.array.isRequired,
  10. label: PropTypes.node.isRequired,
  11. meta: PropTypes.node,
  12. onChange: PropTypes.func.isRequired,
  13. }
  14. onChange = ({ target }) => {
  15. this.props.onChange(this.props.settingKey, target.checked);
  16. }
  17. onKeyDown = e => {
  18. if (e.key === ' ') {
  19. this.props.onChange(this.props.settingKey, !e.target.checked);
  20. }
  21. }
  22. render () {
  23. const { prefix, settings, settingKey, label, meta } = this.props;
  24. const id = ['setting-toggle', prefix, ...settingKey].filter(Boolean).join('-');
  25. return (
  26. <div className='setting-toggle'>
  27. <Toggle id={id} checked={settings.getIn(settingKey)} onChange={this.onChange} onKeyDown={this.onKeyDown} />
  28. <label htmlFor={id} className='setting-toggle__label'>{label}</label>
  29. {meta && <span className='setting-meta__label'>{meta}</span>}
  30. </div>
  31. );
  32. }
  33. }