-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(components): token input (#19)
- Loading branch information
1 parent
85d810a
commit 0609525
Showing
22 changed files
with
753 additions
and
354 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,6 @@ | ||
--- | ||
"@interlay/ui": patch | ||
"@interlay/hooks": patch | ||
--- | ||
|
||
refactor(components): token input |
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
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
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
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,77 @@ | ||
import { useLabel } from '@react-aria/label'; | ||
import { mergeProps } from '@react-aria/utils'; | ||
import { forwardRef, ReactNode } from 'react'; | ||
|
||
import { Flex } from '../Flex'; | ||
import { NumberInput, NumberInputProps } from '../NumberInput'; | ||
import { formatUSD } from '../utils/format'; | ||
|
||
import { TokenInputLabel } from './TokenInputLabel'; | ||
import { StyledUSDAdornment } from './TokenInput.style'; | ||
|
||
type Props = { | ||
valueUSD?: number; | ||
balance?: ReactNode; | ||
}; | ||
|
||
type InheritAttrs = Omit<NumberInputProps, keyof Props>; | ||
|
||
type BaseTokenInputProps = Props & InheritAttrs; | ||
|
||
const BaseTokenInput = forwardRef<HTMLInputElement, BaseTokenInputProps>( | ||
( | ||
{ | ||
label, | ||
style, | ||
hidden, | ||
className, | ||
placeholder = '0', | ||
errorMessage, | ||
description, | ||
balance, | ||
children, | ||
valueUSD, | ||
isDisabled, | ||
...props | ||
}, | ||
ref | ||
): JSX.Element => { | ||
const { labelProps, fieldProps } = useLabel({ label, ...props }); | ||
|
||
const hasLabel = !!label || !!balance; | ||
|
||
const bottomAdornment = valueUSD !== undefined && ( | ||
<StyledUSDAdornment $isDisabled={isDisabled}>{formatUSD(valueUSD, { compact: true })}</StyledUSDAdornment> | ||
); | ||
|
||
return ( | ||
<Flex className={className} direction='column' gap='spacing0' hidden={hidden} style={style}> | ||
{hasLabel && ( | ||
<TokenInputLabel {...labelProps} balance={balance}> | ||
{label} | ||
</TokenInputLabel> | ||
)} | ||
<NumberInput | ||
ref={ref} | ||
bottomAdornment={bottomAdornment} | ||
description={description} | ||
errorMessage={errorMessage} | ||
inputMode='decimal' | ||
isDisabled={isDisabled} | ||
maxLength={79} | ||
minLength={1} | ||
pattern='^[0-9]*[.,]?[0-9]*$' | ||
placeholder={placeholder} | ||
size='large' | ||
{...mergeProps(props, fieldProps)} | ||
/> | ||
{children} | ||
</Flex> | ||
); | ||
} | ||
); | ||
|
||
BaseTokenInput.displayName = 'BaseTokenInput'; | ||
|
||
export { BaseTokenInput }; | ||
export type { BaseTokenInputProps }; |
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,51 @@ | ||
import { ReactNode, forwardRef } from 'react'; | ||
|
||
import { TokenAdornment, TokenTicker } from './TokenAdornment'; | ||
import { BaseTokenInput, BaseTokenInputProps } from './BaseTokenInput'; | ||
import { TokenInputBalance } from './TokenInputBalance'; | ||
|
||
type Props = { | ||
balance?: string | number; | ||
humanBalance?: string | number; | ||
balanceLabel?: ReactNode; | ||
onClickBalance?: (balance: string | number) => void; | ||
ticker: TokenTicker; | ||
}; | ||
|
||
type InheritAttrs = Omit<BaseTokenInputProps, keyof Props>; | ||
|
||
type FixedTokenInputProps = Props & InheritAttrs; | ||
|
||
const FixedTokenInput = forwardRef<HTMLInputElement, FixedTokenInputProps>( | ||
( | ||
{ balance: balanceProp, humanBalance, balanceLabel, onClickBalance, ticker: tickerProp, isDisabled, id, ...props }, | ||
ref | ||
): JSX.Element => { | ||
const balance = balanceProp !== undefined && ( | ||
<TokenInputBalance | ||
balance={balanceProp} | ||
balanceHuman={humanBalance} | ||
inputId={id} | ||
isDisabled={isDisabled} | ||
label={balanceLabel} | ||
onClickBalance={onClickBalance} | ||
/> | ||
); | ||
|
||
return ( | ||
<BaseTokenInput | ||
ref={ref} | ||
balance={balance} | ||
endAdornment={<TokenAdornment ticker={tickerProp} />} | ||
id={id} | ||
isDisabled={isDisabled} | ||
{...props} | ||
/> | ||
); | ||
} | ||
); | ||
|
||
FixedTokenInput.displayName = 'FixedTokenInput'; | ||
|
||
export { FixedTokenInput }; | ||
export type { FixedTokenInputProps }; |
Oops, something went wrong.