-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added a `NumberInput` component with formatting, - Removed the default value of `10,000,000,000` of the inital supply & set as a placeholder. @0xChqrles Let me know if I should it differently!
- Loading branch information
Showing
3 changed files
with
96 additions
and
3 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,60 @@ | ||
import clsx from 'clsx' | ||
import React, { forwardRef, useEffect, useState } from 'react' | ||
import Box, { BoxProps } from 'src/theme/components/Box' | ||
|
||
import * as styles from './style.css' | ||
|
||
const formatNumber = (value: string) => { | ||
const numericValue = parseFloat(value.replace(/[^0-9.]/g, '')) | ||
if (isNaN(numericValue)) return '' | ||
|
||
return new Intl.NumberFormat('en-US', { | ||
style: 'decimal', | ||
minimumFractionDigits: 2, | ||
maximumFractionDigits: 2, | ||
}).format(numericValue) | ||
} | ||
|
||
type NumberInputProps = { | ||
addon?: React.ReactNode | ||
} & BoxProps | ||
|
||
const NumberInput = forwardRef<HTMLInputElement, NumberInputProps>( | ||
({ addon, className, value, onChange, onBlur, ...props }, ref) => { | ||
const [inputValue, setInputValue] = useState('') | ||
useEffect(() => { | ||
if (value !== undefined && value !== null) { | ||
setInputValue(formatNumber(value.toString())) | ||
} | ||
}, [value]) | ||
|
||
const handleInputEvent = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
setInputValue(event.target.value.replace(/[^0-9.]/g, '')) | ||
onChange && onChange(event) | ||
} | ||
const handleBlurEvent = (event: React.FocusEvent<HTMLInputElement>) => { | ||
setInputValue(formatNumber(event.target.value)) | ||
onBlur && onBlur(event) | ||
} | ||
|
||
return ( | ||
<Box className={clsx(styles.inputContainer, className)}> | ||
{addon} | ||
<Box | ||
as="input" | ||
type="text" | ||
{...props} | ||
ref={ref} | ||
className={styles.input} | ||
value={inputValue} | ||
onChange={handleInputEvent} | ||
onBlur={handleBlurEvent} | ||
/> | ||
</Box> | ||
) | ||
} | ||
) | ||
|
||
NumberInput.displayName = 'NumberInput' | ||
|
||
export default NumberInput |
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,33 @@ | ||
import { sprinkles } from 'src/theme/css/sprinkles.css' | ||
|
||
export const inputContainer = sprinkles({ | ||
display: 'flex', | ||
alignItems: 'center', | ||
borderRadius: '10', | ||
borderWidth: '1px', | ||
borderStyle: 'solid', | ||
overflow: 'hidden', | ||
padding: '12', | ||
fontSize: '16', | ||
borderColor: { | ||
default: 'border1', | ||
hover: 'accent', | ||
}, | ||
gap: '8', | ||
transitionDuration: '125', | ||
backgroundColor: 'bg1', | ||
}) | ||
|
||
export const input = sprinkles({ | ||
fontSize: '16', | ||
position: 'relative', | ||
whiteSpace: 'nowrap', | ||
outline: 'none', | ||
color: { | ||
default: 'text1', | ||
placeholder: 'text2', | ||
}, | ||
background: 'none', | ||
border: 'none', | ||
width: 'full', | ||
}) |
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