Skip to content
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

Number input component #78

Merged
merged 7 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion dapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"@ledgerhq/wallet-api-client-react": "^1.1.2",
"framer-motion": "^10.16.5",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"react-number-format": "^5.3.1"
},
"devDependencies": {
"@thesis-co/eslint-config": "^0.6.1",
Expand Down
9 changes: 5 additions & 4 deletions dapp/src/components/shared/CurrencyBalance/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useMemo } from "react"
import { Box, useMultiStyleConfig } from "@chakra-ui/react"
import { Box, useMultiStyleConfig, TextProps } from "@chakra-ui/react"
import { formatTokenAmount, toLocaleString } from "../../../utils"
import { CurrencyType } from "../../../types"
import { CURRENCIES_BY_TYPE } from "../../../constants"
Expand All @@ -11,7 +11,7 @@ export type CurrencyBalanceProps = {
desiredDecimals?: number
size?: string
variant?: "greater-balance"
}
} & TextProps

export function CurrencyBalance({
currencyType,
Expand All @@ -20,6 +20,7 @@ export function CurrencyBalance({
desiredDecimals = 2,
size,
variant,
...textProps
}: CurrencyBalanceProps) {
const styles = useMultiStyleConfig("CurrencyBalance", { size, variant })

Expand All @@ -38,10 +39,10 @@ export function CurrencyBalance({

return (
<Box>
<Box as="span" __css={styles.balance}>
<Box as="span" __css={styles.balance} {...textProps}>
{balance}
</Box>
<Box as="span" __css={styles.symbol}>
<Box as="span" __css={styles.symbol} {...textProps}>
{currency.symbol}
</Box>
</Box>
Expand Down
48 changes: 48 additions & 0 deletions dapp/src/components/shared/NumberFormatInput/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from "react"
import { NumericFormat } from "react-number-format"
import { InputProps, chakra, useMultiStyleConfig } from "@chakra-ui/react"

const ChakraWrapper = chakra(NumericFormat)

export type NumberFormatInputValues = {
formattedValue: string
value: string
floatValue: number
}

type NumberFormatInputProps = {
onValueChange: (values: NumberFormatInputValues) => void
decimalScale?: number
} & InputProps

/**
* Component is from the Threshold Network React Components repository.
* It has been used because it supports the thousandth separator
* and can be easily integrated with Chakra UI.
*
* More info:
* https://github.com/threshold-network/components/blob/main/src/components/NumberFormatInput/index.tsx
*/
const NumberFormatInput = React.forwardRef<
HTMLInputElement,
NumberFormatInputProps
>((props, ref) => {
const { field: css } = useMultiStyleConfig("Input", props)

const { decimalScale, isDisabled, isInvalid, ...restProps } = props

return (
<ChakraWrapper
allowLeadingZeros={false}
thousandSeparator
decimalScale={decimalScale}
__css={css}
disabled={isDisabled}
aria-invalid={isInvalid}
getInputRef={ref}
{...restProps}
/>
)
})

export default NumberFormatInput
171 changes: 171 additions & 0 deletions dapp/src/components/shared/TokenBalanceInput/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import React, { useMemo } from "react"
import {
Box,
Button,
HStack,
Icon,
InputGroup,
InputProps,
InputRightElement,
TypographyProps,
createStylesContext,
useMultiStyleConfig,
} from "@chakra-ui/react"
import { fixedPointNumberToString } from "../../../utils"
import { CurrencyType } from "../../../types"
import { CURRENCIES_BY_TYPE } from "../../../constants"
import NumberFormatInput, {
NumberFormatInputValues,
} from "../NumberFormatInput"
import { CurrencyBalance } from "../CurrencyBalance"
import { AlertInfo } from "../../../static/icons"

const VARIANT = "balance"
const [StylesProvider, useStyles] = createStylesContext("TokenBalanceInput")

type HelperErrorTextProps = {
errorMsgText?: string | JSX.Element
hasError?: boolean
helperText?: string | JSX.Element
}

function HelperErrorText({
helperText,
errorMsgText,
hasError,
}: HelperErrorTextProps) {
const styles = useStyles()

if (hasError) {
return (
<Box as="span" __css={styles.errorMsgText}>
{errorMsgText || "Please enter a valid value"}
</Box>
)
}

if (helperText) {
return (
<HStack __css={styles.helperText}>
<Icon as={AlertInfo} />
<Box as="span">{helperText}</Box>
</HStack>
)
}
r-czajkowski marked this conversation as resolved.
Show resolved Hide resolved

return null
}

type FiatCurrencyBalanceProps = {
fiatAmount?: string
fiatCurrencyType?: CurrencyType
}

function FiatCurrencyBalance({
fiatAmount,
fiatCurrencyType,
}: FiatCurrencyBalanceProps) {
const { helperText } = useStyles()
const textProps = helperText as TypographyProps

if (fiatAmount && fiatCurrencyType) {
return (
<CurrencyBalance
currencyType={fiatCurrencyType}
amount={fiatAmount}
shouldBeFormatted={false}
{...textProps}
/>
)
}

return null
}

type TokenBalanceInputProps = {
amount?: string
currencyType: CurrencyType
tokenBalance: string | number
placeholder?: string
size?: "lg" | "md"
setAmount: (value: string) => void
} & InputProps &
HelperErrorTextProps &
FiatCurrencyBalanceProps

export default function TokenBalanceInput({
amount,
currencyType,
tokenBalance,
placeholder,
size = "lg",
setAmount,
errorMsgText,
helperText,
hasError = false,
fiatAmount,
fiatCurrencyType,
...inputProps
}: TokenBalanceInputProps) {
const styles = useMultiStyleConfig("TokenBalanceInput", { size })

const tokenBalanceAmount = useMemo(
() =>
fixedPointNumberToString(
BigInt(tokenBalance || 0),
CURRENCIES_BY_TYPE[currencyType].decimals,
),
[currencyType, tokenBalance],
)

return (
<Box __css={styles.container}>
r-czajkowski marked this conversation as resolved.
Show resolved Hide resolved
<Box __css={styles.labelContainer}>
<Box as="span" __css={styles.label}>
Amount
</Box>
<HStack>
<Box as="span" __css={styles.balance}>
Balance
</Box>
<CurrencyBalance
size={size === "lg" ? "md" : "sm"}
amount={tokenBalance}
currencyType={currencyType}
/>
</HStack>
</Box>
<InputGroup variant={VARIANT}>
<NumberFormatInput
size={size}
value={amount}
variant={VARIANT}
isInvalid={hasError}
placeholder={placeholder}
onValueChange={(values: NumberFormatInputValues) =>
setAmount(values.value)
}
{...inputProps}
/>
<InputRightElement>
<Button h="70%" onClick={() => setAmount(tokenBalanceAmount)}>
Max
</Button>
</InputRightElement>
</InputGroup>
<StylesProvider value={styles}>
<HelperErrorText
helperText={helperText}
errorMsgText={errorMsgText}
hasError={hasError}
/>
{!hasError && !helperText && (
<FiatCurrencyBalance
fiatAmount={fiatAmount}
fiatCurrencyType={fiatCurrencyType}
/>
)}
</StylesProvider>
</Box>
)
}
2 changes: 1 addition & 1 deletion dapp/src/constants/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ export const CURRENCY_ID_ETHEREUM =
export const CURRENCIES_BY_TYPE: Record<CurrencyType, Currency> = {
bitcoin: BITCOIN,
ethereum: ETHEREUM,
usd: ETHEREUM,
usd: USD,
}
44 changes: 44 additions & 0 deletions dapp/src/theme/Input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { inputAnatomy as parts } from "@chakra-ui/anatomy"
import { createMultiStyleConfigHelpers, defineStyle } from "@chakra-ui/react"

const { definePartsStyle, defineMultiStyleConfig } =
createMultiStyleConfigHelpers(parts.keys)

const variantBalanceField = defineStyle({
border: "1px solid",
borderColor: "gold.300",
color: "grey.700",
fontWeight: "bold",
bg: "opacity.white.5",
paddingRight: 20,
// TODO: Set the color correctly without using the chakra variable.
caretColor: "var(--chakra-colors-brand-400)",

_placeholder: {
color: "grey.300",
fontWeight: "medium",
},

_invalid: {
color: "red.400",
},
})

const variantBalanceElement = defineStyle({
h: "100%",
width: 14,
mr: 2,
})

const variantBalance = definePartsStyle({
field: variantBalanceField,
element: variantBalanceElement,
})

const variants = {
balance: variantBalance,
}

const Input = defineMultiStyleConfig({ variants })

export default Input
Loading
Loading