From 6b32256dca9af83e7a6de139a0bf9699be2b4d37 Mon Sep 17 00:00:00 2001 From: DiegoAndai Date: Wed, 16 Oct 2024 17:23:55 -0300 Subject: [PATCH 1/2] Move onFilled and onEmpty out of shared useMemo --- .../src/FormControl/FormControl.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/mui-material/src/FormControl/FormControl.js b/packages/mui-material/src/FormControl/FormControl.js index 19698ecb6c3802..9b407377ced042 100644 --- a/packages/mui-material/src/FormControl/FormControl.js +++ b/packages/mui-material/src/FormControl/FormControl.js @@ -191,6 +191,14 @@ const FormControl = React.forwardRef(function FormControl(inProps, ref) { }; } + const onFilled = React.useCallback(() => { + setFilled(true); + }, []); + + const onEmpty = React.useCallback(() => { + setFilled(false); + }, []); + const childContext = React.useMemo(() => { return { adornedStart, @@ -206,15 +214,11 @@ const FormControl = React.forwardRef(function FormControl(inProps, ref) { onBlur: () => { setFocused(false); }, - onEmpty: () => { - setFilled(false); - }, - onFilled: () => { - setFilled(true); - }, onFocus: () => { setFocused(true); }, + onEmpty, + onFilled, registerEffect, required, variant, @@ -229,6 +233,8 @@ const FormControl = React.forwardRef(function FormControl(inProps, ref) { fullWidth, hiddenLabel, registerEffect, + onEmpty, + onFilled, required, size, variant, From 515ace9d0ed4eea6d97b99c9b6d6a315077bea6a Mon Sep 17 00:00:00 2001 From: DiegoAndai Date: Tue, 7 Jan 2025 17:02:59 -0300 Subject: [PATCH 2/2] Add test to simulate autofill --- .../src/TextField/TextField.test.js | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/packages/mui-material/src/TextField/TextField.test.js b/packages/mui-material/src/TextField/TextField.test.js index e1392563a0d5d7..adc29954d2ce2b 100644 --- a/packages/mui-material/src/TextField/TextField.test.js +++ b/packages/mui-material/src/TextField/TextField.test.js @@ -303,4 +303,28 @@ describe('', () => { expect(getByRole('textbox')).to.have.attribute('data-testid', 'input-element'); }); }); + + describe('autofill', () => { + it('should be filled after auto fill event', () => { + function AutoFillComponentTest() { + const [value, setValue] = React.useState(''); + return ( + setValue(event.target.value)} + label="test" + variant="standard" + slotProps={{ + htmlInput: { 'data-testid': 'htmlInput' }, + inputLabel: { 'data-testid': 'label' }, + }} + /> + ); + } + + const { getByTestId } = render(); + fireEvent.animationStart(getByTestId('htmlInput'), { animationName: 'mui-auto-fill' }); + expect(getByTestId('label').getAttribute('data-shrink')).to.equal('true'); + }); + }); });