Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
interface
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
LuckySwap
interface
Commits
ec784ccb
Unverified
Commit
ec784ccb
authored
Sep 26, 2023
by
Jack Short
Committed by
GitHub
Sep 26, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: updating input currency panel to handle comma separator (#7336)
parent
20d84047
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
2 deletions
+19
-2
index.tsx
src/components/NumericalInput/index.tsx
+18
-1
formatNumbers.ts
src/utils/formatNumbers.ts
+1
-1
No files found.
src/components/NumericalInput/index.tsx
View file @
ec784ccb
import
{
SupportedLocale
}
from
'
constants/locales
'
import
React
,
{
forwardRef
}
from
'
react
'
import
React
,
{
forwardRef
}
from
'
react
'
import
styled
from
'
styled-components
'
import
styled
from
'
styled-components
'
import
{
escapeRegExp
}
from
'
utils
'
import
{
escapeRegExp
}
from
'
utils
'
import
{
useFormatterLocales
}
from
'
utils/formatNumbers
'
const
StyledInput
=
styled
.
input
<
{
error
?:
boolean
;
fontSize
?:
string
;
align
?:
string
;
disabled
?:
boolean
}
>
`
const
StyledInput
=
styled
.
input
<
{
error
?:
boolean
;
fontSize
?:
string
;
align
?:
string
;
disabled
?:
boolean
}
>
`
color:
${({
error
,
theme
})
=>
(
error
?
theme
.
critical
:
theme
.
neutral1
)}
;
color:
${({
error
,
theme
})
=>
(
error
?
theme
.
critical
:
theme
.
neutral1
)}
;
...
@@ -39,6 +41,12 @@ const StyledInput = styled.input<{ error?: boolean; fontSize?: string; align?: s
...
@@ -39,6 +41,12 @@ const StyledInput = styled.input<{ error?: boolean; fontSize?: string; align?: s
}
}
`
`
function
localeUsesComma
(
locale
:
SupportedLocale
):
boolean
{
const
decimalSeparator
=
new
Intl
.
NumberFormat
(
locale
).
format
(
1.1
)[
1
]
return
decimalSeparator
===
'
,
'
}
const
inputRegex
=
RegExp
(
`^\\d*(?:\\\\[.])?\\d*$`
)
// match escaped "." characters via in a non-capturing group
const
inputRegex
=
RegExp
(
`^\\d*(?:\\\\[.])?\\d*$`
)
// match escaped "." characters via in a non-capturing group
interface
InputProps
extends
Omit
<
React
.
HTMLProps
<
HTMLInputElement
>
,
'
ref
'
|
'
onChange
'
|
'
as
'
>
{
interface
InputProps
extends
Omit
<
React
.
HTMLProps
<
HTMLInputElement
>
,
'
ref
'
|
'
onChange
'
|
'
as
'
>
{
...
@@ -52,17 +60,26 @@ interface InputProps extends Omit<React.HTMLProps<HTMLInputElement>, 'ref' | 'on
...
@@ -52,17 +60,26 @@ interface InputProps extends Omit<React.HTMLProps<HTMLInputElement>, 'ref' | 'on
const
Input
=
forwardRef
<
HTMLInputElement
,
InputProps
>
(
const
Input
=
forwardRef
<
HTMLInputElement
,
InputProps
>
(
({
value
,
onUserInput
,
placeholder
,
prependSymbol
,
...
rest
}:
InputProps
,
ref
)
=>
{
({
value
,
onUserInput
,
placeholder
,
prependSymbol
,
...
rest
}:
InputProps
,
ref
)
=>
{
const
{
formatterLocale
}
=
useFormatterLocales
()
const
enforcer
=
(
nextUserInput
:
string
)
=>
{
const
enforcer
=
(
nextUserInput
:
string
)
=>
{
if
(
nextUserInput
===
''
||
inputRegex
.
test
(
escapeRegExp
(
nextUserInput
)))
{
if
(
nextUserInput
===
''
||
inputRegex
.
test
(
escapeRegExp
(
nextUserInput
)))
{
onUserInput
(
nextUserInput
)
onUserInput
(
nextUserInput
)
}
}
}
}
const
formatValueWithLocale
=
(
value
:
string
|
number
)
=>
{
const
[
searchValue
,
replaceValue
]
=
localeUsesComma
(
formatterLocale
)
?
[
/
\.
/g
,
'
,
'
]
:
[
/,/g
,
'
.
'
]
return
value
.
toString
().
replace
(
searchValue
,
replaceValue
)
}
const
valueFormattedWithLocale
=
formatValueWithLocale
(
value
)
return
(
return
(
<
StyledInput
<
StyledInput
{
...
rest
}
{
...
rest
}
ref=
{
ref
}
ref=
{
ref
}
value=
{
prependSymbol
&&
value
?
prependSymbol
+
value
:
valu
e
}
value=
{
prependSymbol
&&
value
?
prependSymbol
+
value
FormattedWithLocale
:
valueFormattedWithLocal
e
}
onChange=
{
(
event
)
=>
{
onChange=
{
(
event
)
=>
{
if
(
prependSymbol
)
{
if
(
prependSymbol
)
{
const
value
=
event
.
target
.
value
const
value
=
event
.
target
.
value
...
...
src/utils/formatNumbers.ts
View file @
ec784ccb
...
@@ -579,7 +579,7 @@ function formatReviewSwapCurrencyAmount(
...
@@ -579,7 +579,7 @@ function formatReviewSwapCurrencyAmount(
return
formattedAmount
return
formattedAmount
}
}
function
useFormatterLocales
():
{
export
function
useFormatterLocales
():
{
formatterLocale
:
SupportedLocale
formatterLocale
:
SupportedLocale
formatterLocalCurrency
:
SupportedLocalCurrency
formatterLocalCurrency
:
SupportedLocalCurrency
}
{
}
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment