Skip to content

Commit cb2a191

Browse files
authored
[material-ui][TextField] Fix filled state to be synced with autofill (#44135)
1 parent ca6afdb commit cb2a191

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

packages/mui-material/src/FormControl/FormControl.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,14 @@ const FormControl = React.forwardRef(function FormControl(inProps, ref) {
193193
};
194194
}
195195

196+
const onFilled = React.useCallback(() => {
197+
setFilled(true);
198+
}, []);
199+
200+
const onEmpty = React.useCallback(() => {
201+
setFilled(false);
202+
}, []);
203+
196204
const childContext = React.useMemo(() => {
197205
return {
198206
adornedStart,
@@ -208,15 +216,11 @@ const FormControl = React.forwardRef(function FormControl(inProps, ref) {
208216
onBlur: () => {
209217
setFocused(false);
210218
},
211-
onEmpty: () => {
212-
setFilled(false);
213-
},
214-
onFilled: () => {
215-
setFilled(true);
216-
},
217219
onFocus: () => {
218220
setFocused(true);
219221
},
222+
onEmpty,
223+
onFilled,
220224
registerEffect,
221225
required,
222226
variant,
@@ -231,6 +235,8 @@ const FormControl = React.forwardRef(function FormControl(inProps, ref) {
231235
fullWidth,
232236
hiddenLabel,
233237
registerEffect,
238+
onEmpty,
239+
onFilled,
234240
required,
235241
size,
236242
variant,

packages/mui-material/src/TextField/TextField.test.js

+24
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,28 @@ describe('<TextField />', () => {
303303
expect(getByRole('textbox')).to.have.attribute('data-testid', 'input-element');
304304
});
305305
});
306+
307+
describe('autofill', () => {
308+
it('should be filled after auto fill event', () => {
309+
function AutoFillComponentTest() {
310+
const [value, setValue] = React.useState('');
311+
return (
312+
<TextField
313+
value={value}
314+
onChange={(event) => setValue(event.target.value)}
315+
label="test"
316+
variant="standard"
317+
slotProps={{
318+
htmlInput: { 'data-testid': 'htmlInput' },
319+
inputLabel: { 'data-testid': 'label' },
320+
}}
321+
/>
322+
);
323+
}
324+
325+
const { getByTestId } = render(<AutoFillComponentTest />);
326+
fireEvent.animationStart(getByTestId('htmlInput'), { animationName: 'mui-auto-fill' });
327+
expect(getByTestId('label').getAttribute('data-shrink')).to.equal('true');
328+
});
329+
});
306330
});

0 commit comments

Comments
 (0)