-
-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix(Expense form): Update payout method form inputs (#10954)
- Loading branch information
1 parent
ceee9ea
commit 43e9d7c
Showing
28 changed files
with
442 additions
and
364 deletions.
There are no files selected for viewing
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,123 @@ | ||
'use client'; | ||
|
||
import * as React from 'react'; | ||
import { isString } from 'lodash'; | ||
import { Check, ChevronDown } from 'lucide-react'; | ||
import { defineMessages, useIntl } from 'react-intl'; | ||
|
||
import { cn } from '@/lib/utils'; | ||
|
||
import { Button } from '@/components/ui/Button'; | ||
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from '@/components/ui/Command'; | ||
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/Popover'; | ||
|
||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from './ui/Select'; | ||
|
||
const Messages = defineMessages({ | ||
loading: { | ||
id: 'Select.Loading', | ||
defaultMessage: 'Loading...', | ||
}, | ||
noOptions: { | ||
id: 'Select.NoOptions', | ||
defaultMessage: 'Nothing found', | ||
}, | ||
placeholder: { | ||
id: 'Select.Placeholder', | ||
defaultMessage: 'No selection', | ||
}, | ||
inputPlaceholder: { | ||
id: 'search.placeholder', | ||
defaultMessage: 'Search...', | ||
}, | ||
}); | ||
|
||
type ComboSelectProps = { | ||
options: Array<{ value: string; label: string | React.JSX.Element; keywords?: string[] }>; | ||
value: string | undefined; | ||
onChange: (val: string) => void; | ||
id?: string; | ||
disabled?: boolean; | ||
error?: boolean; | ||
loading?: boolean; | ||
className?: string; | ||
placeholder?: string; | ||
inputPlaceholder?: string; | ||
isSearchable?: boolean; | ||
'data-cy'?: string; | ||
}; | ||
|
||
export function ComboSelect(props: ComboSelectProps) { | ||
const [open, setOpen] = React.useState(false); | ||
const intl = useIntl(); | ||
const isSearchable = props.isSearchable ?? props.options?.length > 8; | ||
const placeholder = props.loading | ||
? intl.formatMessage(Messages.loading) | ||
: (props.placeholder ?? intl.formatMessage(Messages.placeholder)); | ||
const selectedOption = props.options.find(option => option.value === props.value); | ||
|
||
const onChange = (val: string) => { | ||
props.onChange(val); | ||
setOpen(false); | ||
}; | ||
|
||
if (!isSearchable) { | ||
return ( | ||
<Select open={open} onOpenChange={setOpen} onValueChange={onChange} value={props.value}> | ||
<SelectTrigger | ||
className={cn({ 'text-muted-foreground': !props.value }, props.className)} | ||
id={props.id} | ||
data-cy={props['data-cy']} | ||
> | ||
<SelectValue placeholder={placeholder} /> | ||
</SelectTrigger> | ||
<SelectContent> | ||
{props.options.map(option => ( | ||
<SelectItem key={option.value} value={option.value} data-cy="select-option"> | ||
{option.label} | ||
</SelectItem> | ||
))} | ||
</SelectContent> | ||
</Select> | ||
); | ||
} | ||
return ( | ||
<Popover open={open} onOpenChange={setOpen}> | ||
<PopoverTrigger asChild> | ||
<Button | ||
id={props.id} | ||
data-cy={props['data-cy']} | ||
variant="outline" | ||
role="combobox" | ||
aria-expanded={open} | ||
className={cn('justify-between font-normal', { 'text-muted-foreground': !props.value }, props.className)} | ||
> | ||
{selectedOption?.label ?? placeholder ?? intl.formatMessage(Messages.placeholder)} | ||
<ChevronDown className="ml-2 h-4 w-4 shrink-0 opacity-50" /> | ||
</Button> | ||
</PopoverTrigger> | ||
<PopoverContent className="w-[--radix-popover-trigger-width] p-0"> | ||
<Command> | ||
<CommandInput placeholder={props.inputPlaceholder || intl.formatMessage(Messages.inputPlaceholder)} /> | ||
<CommandList> | ||
<CommandEmpty>{intl.formatMessage(Messages.noOptions)}</CommandEmpty> | ||
<CommandGroup> | ||
{props.options.map(option => ( | ||
<CommandItem | ||
key={option.value} | ||
value={option.value} | ||
keywords={option.keywords || (isString(option.label) ? [option.label] : undefined)} | ||
onSelect={onChange} | ||
data-cy="select-option" | ||
> | ||
<Check className={cn('mr-2 h-4 w-4', props.value === option.value ? 'opacity-100' : 'opacity-0')} /> | ||
{option.label} | ||
</CommandItem> | ||
))} | ||
</CommandGroup> | ||
</CommandList> | ||
</Command> | ||
</PopoverContent> | ||
</Popover> | ||
); | ||
} |
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,61 @@ | ||
import React from 'react'; | ||
import { getEmojiByCurrencyCode } from 'country-currency-emoji-flags'; | ||
import { truncate } from 'lodash'; | ||
import { useIntl } from 'react-intl'; | ||
|
||
import { Currency } from '../lib/constants/currency'; | ||
import { getIntlDisplayNames } from '../lib/i18n'; | ||
|
||
import { ComboSelect } from './ComboSelect'; | ||
|
||
const generateCurrencyOptions = (intl, availableCurrencies) => { | ||
const currencyDisplayNames = getIntlDisplayNames(intl.locale, 'currency'); | ||
return availableCurrencies.map(currency => { | ||
const currencyName = currencyDisplayNames.of(currency); | ||
const emoji = getEmojiByCurrencyCode(currency); | ||
return { | ||
value: currency, | ||
label: ( | ||
<div className="" title={currencyName}> | ||
{emoji && <span>{emoji}</span>} | ||
| ||
<span className="ml-1 whitespace-nowrap"> | ||
<span className="">{currency}</span> | ||
{` `} | ||
<span className="align-middle text-xs text-muted-foreground">{truncate(currencyName, { length: 30 })}</span> | ||
</span> | ||
</div> | ||
), | ||
}; | ||
}); | ||
}; | ||
|
||
export default function CurrencyPicker({ | ||
availableCurrencies = Currency, | ||
value, | ||
onChange, | ||
...props | ||
}: { | ||
/** A list of currencies presented in the currency picker */ | ||
availableCurrencies: string[]; | ||
onChange: (selectedCurrency: string) => void; | ||
value?: string; | ||
} & Omit<React.ComponentProps<typeof ComboSelect>, 'options'>) { | ||
const intl = useIntl(); | ||
const currencyOptions = React.useMemo( | ||
() => generateCurrencyOptions(intl, availableCurrencies), | ||
[intl, availableCurrencies], | ||
); | ||
return ( | ||
<ComboSelect | ||
id="currency-picker" | ||
data-cy="currency-picker" | ||
error={!value} | ||
isSearchable={availableCurrencies.length > 10} | ||
options={currencyOptions} | ||
value={value} | ||
onChange={onChange} | ||
{...props} | ||
/> | ||
); | ||
} |
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
Oops, something went wrong.