-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NO CHANGELOG] [Add Funds Widget] Options drawer (#2124)
Co-authored-by: Mimi Tran <[email protected]>
- Loading branch information
1 parent
3bc1af9
commit 7cacae4
Showing
7 changed files
with
403 additions
and
58 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
73 changes: 73 additions & 0 deletions
73
packages/checkout/widgets-lib/src/widgets/add-funds/components/Option.tsx
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,73 @@ | ||
import { IconProps, MenuItem, MenuItemSize } from '@biom3/react'; | ||
import { ReactElement } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
export enum OptionTypes { | ||
SWAP = 'swap', | ||
DEBIT = 'debit', | ||
CREDIT = 'credit', | ||
} | ||
|
||
export interface OptionProps<RC extends ReactElement | undefined = undefined> { | ||
rc?: RC; | ||
type: OptionTypes; | ||
onClick: (type: OptionTypes) => void; | ||
disabled?: boolean; | ||
caption?: string; | ||
size?: MenuItemSize; | ||
} | ||
|
||
export function Option<RC extends ReactElement | undefined = undefined>({ | ||
type, | ||
onClick, | ||
disabled = false, | ||
caption, | ||
size, | ||
rc = <span />, | ||
}: OptionProps<RC>) { | ||
const { t } = useTranslation(); | ||
|
||
const icon: Record<OptionTypes, IconProps['icon']> = { | ||
[OptionTypes.SWAP]: 'Coins', | ||
[OptionTypes.DEBIT]: 'BankCard', | ||
[OptionTypes.CREDIT]: 'BankCard', | ||
}; | ||
|
||
const handleClick = () => onClick(type); | ||
|
||
const menuItemProps = { | ||
disabled, | ||
emphasized: true, | ||
onClick: disabled ? undefined : handleClick, | ||
}; | ||
|
||
return ( | ||
<MenuItem | ||
rc={rc} | ||
size={size || 'medium'} | ||
sx={{ | ||
marginBottom: 'base.spacing.x1', | ||
userSelect: 'none', | ||
...(disabled && { | ||
filter: 'opacity(0.5)', | ||
cursor: 'not-allowed !important', | ||
}), | ||
}} | ||
{...menuItemProps} | ||
> | ||
<MenuItem.FramedIcon icon={icon[type]} /> | ||
<MenuItem.Label size="medium"> | ||
{t(`views.ADD_FUNDS.drawer.options.${type}.heading`)} | ||
</MenuItem.Label> | ||
{!disabled && <MenuItem.IntentIcon />} | ||
<MenuItem.Caption> | ||
{caption | ||
|| t( | ||
`views.ADD_FUNDS.drawer.options.${type}.${ | ||
disabled ? 'disabledCaption' : 'caption' | ||
}`, | ||
)} | ||
</MenuItem.Caption> | ||
</MenuItem> | ||
); | ||
} |
75 changes: 75 additions & 0 deletions
75
packages/checkout/widgets-lib/src/widgets/add-funds/components/Options.tsx
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,75 @@ | ||
import { Box, MenuItemSize } from '@biom3/react'; | ||
|
||
import { motion } from 'framer-motion'; | ||
import { useEffect, useMemo } from 'react'; | ||
import { | ||
listItemVariants, | ||
listVariants, | ||
} from '../../../lib/animation/listAnimation'; | ||
import { Option, OptionTypes } from './Option'; | ||
|
||
const defaultOptions: OptionTypes[] = [ | ||
OptionTypes.SWAP, | ||
OptionTypes.DEBIT, | ||
OptionTypes.CREDIT, | ||
]; | ||
|
||
export interface OptionsProps { | ||
onClick: (type: OptionTypes) => void; | ||
disabledOptions?: OptionTypes[]; | ||
options?: OptionTypes[]; | ||
captions?: Partial<Record<OptionTypes, string>>; | ||
size?: MenuItemSize; | ||
hideDisabledOptions?: boolean; | ||
} | ||
|
||
export function Options(props: OptionsProps) { | ||
const { | ||
disabledOptions = [], | ||
options, | ||
onClick, | ||
captions, | ||
size, | ||
hideDisabledOptions, | ||
} = props; | ||
const filteredOptions = useMemo( | ||
() => (options || defaultOptions).filter( | ||
(option) => !hideDisabledOptions || !disabledOptions.includes(option), | ||
), | ||
[options, disabledOptions, hideDisabledOptions], | ||
); | ||
|
||
useEffect(() => { | ||
if (filteredOptions.length === 1) { | ||
onClick(filteredOptions[0]); | ||
} | ||
}, [options, onClick]); | ||
|
||
return ( | ||
<Box | ||
testId="options-list" | ||
sx={{ | ||
width: '100%', | ||
display: 'flex', | ||
flexDirection: 'column', | ||
justifyContent: 'center', | ||
alignItems: 'flex-start', | ||
}} | ||
rc={ | ||
<motion.div variants={listVariants} initial="hidden" animate="show" /> | ||
} | ||
> | ||
{filteredOptions.map((type, idx: number) => ( | ||
<Option | ||
key={`option-type-${type}`} | ||
type={type} | ||
size={size} | ||
onClick={onClick} | ||
disabled={disabledOptions.includes(type)} | ||
caption={captions?.[type]} | ||
rc={<motion.div custom={idx} variants={listItemVariants} />} | ||
/> | ||
))} | ||
</Box> | ||
); | ||
} |
60 changes: 60 additions & 0 deletions
60
packages/checkout/widgets-lib/src/widgets/add-funds/components/OptionsDrawer.tsx
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,60 @@ | ||
import { Box, Drawer } from '@biom3/react'; | ||
import { motion } from 'framer-motion'; | ||
import { listVariants } from '../../../lib/animation/listAnimation'; | ||
import { Options } from './Options'; | ||
import { OptionTypes } from './Option'; | ||
|
||
type OptionsDrawerProps = { | ||
visible: boolean; | ||
onClose: () => void; | ||
onPayWithCard?: (paymentType: OptionTypes) => void; | ||
}; | ||
|
||
export function OptionsDrawer({ | ||
visible, | ||
onClose, | ||
onPayWithCard, | ||
}: OptionsDrawerProps) { | ||
return ( | ||
<Drawer | ||
size="full" | ||
visible={visible} | ||
showHeaderBar | ||
onCloseDrawer={onClose} | ||
headerBarTitle="Pay with ..." | ||
> | ||
<Drawer.Content | ||
rc={ | ||
<motion.div variants={listVariants} initial="hidden" animate="show" /> | ||
} | ||
> | ||
<Box | ||
sx={{ | ||
height: '100%', | ||
display: 'flex', | ||
flexDirection: 'column', | ||
justifyContent: 'space-between', | ||
px: 'base.spacing.x4', | ||
}} | ||
> | ||
<Options | ||
onClick={onPayWithCard ?? (() => {})} | ||
size="medium" | ||
hideDisabledOptions | ||
options={[ | ||
OptionTypes.SWAP, | ||
OptionTypes.DEBIT, | ||
OptionTypes.CREDIT, | ||
]} | ||
disabledOptions={[]} | ||
captions={{ | ||
[OptionTypes.SWAP]: 'Swap', | ||
[OptionTypes.DEBIT]: 'Debit', | ||
[OptionTypes.CREDIT]: 'Credit', | ||
}} | ||
/> | ||
</Box> | ||
</Drawer.Content> | ||
</Drawer> | ||
); | ||
} |
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.