Skip to content

Commit b08e43d

Browse files
committed
Add more fixes for invalid states not updating as expected
+ remove `onBlur` logic - the useEffect now handles that on mount, and blur behavior should no longer be necessary
1 parent 503a914 commit b08e43d

2 files changed

Lines changed: 80 additions & 18 deletions

File tree

src/components/form/field_number/field_number.spec.tsx

Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,6 @@ describe('EuiFieldNumber', () => {
5353
checkIsInvalid();
5454
});
5555

56-
it('shows invalid state on blur', () => {
57-
cy.mount(<EuiFieldNumber max={1} value={2} />);
58-
checkIsValid();
59-
cy.get('input').click();
60-
cy.get('body').click('bottomRight');
61-
checkIsInvalid();
62-
});
63-
6456
it('does not show invalid state on decimal values by default', () => {
6557
cy.mount(<EuiFieldNumber />);
6658
checkIsValid();
@@ -103,5 +95,82 @@ describe('EuiFieldNumber', () => {
10395
cy.get('input').clear().type('2');
10496
checkIsValid();
10597
});
98+
99+
describe('checks/updates invalid state when props that would affect validity change', () => {
100+
it('min', () => {
101+
const UpdatedEuiFieldNumber = () => {
102+
const [min, setMin] = useState<number | undefined>();
103+
return (
104+
<>
105+
<EuiFieldNumber min={min} />
106+
<button id="setInvalidMin" onClick={() => setMin(100)}>
107+
Set invalid min
108+
</button>
109+
<button id="setValidMin" onClick={() => setMin(0)}>
110+
Change valid min
111+
</button>
112+
</>
113+
);
114+
};
115+
cy.mount(<UpdatedEuiFieldNumber />);
116+
cy.get('input').type('1');
117+
checkIsValid();
118+
119+
cy.get('#setInvalidMin').click();
120+
checkIsInvalid();
121+
cy.get('#setValidMin').click();
122+
checkIsValid();
123+
});
124+
125+
it('max', () => {
126+
const UpdatedEuiFieldNumber = () => {
127+
const [max, setMax] = useState<number | undefined>();
128+
return (
129+
<>
130+
<EuiFieldNumber max={max} />
131+
<button id="setInvalidMax" onClick={() => setMax(0)}>
132+
Set invalid max
133+
</button>
134+
<button id="setValidMax" onClick={() => setMax(10)}>
135+
Change valid max
136+
</button>
137+
</>
138+
);
139+
};
140+
cy.mount(<UpdatedEuiFieldNumber />);
141+
cy.get('input').type('1');
142+
checkIsValid();
143+
144+
cy.get('#setInvalidMax').click();
145+
checkIsInvalid();
146+
cy.get('#setValidMax').click();
147+
checkIsValid();
148+
});
149+
150+
it('step', () => {
151+
const UpdatedEuiFieldNumber = () => {
152+
const [step, setStep] = useState<number | undefined>();
153+
return (
154+
<>
155+
<EuiFieldNumber step={step} />
156+
<button id="setInvalidStep" onClick={() => setStep(1.5)}>
157+
Set invalid step
158+
</button>
159+
<button id="setValidStep" onClick={() => setStep(1)}>
160+
Change valid step
161+
</button>
162+
</>
163+
);
164+
};
165+
cy.mount(<UpdatedEuiFieldNumber />);
166+
cy.get('input').type('1');
167+
checkIsValid();
168+
169+
cy.get('#setInvalidStep').click();
170+
checkIsInvalid();
171+
cy.get('#setValidStep').click();
172+
checkIsValid();
173+
});
174+
});
106175
});
107176
});

src/components/form/field_number/field_number.tsx

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ export const EuiFieldNumber: FunctionComponent<EuiFieldNumberProps> = (
107107
readOnly,
108108
controlOnly,
109109
onKeyUp,
110-
onBlur,
111110
...rest
112111
} = props;
113112

@@ -128,13 +127,12 @@ export const EuiFieldNumber: FunctionComponent<EuiFieldNumberProps> = (
128127
setIsNativelyInvalid(isInvalid);
129128
}, []);
130129

131-
// Ensure controlled `value/onChange` fields are checked for native validity
130+
// Re-check validity whenever props that might affect validity are updated
132131
useEffect(() => {
133-
const isControlledValue = value != null;
134-
if (isControlledValue && _inputRef.current) {
132+
if (_inputRef.current) {
135133
checkNativeValidity(_inputRef.current);
136134
}
137-
}, [value, checkNativeValidity]);
135+
}, [value, min, max, step, checkNativeValidity]);
138136

139137
const numIconsClass = controlOnly
140138
? false
@@ -172,11 +170,6 @@ export const EuiFieldNumber: FunctionComponent<EuiFieldNumberProps> = (
172170
onKeyUp?.(e);
173171
checkNativeValidity(e.currentTarget);
174172
}}
175-
onBlur={(e) => {
176-
// Browsers can also set/determine validity (e.g. when `step` is undefined) on focus blur
177-
onBlur?.(e);
178-
checkNativeValidity(e.currentTarget);
179-
}}
180173
{...rest}
181174
/>
182175
</EuiValidatableControl>

0 commit comments

Comments
 (0)