-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a26798b
Create a basic `TokenBalanceInput` component
kkosiorowska bacd2ce
Add styles when input is invalid
kkosiorowska 10815a6
Merge branch 'main' into custom-input
kkosiorowska 22914ca
Merge branch 'main' into custom-input
kkosiorowska d7cdec6
Merge branch 'main' of github.com:thesis/acre into custom-input
kkosiorowska d4fa50e
Merge branch 'main' of github.com:thesis/acre into custom-input
kkosiorowska fdd62ed
Use Form Control for `TokenBalanceInput`
kkosiorowska File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,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 |
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,170 @@ | ||
import React, { useMemo } from "react" | ||
import { | ||
Box, | ||
Button, | ||
FormControl, | ||
FormErrorMessage, | ||
FormHelperText, | ||
FormLabel, | ||
Icon, | ||
InputGroup, | ||
InputProps, | ||
InputRightElement, | ||
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" | ||
|
||
type HelperErrorTextProps = { | ||
errorMsgText?: string | JSX.Element | ||
hasError?: boolean | ||
helperText?: string | JSX.Element | ||
} | ||
|
||
function HelperErrorText({ | ||
helperText, | ||
errorMsgText, | ||
hasError, | ||
}: HelperErrorTextProps) { | ||
if (hasError) { | ||
return ( | ||
<FormErrorMessage> | ||
{errorMsgText || "Please enter a valid value"} | ||
</FormErrorMessage> | ||
) | ||
} | ||
|
||
if (helperText) { | ||
return ( | ||
<FormHelperText> | ||
<Icon as={AlertInfo} /> | ||
{helperText} | ||
</FormHelperText> | ||
) | ||
} | ||
|
||
return null | ||
} | ||
|
||
type FiatCurrencyBalanceProps = { | ||
fiatAmount?: string | ||
fiatCurrencyType?: CurrencyType | ||
} | ||
|
||
function FiatCurrencyBalance({ | ||
fiatAmount, | ||
fiatCurrencyType, | ||
}: FiatCurrencyBalanceProps) { | ||
const styles = useMultiStyleConfig("Form") | ||
const { fontWeight } = styles.helperText | ||
|
||
if (fiatAmount && fiatCurrencyType) { | ||
return ( | ||
<CurrencyBalance | ||
currencyType={fiatCurrencyType} | ||
amount={fiatAmount} | ||
shouldBeFormatted={false} | ||
fontWeight={fontWeight as string} | ||
size="sm" | ||
/> | ||
) | ||
} | ||
|
||
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 ( | ||
<FormControl isInvalid={hasError} isDisabled={inputProps.isDisabled}> | ||
<FormLabel htmlFor={inputProps.name} size={size}> | ||
<Box __css={styles.labelContainer}> | ||
Amount | ||
<Box __css={styles.balanceContainer}> | ||
<Box as="span" __css={styles.balance}> | ||
Balance | ||
</Box> | ||
<CurrencyBalance | ||
size={size === "lg" ? "md" : "sm"} | ||
amount={tokenBalance} | ||
currencyType={currencyType} | ||
/> | ||
</Box> | ||
</Box> | ||
</FormLabel> | ||
<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> | ||
<HelperErrorText | ||
helperText={helperText} | ||
errorMsgText={errorMsgText} | ||
hasError={hasError} | ||
/> | ||
{!hasError && !helperText && ( | ||
<FormHelperText> | ||
<FiatCurrencyBalance | ||
fiatAmount={fiatAmount} | ||
fiatCurrencyType={fiatCurrencyType} | ||
/> | ||
</FormHelperText> | ||
)} | ||
</FormControl> | ||
) | ||
} |
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,21 @@ | ||
import { formAnatomy as parts } from "@chakra-ui/anatomy" | ||
import { createMultiStyleConfigHelpers, defineStyle } from "@chakra-ui/react" | ||
|
||
const { defineMultiStyleConfig, definePartsStyle } = | ||
createMultiStyleConfigHelpers(parts.keys) | ||
|
||
const baseStyleHelperText = defineStyle({ | ||
display: "flex", | ||
alignItems: "center", | ||
gap: 1, | ||
fontWeight: "medium", | ||
color: "grey.500", | ||
}) | ||
|
||
const baseStyle = definePartsStyle({ | ||
helperText: baseStyleHelperText, | ||
}) | ||
|
||
const Form = defineMultiStyleConfig({ baseStyle }) | ||
|
||
export default Form |
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,19 @@ | ||
import { defineStyle, createMultiStyleConfigHelpers } from "@chakra-ui/react" | ||
|
||
import { formErrorAnatomy as parts } from "@chakra-ui/anatomy" | ||
|
||
const { defineMultiStyleConfig, definePartsStyle } = | ||
createMultiStyleConfigHelpers(parts.keys) | ||
|
||
const baseStyleText = defineStyle({ | ||
fontWeight: "medium", | ||
color: "red.400", | ||
}) | ||
|
||
const baseStyle = definePartsStyle({ | ||
text: baseStyleText, | ||
}) | ||
|
||
const FormError = defineMultiStyleConfig({ baseStyle }) | ||
|
||
export default FormError |
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,25 @@ | ||
import { defineStyle, defineStyleConfig } from "@chakra-ui/react" | ||
|
||
const baseStyle = defineStyle({ | ||
fontWeight: "semibold", | ||
color: "grey.700", | ||
}) | ||
|
||
const sizeMd = defineStyle({ | ||
fontSize: "sm", | ||
lineHeight: "sm", | ||
}) | ||
|
||
const sizeLg = defineStyle({ | ||
fontSize: "md", | ||
lineHeight: "md", | ||
}) | ||
|
||
const sizes = { | ||
md: sizeMd, | ||
lg: sizeLg, | ||
} | ||
|
||
const FormLabel = defineStyleConfig({ baseStyle, sizes }) | ||
|
||
export default FormLabel |
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,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 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@r-czajkowski I still don't quite sure how best to style this part. Let me know if you have any better ideas.