import {Component} from 'react'; import styled from '@emotion/styled'; import {NumberDragInput} from 'sentry/components/core/input/numberDragInput'; import {Select} from 'sentry/components/core/select'; import {t} from 'sentry/locale'; import {space} from 'sentry/styles/space'; import type {ThresholdControlValue} from 'sentry/views/alerts/rules/metric/types'; import { AlertRuleComparisonType, AlertRuleThresholdType, } from 'sentry/views/alerts/rules/metric/types'; interface Props extends ThresholdControlValue { comparisonType: AlertRuleComparisonType; disableThresholdType: boolean; disabled: boolean; onChange: (value: ThresholdControlValue, e: React.FormEvent) => void; onThresholdTypeChange: (thresholdType: AlertRuleThresholdType) => void; placeholder: string; type: string; hideControl?: boolean; } type State = { currentValue: string | null; }; class ThresholdControl extends Component { state: State = { currentValue: null, }; handleThresholdChange = (event: React.ChangeEvent) => { // Only allow number and partial number inputs if (!/^[0-9]*\.?[0-9]*$/.test(event.target.value)) { return; } // Empty input if (event.target.value === '') { this.setState({currentValue: null}); this.props.onChange( {thresholdType: this.props.thresholdType, threshold: ''}, event ); return; } // Only call onChange if the new number is valid, and not partially typed // (eg writing out the decimal '5.') if (/\.+0*$/.test(event.target.value)) { this.setState({currentValue: event.target.value}); return; } const numberValue = Number(event.target.value); this.setState({currentValue: null}); this.props.onChange( {thresholdType: this.props.thresholdType, threshold: numberValue}, event ); }; handleTypeChange = ({value}: any) => { this.props.onThresholdTypeChange(value); }; render() { const {currentValue} = this.state; const { thresholdType, comparisonType, hideControl, threshold, placeholder, type, onChange: _, onThresholdTypeChange: __, disabled, disableThresholdType, } = this.props; const inputValue = currentValue ?? threshold ?? ''; return (