Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EuiFieldNumber] Fix native validity state when controlled value changes #7291

Merged
merged 7 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ exports[`EuiFieldNumber is rendered 1`] = `
max="8"
min="1"
name="elastic"
step="1"
step="any"
type="number"
value="1"
/>
Expand Down
124 changes: 115 additions & 9 deletions src/components/form/field_number/field_number.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
/// <reference types="cypress-real-events" />
/// <reference types="../../../../cypress/support" />

import React from 'react';
import React, { useState } from 'react';

import { EuiFieldNumber } from './field_number';

describe('EuiFieldNumber', () => {
Expand Down Expand Up @@ -52,19 +53,124 @@ describe('EuiFieldNumber', () => {
checkIsInvalid();
});

it('shows invalid state on blur', () => {
cy.mount(<EuiFieldNumber max={1} value={2} />);
checkIsValid();
cy.get('input').click();
cy.get('body').click('bottomRight');
checkIsInvalid();
});

it('does not show invalid state on decimal values by default', () => {
cy.mount(<EuiFieldNumber />);
checkIsValid();
cy.get('input').click().type('1.5');
checkIsValid();
});

it('checks/updates invalid state for controlled components', () => {
const ControlledEuiFieldNumber = () => {
const [value, setValue] = useState('0');
return (
<>
<EuiFieldNumber
value={value}
onChange={(e) => setValue(e.target.value)}
min={0}
max={5}
/>
<button id="setToInvalidValue" onClick={() => setValue('10')}>
Set to invalid value
</button>
<button id="setToValidValue" onClick={() => setValue('3')}>
Set to valid value
</button>
</>
);
};
cy.mount(<ControlledEuiFieldNumber />);
checkIsValid();

// Controlled value changes should work as expected
cy.get('#setToInvalidValue').click();
checkIsInvalid();
cy.get('#setToValidValue').click();
checkIsValid();

// (regression test) User input changes should still work as expected w/ onChange
cy.get('input').clear().type('-2');
checkIsInvalid();
cy.get('input').clear().type('2');
checkIsValid();
});

describe('checks/updates invalid state when props that would affect validity change', () => {
it('min', () => {
const UpdatedEuiFieldNumber = () => {
const [min, setMin] = useState<number | undefined>();
return (
<>
<EuiFieldNumber min={min} />
<button id="setInvalidMin" onClick={() => setMin(100)}>
Set invalid min
</button>
<button id="setValidMin" onClick={() => setMin(0)}>
Change valid min
</button>
</>
);
};
cy.mount(<UpdatedEuiFieldNumber />);
cy.get('input').type('1');
checkIsValid();

cy.get('#setInvalidMin').click();
checkIsInvalid();
cy.get('#setValidMin').click();
checkIsValid();
});

it('max', () => {
const UpdatedEuiFieldNumber = () => {
const [max, setMax] = useState<number | undefined>();
return (
<>
<EuiFieldNumber max={max} />
<button id="setInvalidMax" onClick={() => setMax(0)}>
Set invalid max
</button>
<button id="setValidMax" onClick={() => setMax(10)}>
Change valid max
</button>
</>
);
};
cy.mount(<UpdatedEuiFieldNumber />);
cy.get('input').type('1');
checkIsValid();

cy.get('#setInvalidMax').click();
checkIsInvalid();
cy.get('#setValidMax').click();
checkIsValid();
});

it('step', () => {
const UpdatedEuiFieldNumber = () => {
const [step, setStep] = useState<number | undefined>();
return (
<>
<EuiFieldNumber step={step} />
<button id="setInvalidStep" onClick={() => setStep(1.5)}>
Set invalid step
</button>
<button id="setValidStep" onClick={() => setStep(1)}>
Change valid step
</button>
</>
);
};
cy.mount(<UpdatedEuiFieldNumber />);
cy.get('input').type('1');
checkIsValid();

cy.get('#setInvalidStep').click();
checkIsInvalid();
cy.get('#setValidStep').click();
checkIsValid();
});
});
});
});
34 changes: 34 additions & 0 deletions src/components/form/field_number/field_number.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { Meta, StoryObj } from '@storybook/react';

import { EuiFieldNumber, EuiFieldNumberProps } from './field_number';

const meta: Meta<EuiFieldNumberProps> = {
title: 'EuiFieldNumber',
component: EuiFieldNumber,
argTypes: {
step: { control: 'number' },
},
};

export default meta;
type Story = StoryObj<EuiFieldNumberProps>;

export const Playground: Story = {};

export const ControlledComponent: Story = {
args: {
value: 0,
},
argTypes: {
value: { control: 'number' },
onChange: () => {},
},
};
5 changes: 4 additions & 1 deletion src/components/form/field_number/field_number.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ describe('EuiFieldNumber', () => {
name="elastic"
min={1}
max={8}
step={1}
// TODO: Restore this once we upgrade Jest/jsdom to v15+. Right now passing
// a `step` prop always leads to jsdom thinking that validity.valid is false
// @see https://github.com/jsdom/jsdom/issues/2288
// step={1}
Comment on lines +35 to +38
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a chore ticket in for this? If not, I can add one quick.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you want to create one that'd be super awesome! 🙏

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or alternatively we can bogart #6813

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add one and link it to #6813 in just a short.

value={1}
icon="warning"
onChange={() => {}}
Expand Down
23 changes: 15 additions & 8 deletions src/components/form/field_number/field_number.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ import React, {
Ref,
FunctionComponent,
useState,
useEffect,
useCallback,
useRef,
} from 'react';
import { CommonProps } from '../../common';
import classNames from 'classnames';

import { useCombinedRefs } from '../../../services';
import { CommonProps } from '../../common';
import { IconType } from '../../icon';

import { EuiValidatableControl } from '../validatable_control';
Expand Down Expand Up @@ -104,10 +107,12 @@ export const EuiFieldNumber: FunctionComponent<EuiFieldNumberProps> = (
readOnly,
controlOnly,
onKeyUp,
onBlur,
...rest
} = props;

const _inputRef = useRef<HTMLInputElement | null>(null);
const combinedRefs = useCombinedRefs([_inputRef, inputRef]);

// Attempt to determine additional invalid state. The native number input
// will set :invalid state automatically, but we need to also set
// `aria-invalid` as well as display an icon. We also want to *not* set this on
Expand All @@ -122,6 +127,13 @@ export const EuiFieldNumber: FunctionComponent<EuiFieldNumberProps> = (
setIsNativelyInvalid(isInvalid);
}, []);

// Re-check validity whenever props that might affect validity are updated
useEffect(() => {
if (_inputRef.current) {
checkNativeValidity(_inputRef.current);
}
}, [value, min, max, step, checkNativeValidity]);

const numIconsClass = controlOnly
? false
: getFormControlClassNameForIconCount({
Expand Down Expand Up @@ -150,19 +162,14 @@ export const EuiFieldNumber: FunctionComponent<EuiFieldNumberProps> = (
placeholder={placeholder}
readOnly={readOnly}
className={classes}
ref={inputRef}
ref={combinedRefs}
aria-invalid={isInvalid || isNativelyInvalid}
onKeyUp={(e) => {
// Note that we can't use `onChange` because browsers don't emit change events
// for invalid text - see https://github.com/facebook/react/issues/16554
onKeyUp?.(e);
checkNativeValidity(e.currentTarget);
}}
onBlur={(e) => {
// Browsers can also set/determine validity (e.g. when `step` is undefined) on focus blur
onBlur?.(e);
checkNativeValidity(e.currentTarget);
}}
{...rest}
/>
</EuiValidatableControl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ exports[`EuiDualRange props maxInputProps allows overriding default props 1`] =
class="euiFormControlLayout__childrenWrapper"
>
<input
class="euiFieldNumber euiRangeInput euiRangeInput--max emotion-euiRangeInput"
aria-invalid="true"
class="euiFieldNumber euiRangeInput euiRangeInput--max emotion-euiRangeInput euiFormControlLayout--1icons"
max="100"
min="1"
name="undefined-maxValue"
Expand All @@ -233,6 +234,14 @@ exports[`EuiDualRange props maxInputProps allows overriding default props 1`] =
type="number"
value="123"
/>
<div
class="euiFormControlLayoutIcons euiFormControlLayoutIcons--right euiFormControlLayoutIcons--absolute"
>
<span
color="danger"
data-euiicon-type="warning"
/>
</div>
</div>
</div>
</div>
Expand Down Expand Up @@ -318,8 +327,9 @@ exports[`EuiDualRange props maxInputProps applies passed props to max input 1`]
class="euiFormControlLayout__childrenWrapper"
>
<input
aria-invalid="true"
aria-label="Max value"
class="euiFieldNumber euiRangeInput euiRangeInput--max emotion-euiRangeInput"
class="euiFieldNumber euiRangeInput euiRangeInput--max emotion-euiRangeInput euiFormControlLayout--1icons"
max="100"
min="1"
name="undefined-maxValue"
Expand All @@ -328,6 +338,14 @@ exports[`EuiDualRange props maxInputProps applies passed props to max input 1`]
type="number"
value="8"
/>
<div
class="euiFormControlLayoutIcons euiFormControlLayoutIcons--right euiFormControlLayoutIcons--absolute"
>
<span
color="danger"
data-euiicon-type="warning"
/>
</div>
</div>
</div>
</div>
Expand All @@ -344,7 +362,8 @@ exports[`EuiDualRange props minInputProps allows overriding default props 1`] =
class="euiFormControlLayout__childrenWrapper"
>
<input
class="euiFieldNumber euiRangeInput euiRangeInput--min emotion-euiRangeInput"
aria-invalid="true"
class="euiFieldNumber euiRangeInput euiRangeInput--min emotion-euiRangeInput euiFormControlLayout--1icons"
max="8"
min="0"
name="undefined-minValue"
Expand All @@ -353,6 +372,14 @@ exports[`EuiDualRange props minInputProps allows overriding default props 1`] =
type="number"
value="123"
/>
<div
class="euiFormControlLayoutIcons euiFormControlLayoutIcons--right euiFormControlLayoutIcons--absolute"
>
<span
color="danger"
data-euiicon-type="warning"
/>
</div>
</div>
</div>
<div
Expand Down Expand Up @@ -413,7 +440,8 @@ exports[`EuiDualRange props minInputProps allows overriding default props 1`] =
class="euiFormControlLayout__childrenWrapper"
>
<input
class="euiFieldNumber euiRangeInput euiRangeInput--max emotion-euiRangeInput"
aria-invalid="true"
class="euiFieldNumber euiRangeInput euiRangeInput--max emotion-euiRangeInput euiFormControlLayout--1icons"
max="100"
min="1"
name="undefined-maxValue"
Expand All @@ -422,6 +450,14 @@ exports[`EuiDualRange props minInputProps allows overriding default props 1`] =
type="number"
value="8"
/>
<div
class="euiFormControlLayoutIcons euiFormControlLayoutIcons--right euiFormControlLayoutIcons--absolute"
>
<span
color="danger"
data-euiicon-type="warning"
/>
</div>
</div>
</div>
</div>
Expand Down Expand Up @@ -508,7 +544,8 @@ exports[`EuiDualRange props minInputProps applies passed props to min input 1`]
class="euiFormControlLayout__childrenWrapper"
>
<input
class="euiFieldNumber euiRangeInput euiRangeInput--max emotion-euiRangeInput"
aria-invalid="true"
class="euiFieldNumber euiRangeInput euiRangeInput--max emotion-euiRangeInput euiFormControlLayout--1icons"
max="100"
min="1"
name="undefined-maxValue"
Expand All @@ -517,6 +554,14 @@ exports[`EuiDualRange props minInputProps applies passed props to min input 1`]
type="number"
value="8"
/>
<div
class="euiFormControlLayoutIcons euiFormControlLayoutIcons--right euiFormControlLayoutIcons--absolute"
>
<span
color="danger"
data-euiicon-type="warning"
/>
</div>
</div>
</div>
</div>
Expand Down
3 changes: 3 additions & 0 deletions upcoming_changelogs/7291.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**Bug fixes**

- Fixed controlled `EuiFieldNumbers` not correctly updating native validity state
Loading