integrated combobox

This commit is contained in:
ronronscelestes 2021-09-27 15:34:11 +02:00
parent 0317e5f4c9
commit 3145518c8e
2 changed files with 117 additions and 230 deletions

View File

@ -1,27 +1,18 @@
/* eslint-disable react/jsx-indent */ /* eslint-disable react/jsx-indent */
import React from 'react'; import React from 'react';
import styled from 'styled-components'; import { Combobox, ComboboxOption } from '@strapi/parts/Combobox';
import { Select, Option } from '@strapi/parts/Select';
import { Loader } from '@strapi/parts/Loader';
import { useIntl } from 'react-intl'; import { useIntl } from 'react-intl';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import useLocales from '../../hooks/useLocales'; import useLocales from '../../hooks/useLocales';
import useDefaultLocales from '../../hooks/useDefaultLocales'; import useDefaultLocales from '../../hooks/useDefaultLocales';
import { getTrad } from '../../utils'; import { getTrad } from '../../utils';
const SmallLoader = styled(Loader)`
img {
height: 1rem;
width: 1rem;
}
`;
/** /**
* The component is memoized and needs a useCallback over the onLocaleChange and * The component is memoized and needs a useCallback over the onLocaleChange and
* onClear props to prevent the Select from re-rendering N times when typing on a specific * onClear props to prevent the Select from re-rendering N times when typing on a specific
* key in a formik form * key in a formik form
*/ */
const LocaleSelect = React.memo(({ value, onLocaleChange, error, onClear }) => { const LocaleSelect = React.memo(({ value, onLocaleChange, error }) => {
const { formatMessage } = useIntl(); const { formatMessage } = useIntl();
const { defaultLocales, isLoading } = useDefaultLocales(); const { defaultLocales, isLoading } = useDefaultLocales();
const { locales } = useLocales(); const { locales } = useLocales();
@ -40,30 +31,13 @@ const LocaleSelect = React.memo(({ value, onLocaleChange, error, onClear }) => {
const computedValue = value || ''; const computedValue = value || '';
return ( return (
<Select <Combobox
startIcon={
isLoading ? (
<SmallLoader>
{formatMessage({
id: getTrad('Settings.locales.modal.create.defaultLocales.loading'),
defaultMessage: 'Settings.locales.modal.create.defaultLocales.loading',
})}
</SmallLoader>
) : (
undefined
)
}
aria-busy={isLoading} aria-busy={isLoading}
error={error}
label={formatMessage({ label={formatMessage({
id: getTrad('Settings.locales.modal.locales.label'), id: getTrad('Settings.locales.modal.locales.label'),
defaultMessage: 'Locales', defaultMessage: 'Locales',
})} })}
onClear={value ? onClear : undefined}
clearLabel={formatMessage({
id: 'clearLabel',
defaultMessage: 'Clear',
})}
error={error}
value={computedValue} value={computedValue}
onChange={selectedLocaleKey => { onChange={selectedLocaleKey => {
const selectedLocale = options.find(locale => locale.value === selectedLocaleKey); const selectedLocale = options.find(locale => locale.value === selectedLocaleKey);
@ -74,27 +48,23 @@ const LocaleSelect = React.memo(({ value, onLocaleChange, error, onClear }) => {
}} }}
placeholder={formatMessage({ id: 'components.placeholder.select', defaultMessage: 'Select' })} placeholder={formatMessage({ id: 'components.placeholder.select', defaultMessage: 'Select' })}
> >
{isLoading {options.map(option => (
? null <ComboboxOption value={option.value} key={option.value}>
: options.map(option => ( {option.label}
<Option value={option.value} key={option.value}> </ComboboxOption>
{option.label} ))}
</Option> </Combobox>
))}
</Select>
); );
}); });
LocaleSelect.defaultProps = { LocaleSelect.defaultProps = {
error: undefined, error: undefined,
value: undefined, value: undefined,
onClear: () => undefined,
onLocaleChange: () => undefined, onLocaleChange: () => undefined,
}; };
LocaleSelect.propTypes = { LocaleSelect.propTypes = {
error: PropTypes.string, error: PropTypes.string,
onClear: PropTypes.func,
onLocaleChange: PropTypes.func, onLocaleChange: PropTypes.func,
value: PropTypes.string, value: PropTypes.string,
}; };

File diff suppressed because one or more lines are too long