-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ecd166c
commit d077dfd
Showing
14 changed files
with
499 additions
and
3 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
client/src/components/SearchableDropdownField/SearchableDropdownField.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
import AsyncSelect from 'react-select/async'; | ||
import backend from 'lib/Backend'; | ||
import classNames from 'classnames'; | ||
import Config from 'lib/Config'; | ||
import debounce from 'debounce-promise'; | ||
import EmotionCssCacheProvider from 'containers/EmotionCssCacheProvider/EmotionCssCacheProvider'; | ||
import i18n from 'i18n'; | ||
import fieldHolder from 'components/FieldHolder/FieldHolder'; | ||
import PropTypes from 'prop-types'; | ||
import React, { useState, useEffect, createRef } from 'react'; | ||
import Select from 'react-select'; | ||
import url from 'url'; | ||
import FormConstants from '../Form/FormConstants'; | ||
|
||
const SearchableDropdownField = ({ | ||
clearable, | ||
disabled, | ||
lazyLoad, | ||
multi, | ||
passRef, | ||
placeholder, | ||
options, | ||
optionUrl, | ||
onChange, | ||
searchable, | ||
value, | ||
SelectComponent, | ||
AsyncSelectComponent, | ||
...passThroughProps | ||
}) => { | ||
const [hasChanges, setHasChanges] = useState(false); | ||
const [justChanged, setJustChanged] = useState(false); | ||
const selectComponentRef = createRef(); | ||
|
||
// This is required to get the PageForm change tracker to work by firing a bubbling change event | ||
useEffect(() => { | ||
if (!justChanged) { | ||
return; | ||
} | ||
const element = selectComponentRef.current.inputRef; | ||
const event = new Event('change', { bubbles: true }); | ||
element.dispatchEvent(event); | ||
setJustChanged(false); | ||
}); | ||
|
||
const fetchCache = {}; | ||
|
||
const fetchOptions = (term) => { | ||
if (fetchCache.hasOwnProperty(term)) { | ||
return Promise.resolve(fetchCache[term]); | ||
} | ||
let innerFetchOptions = () => { | ||
const fetchUrl = url.parse(optionUrl, true); | ||
if (fetchUrl.search) { | ||
// Remove the search key, though keep the query key | ||
// This is so url.format uses the query key instead of the search key below | ||
delete fetchUrl.search; | ||
} | ||
fetchUrl.query.term = term; | ||
const endpoint = url.format(fetchUrl); | ||
const csrfHeader = FormConstants.CSRF_HEADER; | ||
const headers = {}; | ||
headers[csrfHeader] = Config.get('SecurityID'); | ||
return backend.get(endpoint, headers) | ||
.then(response => response.json()) | ||
.then(responseJson => { | ||
fetchCache[term] = responseJson; | ||
return responseJson; | ||
}); | ||
}; | ||
innerFetchOptions = debounce(innerFetchOptions, 500); | ||
return innerFetchOptions(); | ||
}; | ||
|
||
/** | ||
* Get the options that should be shown to the user for this SearchableDropdownField, | ||
* optionally filtering by the given string input | ||
*/ | ||
const getOptions = (input) => { | ||
if (!lazyLoad) { | ||
return Promise.resolve(options); | ||
} | ||
if (!input) { | ||
return Promise.resolve([]); | ||
} | ||
return fetchOptions(input); | ||
}; | ||
|
||
const handleChange = (val) => { | ||
setHasChanges(false); | ||
if (JSON.stringify(value) !== JSON.stringify(val)) { | ||
setHasChanges(true); | ||
setJustChanged(true); | ||
} | ||
onChange(val); | ||
}; | ||
|
||
/** | ||
* Required to prevent SearchableDropdownField being cleared on blur | ||
* | ||
* @link https://github.com/JedWatson/react-select/issues/805#issuecomment-210646771 | ||
*/ | ||
const handleOnBlur = () => {}; | ||
|
||
const className = classNames({ | ||
'no-change-track': !hasChanges, | ||
'ss-searchable-dropdown-field--lazy-load': lazyLoad | ||
}); | ||
|
||
const optionsProps = lazyLoad ? { loadOptions: getOptions } : { options }; | ||
|
||
const noOptionsMessage = (inputValue) => { | ||
if (inputValue) { | ||
return i18n._t('Admin.NO_MATCHING_OPTIONS', 'No matching options'); | ||
} | ||
return i18n._t('Admin.TYPE_TO_SEARCH', 'Type to search'); | ||
}; | ||
|
||
// Setting passRef to false is purely for jest when mocking the component with a simple function component | ||
// It prevents - Warning: Function components cannot be given refs. Attempts to access this ref will fail. | ||
const refProps = passRef ? { ref: selectComponentRef } : {}; | ||
|
||
let val = value; | ||
// For non-multi only the first value is needed | ||
if (!multi && val) { | ||
const keys = Object.keys(val); | ||
if (keys.length > 0) { | ||
const key = keys[0]; | ||
const v = val[key]; | ||
if (typeof v === 'object') { | ||
val = v; | ||
} | ||
} | ||
} | ||
|
||
const DynamicComponent = lazyLoad ? AsyncSelectComponent : SelectComponent; | ||
|
||
return <EmotionCssCacheProvider> | ||
<DynamicComponent | ||
// passThroughProps needs to be first so that it can be overridden by the other props | ||
{...passThroughProps} | ||
classNamePrefix="ss-searchable-dropdown-field" | ||
className={className} | ||
isClearable={clearable} | ||
isDisabled={disabled} | ||
isMulti={multi} | ||
isSearchable={searchable} | ||
placeholder={placeholder} | ||
onChange={handleChange} | ||
onBlur={handleOnBlur} | ||
{...optionsProps} | ||
noOptionsMessage={noOptionsMessage} | ||
{...refProps} | ||
value={val} | ||
/> | ||
</EmotionCssCacheProvider>; | ||
}; | ||
|
||
SearchableDropdownField.propTypes = { | ||
clearable: PropTypes.bool.isRequired, | ||
disabled: PropTypes.bool.isRequired, | ||
lazyLoad: PropTypes.bool.isRequired, | ||
multi: PropTypes.bool.isRequired, | ||
name: PropTypes.string.isRequired, | ||
placeholder: PropTypes.string.isRequired, | ||
onChange: PropTypes.func.isRequired, | ||
options: PropTypes.arrayOf(PropTypes.object), | ||
optionUrl: PropTypes.string, | ||
passRef: PropTypes.bool.isRequired, | ||
searchable: PropTypes.bool.isRequired, | ||
value: PropTypes.any.isRequired, | ||
SelectComponent: PropTypes.oneOfType([PropTypes.object, PropTypes.func]).isRequired, | ||
AsyncSelectComponent: PropTypes.oneOfType([PropTypes.object, PropTypes.func]).isRequired, | ||
}; | ||
|
||
SearchableDropdownField.defaultProps = { | ||
disabled: false, | ||
lazyLoad: false, | ||
clearable: true, | ||
searchable: true, | ||
multi: false, | ||
passRef: true, | ||
placeholder: '', | ||
SelectComponent: Select, | ||
AsyncSelectComponent: AsyncSelect, | ||
}; | ||
|
||
export { SearchableDropdownField as Component }; | ||
|
||
export default fieldHolder(SearchableDropdownField); |
121 changes: 121 additions & 0 deletions
121
client/src/components/SearchableDropdownField/SearchableDropdownField.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
.ss-searchable-dropdown-field__control { | ||
border-color: $gray-200; | ||
box-shadow: none; | ||
|
||
// this is required to override default react-select style | ||
&:hover { | ||
border-color: $gray-200; | ||
} | ||
|
||
&--is-focused { | ||
box-shadow: none; | ||
border-color: $input-focus-border-color; | ||
|
||
// this is required to override default react-select style | ||
&:hover { | ||
border-color: $input-focus-border-color; | ||
} | ||
} | ||
} | ||
|
||
.ss-searchable-dropdown-field__menu { | ||
margin-top: 0; | ||
} | ||
|
||
.ss-searchable-dropdown-field__option+.ss-searchable-dropdown-field__option { | ||
border-top: 1px solid $border-color-light; | ||
} | ||
|
||
.ss-searchable-dropdown-field__option-button { | ||
border: 1px solid $border-color-light; | ||
border-radius: $border-radius; | ||
background: $white; | ||
// needed to override the width rule in .fill-width | ||
width: auto !important; // sass-lint:disable-line no-important | ||
max-width: 25%; | ||
margin: -4px -5px -4px 5px; | ||
padding: 4px 5px 4px 4px; | ||
cursor: pointer; | ||
|
||
.font-icon-right-open-big { | ||
margin: 2px 0 0 -1px; | ||
width: 24px; | ||
} | ||
} | ||
|
||
.ss-searchable-dropdown-field__option-count-icon { | ||
padding: 0 calc($spacer / 2); | ||
line-height: 0.8; | ||
} | ||
|
||
.ss-searchable-dropdown-field__option-context { | ||
color: $gray-600; | ||
font-size: $font-size-sm; | ||
} | ||
|
||
.ss-searchable-dropdown-field__option--is-focused { | ||
background-color: $list-group-hover-bg; | ||
} | ||
|
||
.ss-searchable-dropdown-field__option--is-selected { | ||
background: $link-color; | ||
color: $white; | ||
|
||
.ss-searchable-dropdown-field__option-button { | ||
border-color: $brand-primary; | ||
background: none; | ||
color: $white; | ||
} | ||
} | ||
|
||
.ss-searchable-dropdown-field__option-title--highlighted { | ||
font-weight: bold; | ||
} | ||
|
||
.ss-searchable-dropdown-field--lazy-load { | ||
.ss-searchable-dropdown-field__dropdown-indicator, | ||
.ss-searchable-dropdown-field__indicator-separator { | ||
display: none; | ||
} | ||
} | ||
|
||
.ss-searchable-dropdown-field__indicator { | ||
cursor: pointer; | ||
} | ||
|
||
.ss-searchable-dropdown-field__clear-indicator { | ||
&:hover, | ||
&:focus { | ||
color: $brand-danger; | ||
} | ||
} | ||
|
||
.ss-searchable-dropdown-field__dropdown-indicator { | ||
&:focus { | ||
color: $body-color-dark; | ||
} | ||
} | ||
|
||
.ss-searchable-dropdown-field__multi-value { | ||
color: $body-color; | ||
background-color: $white; | ||
border: 1px solid $gray-200; | ||
border-radius: $border-radius; | ||
margin-top: 3px; | ||
} | ||
|
||
.ss-searchable-dropdown-field__multi-value__remove { | ||
font-size: $font-size-lg; | ||
padding: 3px 5px 2px 5px; | ||
border-left: 1px solid $gray-200; | ||
border-radius: 0; | ||
|
||
&:hover { | ||
background-color: #ffe0e0; | ||
color: $brand-danger; | ||
} | ||
} | ||
|
||
.ss-searchable-dropdown-field__multi-value__label { | ||
padding: 3px 10px 3px 10px; | ||
} |
Oops, something went wrong.