import {Component} from 'react'; import Confirm from 'sentry/components/confirm'; import InputField, {InputFieldProps, onEvent} from 'sentry/components/forms/inputField'; import Switch from 'sentry/components/switchButton'; export interface BooleanFieldProps extends InputFieldProps { confirm?: { false?: React.ReactNode; true?: React.ReactNode; }; } export default class BooleanField extends Component { coerceValue(value: any) { return !!value; } handleChange = ( value: any, onChange: onEvent, onBlur: onEvent, e: React.FormEvent ) => { // We need to toggle current value because Switch is not an input const newValue = this.coerceValue(!value); onChange(newValue, e); onBlur(newValue, e); }; render() { const {confirm, ...fieldProps} = this.props; return ( { // Create a function with required args bound const handleChange = this.handleChange.bind(this, value, onChange, onBlur); const switchProps = { ...props, size: 'lg' as React.ComponentProps['size'], isActive: !!value, isDisabled: disabled, toggle: handleChange, }; if (confirm) { return ( confirm[(!value).toString()]} onConfirm={() => handleChange({})} > {({open}) => ( { // If we have a `confirm` prop and enabling switch // Then show confirm dialog, otherwise propagate change as normal if (confirm[(!value).toString()]) { // Open confirm modal open(); return; } handleChange(e); }} /> )} ); } return ; }} /> ); } }