import classNames from 'classnames'; import FormField from 'sentry/components/deprecatedforms/formField'; import Tooltip from 'sentry/components/tooltip'; import {IconQuestion} from 'sentry/icons'; import {Choices} from 'sentry/types'; import {defined} from 'sentry/utils'; type Value = string | number | boolean; type Props = { choices: Choices; hideLabelDivider?: boolean; } & FormField['props']; type State = FormField['state'] & { values: Value[]; }; export default class MultipleCheckboxField extends FormField { onChange = (e: React.ChangeEvent, _value?: Value) => { const value = _value as Value; // Casting here to allow _value to be optional, which it has to be since it's overloaded. let allValues = this.state.values; if (e.target.checked) { if (allValues) { allValues = [...allValues, value]; } else { allValues = [value]; } } else { allValues = allValues.filter(v => v !== value); } this.setValues(allValues); }; setValues(values: Value[]) { const form = (this.context || {}).form; this.setState( { values, }, () => { const finalValue = this.coerceValue(this.state.values); this.props.onChange && this.props.onChange(finalValue); form && form.onFieldChange(this.props.name, finalValue); } ); } render() { const { required, className, disabled, disabledReason, label, help, choices, hideLabelDivider, style, } = this.props; const {error} = this.state; const cx = classNames(className, 'control-group', { 'has-error': error, }); // Hacky, but this isn't really a form label vs the checkbox labels, but // we want to treat it as one (i.e. for "required" indicator) const labelCx = classNames({ required, }); const shouldShowDisabledReason = disabled && disabledReason; return (
{help &&

{help}

} {error &&

{error}

}
{choices.map(([value, choiceLabel]) => ( ))}
); } }