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.

90 lines
2.4 KiB

  1. // Package imports
  2. import React from 'react';
  3. import PropTypes from 'prop-types';
  4. import ImmutablePropTypes from 'react-immutable-proptypes';
  5. // Stylesheet imports
  6. import './style';
  7. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  8. export default class LocalSettingsPageItem extends React.PureComponent {
  9. static propTypes = {
  10. children: PropTypes.element.isRequired,
  11. dependsOn: PropTypes.array,
  12. dependsOnNot: PropTypes.array,
  13. id: PropTypes.string.isRequired,
  14. item: PropTypes.array.isRequired,
  15. onChange: PropTypes.func.isRequired,
  16. options: PropTypes.arrayOf(PropTypes.shape({
  17. value: PropTypes.string.isRequired,
  18. message: PropTypes.string.isRequired,
  19. })),
  20. settings: ImmutablePropTypes.map.isRequired,
  21. };
  22. handleChange = e => {
  23. const { target } = e;
  24. const { item, onChange, options } = this.props;
  25. if (options && options.length > 0) onChange(item, target.value);
  26. else onChange(item, target.checked);
  27. }
  28. render () {
  29. const { handleChange } = this;
  30. const { settings, item, id, options, children, dependsOn, dependsOnNot } = this.props;
  31. let enabled = true;
  32. if (dependsOn) {
  33. for (let i = 0; i < dependsOn.length; i++) {
  34. enabled = enabled && settings.getIn(dependsOn[i]);
  35. }
  36. }
  37. if (dependsOnNot) {
  38. for (let i = 0; i < dependsOnNot.length; i++) {
  39. enabled = enabled && !settings.getIn(dependsOnNot[i]);
  40. }
  41. }
  42. if (options && options.length > 0) {
  43. const currentValue = settings.getIn(item);
  44. const optionElems = options && options.length > 0 && options.map((opt) => (
  45. <option
  46. key={opt.value}
  47. value={opt.value}
  48. >
  49. {opt.message}
  50. </option>
  51. ));
  52. return (
  53. <label className='glitch local-settings__page__item' htmlFor={id}>
  54. <p>{children}</p>
  55. <p>
  56. <select
  57. id={id}
  58. disabled={!enabled}
  59. onBlur={handleChange}
  60. onChange={handleChange}
  61. value={currentValue}
  62. >
  63. {optionElems}
  64. </select>
  65. </p>
  66. </label>
  67. );
  68. } else return (
  69. <label className='glitch local-settings__page__item' htmlFor={id}>
  70. <input
  71. id={id}
  72. type='checkbox'
  73. checked={settings.getIn(item)}
  74. onChange={handleChange}
  75. disabled={!enabled}
  76. />
  77. {children}
  78. </label>
  79. );
  80. }
  81. }