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

[TextField] Improve baseline alignment with start adornment #24742

Merged
Merged
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
10 changes: 9 additions & 1 deletion packages/material-ui/src/InputAdornment/InputAdornment.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,15 @@ const InputAdornment = React.forwardRef(function InputAdornment(props, ref) {
{typeof children === 'string' && !disableTypography ? (
<Typography color="textSecondary">{children}</Typography>
) : (
children
<React.Fragment>
{/* To have the correct vertical alignment baseline */}
{position === 'start' ? (
/* notranslate needed while Google Translate will not fix zero-width space issue */
/* eslint-disable-next-line react/no-danger */
<span className="notranslate" dangerouslySetInnerHTML={{ __html: '&#8203;' }} />
) : null}
{children}
</React.Fragment>
)}
</Component>
</FormControlContext.Provider>
Expand Down
17 changes: 16 additions & 1 deletion packages/material-ui/src/InputAdornment/InputAdornment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ describe('<InputAdornment />', () => {

it('should render children', () => {
const { container } = render(
<InputAdornment position="start">
<InputAdornment position="end">
<div>foo</div>
</InputAdornment>,
);
Expand All @@ -175,6 +175,21 @@ describe('<InputAdornment />', () => {
expect(adornment.firstChild).to.have.property('nodeName', 'DIV');
});

describe('prop: position', () => {
it('should render span for vertical baseline alignment', () => {
const { container } = render(
<InputAdornment position="start">
<div>foo</div>
</InputAdornment>,
);
const adornment = container.firstChild;

expect(adornment.firstChild).to.have.tagName('span');
expect(adornment.firstChild).to.have.class('notranslate');
expect(adornment.childNodes[1]).to.have.tagName('div');
});
});

it('applies a size small class inside <FormControl size="small" />', () => {
const { getByTestId } = render(
<FormControl size="small">
Expand Down
55 changes: 55 additions & 0 deletions test/regressions/tests/TextField/BaselineAlignTextField.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import * as React from 'react';
import TextField from '@material-ui/core/TextField';
import Visibility from '@material-ui/icons/Visibility';
import InputAdornment from '@material-ui/core/InputAdornment';

export default function BaselineAlignTextField() {
return (
<div>
<div
style={{
display: 'flex',
flexDirection: 'row',
alignItems: 'baseline',
}}
>
Base
<TextField
label="label"
placeholder="placeholder"
variant="standard"
InputProps={{
startAdornment: (
<InputAdornment position="start">
<Visibility />
</InputAdornment>
),
}}
/>
Base
</div>
<div
style={{
display: 'flex',
flexDirection: 'row',
alignItems: 'baseline',
}}
>
Base
<TextField
label="label"
placeholder="placeholder"
variant="standard"
InputProps={{
endAdornment: (
<InputAdornment position="end">
<Visibility />
</InputAdornment>
),
}}
/>
Base
</div>
</div>
);
}