From 0fd024209dd1cff6103f9ed20f6b8ed84de5e85e Mon Sep 17 00:00:00 2001 From: Andy Edwards Date: Wed, 15 Jan 2020 12:14:50 -0600 Subject: [PATCH] fix(Autocomplete): decrease padding when icon buttons are not rendered fix #19047 --- docs/pages/api/autocomplete.md | 2 + .../src/Autocomplete/Autocomplete.js | 36 +++++-- .../src/Autocomplete/Autocomplete.test.js | 98 +++++++++++++++++++ 3 files changed, 130 insertions(+), 6 deletions(-) diff --git a/docs/pages/api/autocomplete.md b/docs/pages/api/autocomplete.md index a56335724980d0..60dc0e41c369bf 100644 --- a/docs/pages/api/autocomplete.md +++ b/docs/pages/api/autocomplete.md @@ -91,6 +91,8 @@ Any other props supplied will be provided to the root element (native element). | focused | .Mui-focused | Pseudo-class applied to the root element if focused. | tag | .MuiAutocomplete-tag | Styles applied to the tag elements, e.g. the chips. | tagSizeSmall | .MuiAutocomplete-tagSizeSmall | Styles applied to the tag elements, e.g. the chips if `size="small"`. +| hasPopupIcon | .MuiAutocomplete-hasPopupIcon | Styles applied when the popup icon is rendered. +| hasClearIcon | .MuiAutocomplete-hasClearIcon | Styles applied when the clear icon is rendered. | inputRoot | .MuiAutocomplete-inputRoot | Styles applied to the Input element. | input | .MuiAutocomplete-input | Styles applied to the input element. | inputFocused | .MuiAutocomplete-inputFocused | Styles applied to the input element if tag focused. diff --git a/packages/material-ui-lab/src/Autocomplete/Autocomplete.js b/packages/material-ui-lab/src/Autocomplete/Autocomplete.js index eebefc730138c6..708accd968aff2 100644 --- a/packages/material-ui-lab/src/Autocomplete/Autocomplete.js +++ b/packages/material-ui-lab/src/Autocomplete/Autocomplete.js @@ -32,10 +32,19 @@ export const styles = theme => ({ margin: 2, maxWidth: 'calc(100% - 4px)', }, + /* Styles applied when the popup icon is rendered. */ + hasPopupIcon: {}, + /* Styles applied when the clear icon is rendered. */ + hasClearIcon: {}, /* Styles applied to the Input element. */ inputRoot: { flexWrap: 'wrap', - paddingRight: 62, + '$hasPopupIcon &, $hasClearIcon &': { + paddingRight: 31, + }, + '$hasPopupIcon$hasClearIcon &': { + paddingRight: 62, + }, '& $input': { width: 0, minWidth: 30, @@ -59,7 +68,12 @@ export const styles = theme => ({ }, '&[class*="MuiOutlinedInput-root"]': { padding: 9, - paddingRight: 62, + '$hasPopupIcon &, $hasClearIcon &': { + paddingRight: 31, + }, + '$hasPopupIcon$hasClearIcon &': { + paddingRight: 62, + }, '& $input': { padding: '9.5px 4px', }, @@ -72,7 +86,12 @@ export const styles = theme => ({ }, '&[class*="MuiOutlinedInput-root"][class*="MuiOutlinedInput-marginDense"]': { padding: 6, - paddingRight: 62, + '$hasPopupIcon &, $hasClearIcon &': { + paddingRight: 31, + }, + '$hasPopupIcon$hasClearIcon &': { + paddingRight: 62, + }, '& $input': { padding: '4.5px 4px', }, @@ -345,6 +364,9 @@ const Autocomplete = React.forwardRef(function Autocomplete(props, ref) { ); }; + const hasClearIcon = !disableClearable && !disabled; + const hasPopupIcon = (!freeSolo || forcePopupIcon === true) && forcePopupIcon !== false; + return (
- {disableClearable || disabled ? null : ( + {hasClearIcon ? ( {closeIcon} - )} + ) : null} - {(!freeSolo || forcePopupIcon === true) && forcePopupIcon !== false ? ( + {hasPopupIcon ? ( ', () => { document.activeElement.blur(); expect(input.value).to.equal(''); }); + + it('should apply the hasClearIcon class', () => { + const { container } = render( + } />, + ); + expect(container.querySelector('[class*="MuiAutocomplete-root"]')).to.have.class( + classes.hasClearIcon, + ); + }); + + it('should apply the hasPopupIcon class', () => { + const { container } = render( + } />, + ); + expect(container.querySelector('[class*="MuiAutocomplete-root"]')).to.have.class( + classes.hasPopupIcon, + ); + }); }); describe('multiple', () => { @@ -547,6 +565,71 @@ describe('', () => { ); expect(queryByTitle('Clear')).to.be.null; }); + + it('should not apply the hasClearIcon class', () => { + const { container } = render( + } + />, + ); + expect(container.querySelector('[class*="MuiAutocomplete-root"]')).not.to.have.class( + classes.hasClearIcon, + ); + }); + + it('should still apply the hasPopupIcon class', () => { + const { container } = render( + } + />, + ); + expect(container.querySelector('[class*="MuiAutocomplete-root"]')).to.have.class( + classes.hasPopupIcon, + ); + }); + }); + + describe('prop: disableClearable', () => { + it('should not render the clear button', () => { + const { queryByTitle } = render( + } + />, + ); + expect(queryByTitle('Clear')).to.be.null; + }); + + it('should still apply the hasPopupIcon class', () => { + const { container } = render( + } + />, + ); + expect(container.querySelector('[class*="MuiAutocomplete-root"]')).to.have.class( + classes.hasPopupIcon, + ); + }); + + it('should not apply the hasClearIcon class', () => { + const { container } = render( + } + />, + ); + expect(container.querySelector('[class*="MuiAutocomplete-root"]')).not.to.have.class( + classes.hasClearIcon, + ); + }); }); }); @@ -835,6 +918,21 @@ describe('', () => { fireEvent.keyDown(document.activeElement, { key: 'Enter' }); expect(container.querySelectorAll('[class*="MuiChip-root"]')).to.have.length(3); }); + + it('should not apply hasPopupIcon class', () => { + const handleChange = spy(); + const options = [{ name: 'foo' }]; + const { container } = render( + option.name} + renderInput={params => } + />, + ); + expect(container).not.to.have.class(classes.hasPopupIcon); + }); }); describe('prop: onInputChange', () => {