diff --git a/assets/js/base/components/dropdown-selector/index.js b/assets/js/base/components/dropdown-selector/index.js index 54d1273c661..9ca3b63e7e4 100644 --- a/assets/js/base/components/dropdown-selector/index.js +++ b/assets/js/base/components/dropdown-selector/index.js @@ -2,9 +2,10 @@ * External dependencies */ import PropTypes from 'prop-types'; -import { useRef } from '@wordpress/element'; +import { useCallback, useRef } from '@wordpress/element'; import classNames from 'classnames'; import Downshift from 'downshift'; +import { __, sprintf } from '@wordpress/i18n'; /** * Internal dependencies @@ -13,33 +14,9 @@ import DropdownSelectorInput from './input'; import DropdownSelectorInputWrapper from './input-wrapper'; import DropdownSelectorMenu from './menu'; import DropdownSelectorSelectedChip from './selected-chip'; +import DropdownSelectorSelectedValue from './selected-value'; import './style.scss'; -/** - * State reducer for the downshift component. - * See: https://github.com/downshift-js/downshift#statereducer - */ -const stateReducer = ( state, changes ) => { - switch ( changes.type ) { - case Downshift.stateChangeTypes.keyDownEnter: - case Downshift.stateChangeTypes.clickItem: - return { - ...changes, - highlightedIndex: state.highlightedIndex, - isOpen: true, - inputValue: '', - }; - case Downshift.stateChangeTypes.blurInput: - case Downshift.stateChangeTypes.mouseUp: - return { - ...changes, - inputValue: state.inputValue, - }; - default: - return changes; - } -}; - /** * Component used to show an input box with a dropdown with suggestions. */ @@ -50,6 +27,7 @@ const DropdownSelector = ( { inputLabel = '', isDisabled = false, isLoading = false, + multiple = false, onChange = () => {}, options = [], } ) => { @@ -60,11 +38,33 @@ const DropdownSelector = ( { 'is-loading': isLoading, } ); - const focusInput = ( isOpen ) => { - if ( ! isOpen ) { - inputRef.current.focus(); - } - }; + /** + * State reducer for the downshift component. + * See: https://github.com/downshift-js/downshift#statereducer + */ + const stateReducer = useCallback( + ( state, changes ) => { + switch ( changes.type ) { + case Downshift.stateChangeTypes.keyDownEnter: + case Downshift.stateChangeTypes.clickItem: + return { + ...changes, + highlightedIndex: state.highlightedIndex, + isOpen: multiple, + inputValue: '', + }; + case Downshift.stateChangeTypes.blurInput: + case Downshift.stateChangeTypes.mouseUp: + return { + ...changes, + inputValue: state.inputValue, + }; + default: + return changes; + } + }, + [ multiple ] + ); return ( ( -
+
{ /* eslint-disable-next-line jsx-a11y/label-has-for */ }
+ ); +}; + +export default DropdownSelectorSelectedValue; diff --git a/assets/js/base/components/dropdown-selector/style.scss b/assets/js/base/components/dropdown-selector/style.scss index ce7fe0e4ba1..eb14b133233 100644 --- a/assets/js/base/components/dropdown-selector/style.scss +++ b/assets/js/base/components/dropdown-selector/style.scss @@ -5,7 +5,7 @@ } .wc-block-dropdown-selector__input-wrapper { - align-items: baseline; + align-items: center; border: 1px solid #9f9f9f; border-radius: 4px; cursor: text; @@ -26,13 +26,46 @@ } .wc-block-dropdown-selector__input { - background: transparent; - border: 0; - flex: 1; font-size: 0.8em; height: 1.8em; min-width: 0; - margin: 1.5px; + + .is-single & { + margin: 0 4px; + padding: 0; + width: 100%; + + &:hover, + &:focus, + &:active { + outline: 0; + } + + &:not(:first-child):focus { + margin-bottom: 1.5px; + margin-top: 1.5px; + } + + &:not(:first-child):not(:focus) { + @include visually-hidden(); + // Fixes an issue in Firefox that `flex: wrap` in the container was making + // this element to still occupy one line. + position: absolute; + } + } + + .is-multiple & { + flex: 1; + min-width: 0; + margin: 1.5px; + } +} + +// Visually hide the input +.is-single .wc-block-dropdown-selector__input:first-child, +.is-multiple .wc-block-dropdown-selector__input { + background: transparent; + border: 0; &:hover, &:focus, @@ -41,38 +74,78 @@ } } -.wc-block-dropdown-selector__selected-chip { - background-color: $core-grey-light-600; - border: 1px solid #9f9f9f; - border-radius: 4px; - color: $core-grey-dark-600; - display: inline-block; - font-size: 0.8em; - font-weight: inherit; - height: 1.8em; - margin: 1.5px; - padding: 0 0 0 0.3em; - white-space: nowrap; +.wc-block-dropdown-selector { + // Reset