1 import { c } from 'ttag';
3 import { Button } from '@proton/atoms';
4 import ButtonGroup from '@proton/components/components/button/ButtonGroup';
5 import Option from '@proton/components/components/option/Option';
6 import type { SelectTwoProps } from '@proton/components/components/selectTwo/SelectTwo';
7 import SelectTwo from '@proton/components/components/selectTwo/SelectTwo';
8 import { type Currency, DEFAULT_CURRENCY, mainCurrencies } from '@proton/payments';
9 import clsx from '@proton/utils/clsx';
11 interface SharedProps {
12 onSelect: (newCurrency: Currency) => void;
14 currencies: readonly Currency[];
17 type InternalSelectProps = Omit<SelectTwoProps<Currency>, 'onSelect' | 'children'> &
22 type ButtonGroupProps = {
30 type Props = ButtonGroupProps | InternalSelectProps;
32 const CurrencySelector = (props: Props) => {
33 const options = (props.currencies ?? mainCurrencies).map((c) => ({ text: c, value: c }));
35 if (props.mode === 'buttons') {
36 // extracting `mode` and `currencies` in order to remove them from ...rest
37 const { currency = DEFAULT_CURRENCY, onSelect, loading, mode, currencies, ...rest } = props;
39 <ButtonGroup {...rest}>
40 {options.map(({ text, value }) => {
43 className={clsx([currency === value && 'is-selected'])}
45 onClick={() => onSelect(value as Currency)}
56 if (props.mode === 'select-two') {
57 // extracting `mode` and `currencies` in order to remove them from ...rest
58 const { currency = DEFAULT_CURRENCY, onSelect, loading, mode, currencies, ...rest } = props;
59 const handleChange = ({ value }: { value: Currency }) => onSelect(value);
63 onChange={handleChange}
65 aria-describedby={c('Title').t`Currency`}
66 data-testid="currency-selector"
69 {options.map(({ text, value }) => {
71 <Option value={value} title={text} key={value} data-testid={`currency-option-${value}`}>
83 export default CurrencySelector;