Skip to content

Commit

Permalink
SelectControl: Fix multiple prop styling (#47893)
Browse files Browse the repository at this point in the history
* SelectControl: Fix `multiple` prop styling

* Update readme

* Add changelog

* Refactor padding code

* Add comment

* Allow overflow scrolling when `multiple`

* Update snapshot
  • Loading branch information
mirka authored and ntsekouras committed Feb 13, 2023
1 parent 80aaf64 commit a4e7a4b
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 39 deletions.
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Bug Fix

- `SelectControl`: Fix styling when `multiple` prop is enabled ([#47893](https://github.com/WordPress/gutenberg/pull/43213)).

### Enhancements

- `ColorPalette`: ensure text label contrast checking works with CSS variables ([#47373](https://github.com/WordPress/gutenberg/pull/47373)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ exports[`DimensionControl rendering renders with custom sizes 1`] = `
width: 100%;
max-width: none;
cursor: pointer;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
font-size: 16px;
Expand All @@ -126,6 +125,7 @@ exports[`DimensionControl rendering renders with custom sizes 1`] = `
padding-bottom: 0;
padding-left: 8px;
padding-right: 26px;
overflow: hidden;
}
@media ( min-width: 600px ) {
Expand Down Expand Up @@ -387,7 +387,6 @@ exports[`DimensionControl rendering renders with defaults 1`] = `
width: 100%;
max-width: none;
cursor: pointer;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
font-size: 16px;
Expand All @@ -397,6 +396,7 @@ exports[`DimensionControl rendering renders with defaults 1`] = `
padding-bottom: 0;
padding-left: 8px;
padding-right: 26px;
overflow: hidden;
}
@media ( min-width: 600px ) {
Expand Down Expand Up @@ -668,7 +668,6 @@ exports[`DimensionControl rendering renders with icon and custom icon label 1`]
width: 100%;
max-width: none;
cursor: pointer;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
font-size: 16px;
Expand All @@ -678,6 +677,7 @@ exports[`DimensionControl rendering renders with icon and custom icon label 1`]
padding-bottom: 0;
padding-left: 8px;
padding-right: 26px;
overflow: hidden;
}
@media ( min-width: 600px ) {
Expand Down Expand Up @@ -961,7 +961,6 @@ exports[`DimensionControl rendering renders with icon and default icon label 1`]
width: 100%;
max-width: none;
cursor: pointer;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
font-size: 16px;
Expand All @@ -971,6 +970,7 @@ exports[`DimensionControl rendering renders with icon and default icon label 1`]
padding-bottom: 0;
padding-left: 8px;
padding-right: 26px;
overflow: hidden;
}
@media ( min-width: 600px ) {
Expand Down
4 changes: 3 additions & 1 deletion packages/components/src/select-control/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ If this property is added, a help text will be generated using help property as

#### multiple

If this property is added, multiple values can be selected. The value passed should be an array.
If this property is added, multiple values can be selected. The `value` passed should be an array.

In most cases, it is preferable to use the `FormTokenField` or `CheckboxControl` components instead.

- Type: `Boolean`
- Required: No
Expand Down
4 changes: 3 additions & 1 deletion packages/components/src/select-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ function UnforwardedSelectControl(
isFocused={ isFocused }
label={ label }
size={ size }
suffix={ suffix || <SelectControlChevronDown /> }
suffix={
suffix || ( ! multiple && <SelectControlChevronDown /> )
}
prefix={ prefix }
labelPosition={ labelPosition }
__next36pxDefaultSize={ __next36pxDefaultSize }
Expand Down
10 changes: 0 additions & 10 deletions packages/components/src/select-control/style.scss
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
.components-select-control__input {
background: $white;
height: 36px;
line-height: 36px;
margin: 1px;
outline: 0;
width: 100%;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0) !important;

@include break-medium() {
height: 28px;
line-height: 28px;
}
}

@media (max-width: #{ ($break-medium) }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import type { SelectControlProps } from '../types';
import InputControlSuffixWrapper from '../../input-control/input-suffix-wrapper';

interface SelectProps
extends Pick< SelectControlProps, '__next36pxDefaultSize' | 'disabled' > {
extends Pick<
SelectControlProps,
'__next36pxDefaultSize' | 'disabled' | 'multiple'
> {
// Using `selectSize` instead of `size` to avoid a type conflict with the
// `size` HTML attribute of the `select` element.
selectSize?: SelectControlProps[ 'size' ];
Expand Down Expand Up @@ -50,8 +53,15 @@ const fontSizeStyles = ( { selectSize = 'default' }: SelectProps ) => {

const sizeStyles = ( {
__next36pxDefaultSize,
multiple,
selectSize = 'default',
}: SelectProps ) => {
if ( multiple ) {
// When `multiple`, just use the native browser styles
// without setting explicit height.
return;
}

const sizes = {
default: {
height: 36,
Expand Down Expand Up @@ -91,33 +101,37 @@ export const chevronIconSize = 18;

const sizePaddings = ( {
__next36pxDefaultSize,
multiple,
selectSize = 'default',
}: SelectProps ) => {
const iconWidth = chevronIconSize;

const sizes = {
default: {
paddingLeft: 16,
paddingRight: 16 + iconWidth,
},
small: {
paddingLeft: 8,
paddingRight: 8 + iconWidth,
},
'__unstable-large': {
paddingLeft: 16,
paddingRight: 16 + iconWidth,
},
const padding = {
default: 16,
small: 8,
'__unstable-large': 16,
};

if ( ! __next36pxDefaultSize ) {
sizes.default = {
paddingLeft: 8,
paddingRight: 8 + iconWidth,
};
padding.default = 8;
}

return rtl( sizes[ selectSize ] || sizes.default );
const selectedPadding = padding[ selectSize ] || padding.default;

return rtl( {
paddingLeft: selectedPadding,
paddingRight: selectedPadding + chevronIconSize,
...( multiple
? {
paddingTop: selectedPadding,
paddingBottom: selectedPadding,
}
: {} ),
} );
};

const overflowStyles = ( { multiple }: SelectProps ) => {
return {
overflow: multiple ? 'auto' : 'hidden',
};
};

// TODO: Resolve need to use &&& to increase specificity
Expand All @@ -137,14 +151,14 @@ export const Select = styled.select< SelectProps >`
width: 100%;
max-width: none;
cursor: pointer;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
${ disabledStyles };
${ fontSizeStyles };
${ sizeStyles };
${ sizePaddings };
${ overflowStyles }
}
`;

Expand Down
4 changes: 3 additions & 1 deletion packages/components/src/select-control/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export interface SelectControlProps
>,
Pick< BaseControlProps, 'help' | '__nextHasNoMarginBottom' > {
/**
* If this property is added, multiple values can be selected. The value passed should be an array.
* If this property is added, multiple values can be selected. The `value` passed should be an array.
*
* In most cases, it is preferable to use the `FormTokenField` or `CheckboxControl` components instead.
*
* @default false
*/
Expand Down

0 comments on commit a4e7a4b

Please sign in to comment.