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

[material-ui][TextField] Fix filled state to be synced with autofill #44135

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
18 changes: 12 additions & 6 deletions packages/mui-material/src/FormControl/FormControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -231,6 +235,8 @@ const FormControl = React.forwardRef(function FormControl(inProps, ref) {
fullWidth,
hiddenLabel,
registerEffect,
onEmpty,
onFilled,
required,
size,
variant,
Expand Down
24 changes: 24 additions & 0 deletions packages/mui-material/src/TextField/TextField.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
Loading