Skip to content

Commit

Permalink
[material-ui][TextField] Fix filled state to be synced with autofill (#…
Browse files Browse the repository at this point in the history
DiegoAndai authored Jan 20, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent ca6afdb commit cb2a191
Showing 2 changed files with 36 additions and 6 deletions.
18 changes: 12 additions & 6 deletions packages/mui-material/src/FormControl/FormControl.js
Original file line number Diff line number Diff line change
@@ -193,6 +193,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,
@@ -208,15 +216,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,
@@ -231,6 +235,8 @@ const FormControl = React.forwardRef(function FormControl(inProps, ref) {
fullWidth,
hiddenLabel,
registerEffect,
onEmpty,
onFilled,
required,
size,
variant,
24 changes: 24 additions & 0 deletions packages/mui-material/src/TextField/TextField.test.js
Original file line number Diff line number Diff line change
@@ -303,4 +303,28 @@ describe('<TextField />', () => {
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 (
<TextField
value={value}
onChange={(event) => setValue(event.target.value)}
label="test"
variant="standard"
slotProps={{
htmlInput: { 'data-testid': 'htmlInput' },
inputLabel: { 'data-testid': 'label' },
}}
/>
);
}

const { getByTestId } = render(<AutoFillComponentTest />);
fireEvent.animationStart(getByTestId('htmlInput'), { animationName: 'mui-auto-fill' });
expect(getByTestId('label').getAttribute('data-shrink')).to.equal('true');
});
});
});

0 comments on commit cb2a191

Please sign in to comment.