-
Notifications
You must be signed in to change notification settings - Fork 110
frontend: style multi select dropdown #2129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| .select :global(.react-select__input-container) { | ||
| position: absolute; | ||
| top: -2px; | ||
| left: 6px; | ||
| } | ||
|
|
||
| .select :global(.react-select__option) { | ||
| display: flex; | ||
| flex-direction: row; | ||
| align-items: center; | ||
| justify-content: space-between; | ||
| } | ||
|
|
||
| /*displays only first 3 currencies, the rest gets hidden*/ | ||
| .select :global(.react-select__multi-value):nth-child(n + 4) { | ||
| display: none; | ||
| } | ||
|
|
||
|
|
||
| /* | ||
| displays ', ' as a pseudo component for all currency component | ||
| except the last displayed component (the 3rd currency component). | ||
|
|
||
| The reason for :nth-last-child(2) instead of just :last-child is because there's an extra | ||
| component created by react-select after the actual last currency component. | ||
|
|
||
| So, we're targetting the second to last component here, | ||
| which is the last selected currency in the DOM. | ||
| */ | ||
| .select:not(.hideMultiSelect) :global(.react-select__multi-value):not(:nth-last-child(2))::after { | ||
| content: ','; | ||
| padding-right: 2px; | ||
| } | ||
|
|
||
| /* | ||
| displays '...' as a pseudo component of the 3rd currency and only when there's more than 3 selected. | ||
| :nth-last-child(2) is used for the same reason as above | ||
| */ | ||
| .select:not(.hideMultiSelect) :global(.react-select__multi-value):nth-child(3):not(:nth-last-child(2))::after { | ||
| content: '\002026'; | ||
| } | ||
|
|
||
| .select.hideMultiSelect :global(.react-select__multi-value) { | ||
| display: none; | ||
| } | ||
|
|
||
| .select :global(.react-select__value-container--is-multi) { | ||
| height: var(--item-height-xsmall); | ||
| } | ||
|
|
||
| .defaultCurrency:hover { | ||
| cursor: not-allowed; | ||
| } | ||
|
|
||
| .defaultLabel { | ||
| font-size: var(--size-small); | ||
| margin: 0; | ||
| text-transform: capitalize; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { useEffect, useState } from 'react'; | ||
| import Select, { ActionMeta, DropdownIndicatorProps, OptionProps, components } from 'react-select'; | ||
| import { selectFiat, unselectFiat, SharedProps } from '../../../../components/rates/rates'; | ||
| import { Fiat } from '../../../../api/account'; | ||
| import { SelectedCheckLight } from '../../../../components/icon'; | ||
| import dropdownStyles from './dropdowns.module.css'; | ||
| import activeCurrenciesDropdownStyle from './activecurrenciesdropdown.module.css'; | ||
| import { useTranslation } from 'react-i18next'; | ||
|
|
||
| type SelectOption = { | ||
| label: String; | ||
| value: Fiat; | ||
| } | ||
|
|
||
| type TSelectProps = { | ||
| options: SelectOption[]; | ||
| } & SharedProps; | ||
|
|
||
| // a multi-select dropdown | ||
| export const ActiveCurrenciesDropdown = ({ | ||
| options, | ||
| active: defaultCurrency, // active here actually means default, thus aliasing it | ||
| selected | ||
| }: TSelectProps) => { | ||
| const [selectedCurrencies, setSelectedCurrencies] = useState<SelectOption[]>([]); | ||
| const [search, setSearch] = useState(''); | ||
| const { t } = useTranslation(); | ||
|
|
||
| useEffect(() => { | ||
| if (selected.length > 0) { | ||
| const formattedSelectedCurrencies = selected.map(currency => ({ label: currency, value: currency })); | ||
| setSelectedCurrencies(formattedSelectedCurrencies); | ||
| } | ||
| }, [selected]); | ||
|
|
||
| const DropdownIndicator = (props: DropdownIndicatorProps<SelectOption, true>) => { | ||
| return ( | ||
| <components.DropdownIndicator {...props}> | ||
| <div className={dropdownStyles.dropdown} /> | ||
| </components.DropdownIndicator> | ||
| ); | ||
| }; | ||
|
|
||
| const Option = (props: OptionProps<SelectOption, true>) => { | ||
| const { label, value } = props.data; | ||
| const selected = selectedCurrencies.findIndex(currency => currency.value === value) >= 0; | ||
| const isDefaultCurrency = defaultCurrency === value; | ||
| return ( | ||
| <components.Option {...props} className={`${isDefaultCurrency ? activeCurrenciesDropdownStyle.defaultCurrency : ''}`}> | ||
| <span>{label}</span> | ||
| {isDefaultCurrency ? <p className={activeCurrenciesDropdownStyle.defaultLabel}>{t('fiat.default')}</p> : null} | ||
| {selected && !isDefaultCurrency ? <SelectedCheckLight /> : null} | ||
| </components.Option> | ||
| ); | ||
| }; | ||
| return ( | ||
| <Select | ||
| className={` | ||
| ${dropdownStyles.select} | ||
| ${activeCurrenciesDropdownStyle.select} | ||
| ${search.length > 0 ? activeCurrenciesDropdownStyle.hideMultiSelect : ''} | ||
| `} | ||
| classNamePrefix="react-select" | ||
| isSearchable | ||
| isClearable={false} | ||
| components={{ DropdownIndicator, IndicatorSeparator: () => null, MultiValueRemove: () => null, Option }} | ||
| isMulti | ||
| closeMenuOnSelect={false} | ||
| hideSelectedOptions={false} | ||
| value={[...selectedCurrencies].reverse()} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reverse() here so the latest selected currency will be displayed at the most front of the string. E.g:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ahh that makes sense, I was wondering what the order is and why it changes before. This way it always shows the newest nice 👍 |
||
| onInputChange={(newValue) => setSearch(newValue)} | ||
| onChange={(_, meta: ActionMeta<SelectOption>) => { | ||
| switch (meta.action) { | ||
| case 'select-option': | ||
| if (meta.option) { | ||
| selectFiat(meta.option.value); | ||
| } | ||
| break; | ||
| case 'deselect-option': | ||
| if (meta.option && meta.option.value !== defaultCurrency) { | ||
| unselectFiat(meta.option.value); | ||
| } | ||
| } | ||
|
|
||
| }} | ||
| options={options} | ||
| />); | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about if we should display the currency name in the native language. i.e. "Svensk krona" instead of "Swedish krona", but that does't really work for "Swiss france" with our 4 official languages 🤦
so I guess better to have everything in English.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please note we have a new lang (Czech) and new Fiat currencies on master, maybe merge master->staging-revamp-settings first?
#2141