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.
91 lines
2.4 KiB
91 lines
2.4 KiB
7 years ago
|
// Package imports
|
||
8 years ago
|
import React from 'react';
|
||
|
import PropTypes from 'prop-types';
|
||
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
||
|
|
||
7 years ago
|
// Stylesheet imports
|
||
|
import './style';
|
||
|
|
||
|
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||
|
|
||
|
export default class LocalSettingsPageItem extends React.PureComponent {
|
||
8 years ago
|
|
||
|
static propTypes = {
|
||
7 years ago
|
children: PropTypes.element.isRequired,
|
||
|
dependsOn: PropTypes.array,
|
||
|
dependsOnNot: PropTypes.array,
|
||
8 years ago
|
id: PropTypes.string.isRequired,
|
||
7 years ago
|
item: PropTypes.array.isRequired,
|
||
|
onChange: PropTypes.func.isRequired,
|
||
8 years ago
|
options: PropTypes.arrayOf(PropTypes.shape({
|
||
|
value: PropTypes.string.isRequired,
|
||
7 years ago
|
message: PropTypes.string.isRequired,
|
||
8 years ago
|
})),
|
||
7 years ago
|
settings: ImmutablePropTypes.map.isRequired,
|
||
8 years ago
|
};
|
||
|
|
||
7 years ago
|
handleChange = e => {
|
||
|
const { target } = e;
|
||
|
const { item, onChange, options } = this.props;
|
||
|
if (options && options.length > 0) onChange(item, target.value);
|
||
|
else onChange(item, target.checked);
|
||
8 years ago
|
}
|
||
|
|
||
|
render () {
|
||
7 years ago
|
const { handleChange } = this;
|
||
8 years ago
|
const { settings, item, id, options, children, dependsOn, dependsOnNot } = this.props;
|
||
|
let enabled = true;
|
||
|
|
||
|
if (dependsOn) {
|
||
|
for (let i = 0; i < dependsOn.length; i++) {
|
||
|
enabled = enabled && settings.getIn(dependsOn[i]);
|
||
|
}
|
||
|
}
|
||
|
if (dependsOnNot) {
|
||
|
for (let i = 0; i < dependsOnNot.length; i++) {
|
||
|
enabled = enabled && !settings.getIn(dependsOnNot[i]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (options && options.length > 0) {
|
||
|
const currentValue = settings.getIn(item);
|
||
|
const optionElems = options && options.length > 0 && options.map((opt) => (
|
||
7 years ago
|
<option
|
||
|
key={opt.value}
|
||
|
value={opt.value}
|
||
|
>
|
||
8 years ago
|
{opt.message}
|
||
|
</option>
|
||
|
));
|
||
|
return (
|
||
7 years ago
|
<label className='glitch local-settings__page__item' htmlFor={id}>
|
||
8 years ago
|
<p>{children}</p>
|
||
|
<p>
|
||
|
<select
|
||
|
id={id}
|
||
|
disabled={!enabled}
|
||
7 years ago
|
onBlur={handleChange}
|
||
|
onChange={handleChange}
|
||
|
value={currentValue}
|
||
8 years ago
|
>
|
||
|
{optionElems}
|
||
|
</select>
|
||
|
</p>
|
||
|
</label>
|
||
|
);
|
||
7 years ago
|
} else return (
|
||
|
<label className='glitch local-settings__page__item' htmlFor={id}>
|
||
|
<input
|
||
|
id={id}
|
||
|
type='checkbox'
|
||
|
checked={settings.getIn(item)}
|
||
|
onChange={handleChange}
|
||
|
disabled={!enabled}
|
||
|
/>
|
||
|
{children}
|
||
|
</label>
|
||
|
);
|
||
8 years ago
|
}
|
||
|
|
||
|
}
|