Skip to content

Commit 98ff79d

Browse files
committed
fix(Autocomplete): decrease padding when icon buttons are not rendered
fix #19047
1 parent c8d9df0 commit 98ff79d

File tree

2 files changed

+127
-6
lines changed

2 files changed

+127
-6
lines changed

packages/material-ui-lab/src/Autocomplete/Autocomplete.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,17 @@ export const styles = theme => ({
3232
margin: 2,
3333
maxWidth: 'calc(100% - 4px)',
3434
},
35+
hasPopupIcon: {},
36+
hasClearIcon: {},
3537
/* Styles applied to the Input element. */
3638
inputRoot: {
3739
flexWrap: 'wrap',
38-
paddingRight: 62,
40+
'$hasPopupIcon &, $hasClearIcon &': {
41+
paddingRight: 31,
42+
},
43+
'$hasPopupIcon$hasClearIcon &': {
44+
paddingRight: 62,
45+
},
3946
'& $input': {
4047
width: 0,
4148
minWidth: 30,
@@ -59,7 +66,12 @@ export const styles = theme => ({
5966
},
6067
'&[class*="MuiOutlinedInput-root"]': {
6168
padding: 9,
62-
paddingRight: 62,
69+
'$hasPopupIcon &, $hasClearIcon &': {
70+
paddingRight: 31,
71+
},
72+
'$hasPopupIcon$hasClearIcon &': {
73+
paddingRight: 62,
74+
},
6375
'& $input': {
6476
padding: '9.5px 4px',
6577
},
@@ -72,7 +84,12 @@ export const styles = theme => ({
7284
},
7385
'&[class*="MuiOutlinedInput-root"][class*="MuiOutlinedInput-marginDense"]': {
7486
padding: 6,
75-
paddingRight: 62,
87+
'$hasPopupIcon &, $hasClearIcon &': {
88+
paddingRight: 31,
89+
},
90+
'$hasPopupIcon$hasClearIcon &': {
91+
paddingRight: 62,
92+
},
7693
'& $input': {
7794
padding: '4.5px 4px',
7895
},
@@ -345,6 +362,9 @@ const Autocomplete = React.forwardRef(function Autocomplete(props, ref) {
345362
);
346363
};
347364

365+
const hasClearIcon = !disableClearable && !disabled;
366+
const hasPopupIcon = (!freeSolo || forcePopupIcon === true) && forcePopupIcon !== false;
367+
348368
return (
349369
<React.Fragment>
350370
<div
@@ -353,6 +373,8 @@ const Autocomplete = React.forwardRef(function Autocomplete(props, ref) {
353373
classes.root,
354374
{
355375
[classes.focused]: focused,
376+
[classes.hasClearIcon]: hasClearIcon,
377+
[classes.hasPopupIcon]: hasPopupIcon,
356378
},
357379
className,
358380
)}
@@ -369,7 +391,7 @@ const Autocomplete = React.forwardRef(function Autocomplete(props, ref) {
369391
startAdornment,
370392
endAdornment: (
371393
<div className={classes.endAdornment}>
372-
{disableClearable || disabled ? null : (
394+
{hasClearIcon ? (
373395
<IconButton
374396
{...getClearProps()}
375397
aria-label={clearText}
@@ -380,9 +402,9 @@ const Autocomplete = React.forwardRef(function Autocomplete(props, ref) {
380402
>
381403
{closeIcon}
382404
</IconButton>
383-
)}
405+
) : null}
384406

385-
{(!freeSolo || forcePopupIcon === true) && forcePopupIcon !== false ? (
407+
{hasPopupIcon ? (
386408
<IconButton
387409
{...getPopupIndicatorProps()}
388410
disabled={disabled}

packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import describeConformance from '@material-ui/core/test-utils/describeConformanc
55
import consoleErrorMock from 'test/utils/consoleErrorMock';
66
import { spy } from 'sinon';
77
import { createClientRender, fireEvent } from 'test/utils/createClientRender';
8+
import { createRender } from '@material-ui/core/test-utils/createRender';
89
import Autocomplete from './Autocomplete';
910
import TextField from '@material-ui/core/TextField';
1011

@@ -39,6 +40,24 @@ describe('<Autocomplete />', () => {
3940
document.activeElement.blur();
4041
expect(input.value).to.equal('');
4142
});
43+
44+
it('should apply the hasClearIcon class', () => {
45+
const { container } = render(
46+
<Autocomplete renderInput={params => <TextField {...params} />} />,
47+
);
48+
expect(container.querySelector('[class*="MuiAutocomplete-root"]')).to.have.class(
49+
classes.hasClearIcon,
50+
);
51+
});
52+
53+
it('should apply the hasPopupIcon class', () => {
54+
const { container } = render(
55+
<Autocomplete renderInput={params => <TextField {...params} />} />,
56+
);
57+
expect(container.querySelector('[class*="MuiAutocomplete-root"]')).to.have.class(
58+
classes.hasPopupIcon,
59+
);
60+
});
4261
});
4362

4463
describe('multiple', () => {
@@ -547,6 +566,71 @@ describe('<Autocomplete />', () => {
547566
);
548567
expect(queryByTitle('Clear')).to.be.null;
549568
});
569+
570+
it('should not apply the hasClearIcon class', () => {
571+
const { container } = render(
572+
<Autocomplete
573+
disabled
574+
options={['one', 'two', 'three']}
575+
renderInput={params => <TextField {...params} />}
576+
/>,
577+
);
578+
expect(container.querySelector('[class*="MuiAutocomplete-root"]')).not.to.have.class(
579+
classes.hasClearIcon,
580+
);
581+
});
582+
583+
it('should still apply the hasPopupIcon class', () => {
584+
const { container } = render(
585+
<Autocomplete
586+
disabled
587+
options={['one', 'two', 'three']}
588+
renderInput={params => <TextField {...params} />}
589+
/>,
590+
);
591+
expect(container.querySelector('[class*="MuiAutocomplete-root"]')).to.have.class(
592+
classes.hasPopupIcon,
593+
);
594+
});
595+
});
596+
597+
describe('prop: disableClearable', () => {
598+
it('should not render the clear button', () => {
599+
const { queryByTitle } = render(
600+
<Autocomplete
601+
disableClearable
602+
options={['one', 'two', 'three']}
603+
renderInput={params => <TextField {...params} />}
604+
/>,
605+
);
606+
expect(queryByTitle('Clear')).to.be.null;
607+
});
608+
609+
it('should still apply the hasPopupIcon class', () => {
610+
const { container } = render(
611+
<Autocomplete
612+
disableClearable
613+
options={['one', 'two', 'three']}
614+
renderInput={params => <TextField {...params} />}
615+
/>,
616+
);
617+
expect(container.querySelector('[class*="MuiAutocomplete-root"]')).to.have.class(
618+
classes.hasPopupIcon,
619+
);
620+
});
621+
622+
it('should not apply the hasClearIcon class', () => {
623+
const { container } = render(
624+
<Autocomplete
625+
disableClearable
626+
options={['one', 'two', 'three']}
627+
renderInput={params => <TextField {...params} />}
628+
/>,
629+
);
630+
expect(container.querySelector('[class*="MuiAutocomplete-root"]')).not.to.have.class(
631+
classes.hasClearIcon,
632+
);
633+
});
550634
});
551635
});
552636

@@ -835,6 +919,21 @@ describe('<Autocomplete />', () => {
835919
fireEvent.keyDown(document.activeElement, { key: 'Enter' });
836920
expect(container.querySelectorAll('[class*="MuiChip-root"]')).to.have.length(3);
837921
});
922+
923+
it('should not apply hasPopupIcon class', () => {
924+
const handleChange = spy();
925+
const options = [{ name: 'foo' }];
926+
const { container } = render(
927+
<Autocomplete
928+
freeSolo
929+
onChange={handleChange}
930+
options={options}
931+
getOptionLabel={option => option.name}
932+
renderInput={params => <TextField {...params} autoFocus />}
933+
/>,
934+
);
935+
expect(container).not.to.have.class(classes.hasPopupIcon);
936+
});
838937
});
839938

840939
describe('prop: onInputChange', () => {

0 commit comments

Comments
 (0)