From 6a86758f3b3b65a244cac5edd433062a17f0c4c7 Mon Sep 17 00:00:00 2001 From: nikki-kiga <42276368+nikki-kiga@users.noreply.github.com> Date: Wed, 24 Nov 2021 16:21:26 -0800 Subject: [PATCH] NumberInput: Add validation for min max on input (#42254) * add scalar and number input validation * add check and warning message --- .../dimensions/editors/NumberInput.tsx | 56 +++++++++++++------ .../editors/ScalarDimensionEditor.tsx | 10 +++- 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/public/app/features/dimensions/editors/NumberInput.tsx b/public/app/features/dimensions/editors/NumberInput.tsx index 674543e0cf2..da21132a776 100644 --- a/public/app/features/dimensions/editors/NumberInput.tsx +++ b/public/app/features/dimensions/editors/NumberInput.tsx @@ -1,5 +1,5 @@ import React, { PureComponent } from 'react'; -import { Input } from '@grafana/ui'; +import { Field, Input } from '@grafana/ui'; interface Props { value?: number; @@ -13,16 +13,18 @@ interface Props { interface State { text: string; + inputCorrected: boolean; } /** * This is an Input field that will call `onChange` for blur and enter */ export class NumberInput extends PureComponent { - state: State = { text: '' }; + state: State = { text: '', inputCorrected: false }; componentDidMount() { this.setState({ + ...this.state, text: isNaN(this.props.value!) ? '' : `${this.props.value}`, }); } @@ -30,6 +32,7 @@ export class NumberInput extends PureComponent { componentDidUpdate(oldProps: Props) { if (this.props.value !== oldProps.value) { this.setState({ + ...this.state, text: isNaN(this.props.value!) ? '' : `${this.props.value}`, }); } @@ -42,11 +45,30 @@ export class NumberInput extends PureComponent { value = e.currentTarget.valueAsNumber; } this.props.onChange(value); + this.setState({ ...this.state, inputCorrected: false }); }; onChange = (e: React.FocusEvent) => { + let newValue: string | undefined = undefined; + let corrected = false; + const min = this.props.min; + const max = this.props.max; + const currValue = e.currentTarget.valueAsNumber; + if (!Number.isNaN(currValue)) { + if (min != null && currValue < min) { + newValue = min.toString(); + corrected = true; + } else if (max != null && currValue > max) { + newValue = max.toString(); + corrected = true; + } else { + newValue = e.currentTarget.value; + } + } this.setState({ - text: e.currentTarget.value, + ...this.state, + text: newValue ? newValue : '', + inputCorrected: corrected, }); }; @@ -58,20 +80,22 @@ export class NumberInput extends PureComponent { render() { const { placeholder } = this.props; - const { text } = this.state; + const { text, inputCorrected } = this.state; return ( - + + + ); } } diff --git a/public/app/features/dimensions/editors/ScalarDimensionEditor.tsx b/public/app/features/dimensions/editors/ScalarDimensionEditor.tsx index 8d26be77018..4efef4cb0ec 100644 --- a/public/app/features/dimensions/editors/ScalarDimensionEditor.tsx +++ b/public/app/features/dimensions/editors/ScalarDimensionEditor.tsx @@ -19,7 +19,8 @@ const scalarOptions = [ export const ScalarDimensionEditor: FC> = ( props ) => { - const { value, context, onChange } = props; + const { value, context, onChange, item } = props; + const { settings } = item; const DEFAULT_VALUE = 0; @@ -94,7 +95,12 @@ export const ScalarDimensionEditor: FC - + )}