|
1 |
| -import BigNumber from "big.js" |
2 | 1 | import React from "react"
|
3 | 2 | import { Controller, useForm } from "react-hook-form"
|
4 | 3 | import { useTranslation } from "react-i18next"
|
@@ -32,9 +31,10 @@ import {
|
32 | 31 | getAccountMinimumBalance,
|
33 | 32 | getSpendableBalance
|
34 | 33 | } from "~Generic/lib/stellar"
|
| 34 | +import { FormBigNumber, isValidAmount } from "~Generic/lib/form" |
35 | 35 | import { createTransaction } from "~Generic/lib/transaction"
|
36 | 36 | import { Box, HorizontalLayout, VerticalLayout } from "~Layout/components/Box"
|
37 |
| -import { bigNumberToInputValue, isValidAmount, TradingFormValues, useCalculation } from "../hooks/form" |
| 37 | +import { bigNumberToInputValue, TradingFormValues, useCalculation } from "../hooks/form" |
38 | 38 | import TradingPrice from "./TradingPrice"
|
39 | 39 |
|
40 | 40 | const useStyles = makeStyles({
|
@@ -141,8 +141,8 @@ function TradingForm(props: Props) {
|
141 | 141 | }
|
142 | 142 |
|
143 | 143 | const validateManualPrice = React.useCallback(() => {
|
144 |
| - const value = BigNumber(manualPrice).gt(0) ? manualPrice : defaultPrice |
145 |
| - const valid = isValidAmount(value) && BigNumber(value).gt(0) |
| 144 | + const value = FormBigNumber(manualPrice).gt(0) ? manualPrice : defaultPrice |
| 145 | + const valid = isValidAmount(value) && FormBigNumber(value).gt(0) |
146 | 146 | if (!valid) {
|
147 | 147 | if (!expanded) {
|
148 | 148 | setExpanded(true)
|
@@ -272,17 +272,23 @@ function TradingForm(props: Props) {
|
272 | 272 | inputRef={form.register({
|
273 | 273 | required: t<string>("trading.validation.primary-amount-missing"),
|
274 | 274 | validate: value => {
|
275 |
| - const amountInvalid = |
276 |
| - primaryAmount.lt(0) || |
277 |
| - (value.length > 0 && primaryAmount.eq(0)) || |
| 275 | + const amountInvalid = primaryAmount.lt(0) || (value.length > 0 && primaryAmount.eq(0)) |
| 276 | + const exceedsBalance = |
278 | 277 | (props.primaryAction === "sell" && primaryBalance && primaryAmount.gt(spendablePrimaryBalance)) ||
|
279 | 278 | (props.primaryAction === "buy" && secondaryBalance && secondaryAmount.gt(spendableSecondaryBalance))
|
280 |
| - return !amountInvalid || t<string>("trading.validation.invalid-amount") |
| 279 | + |
| 280 | + if (amountInvalid) { |
| 281 | + return t<string>("trading.validation.invalid-amount") |
| 282 | + } else if (exceedsBalance) { |
| 283 | + return t<string>("trading.validation.not-enough-balance") |
| 284 | + } else { |
| 285 | + return true |
| 286 | + } |
281 | 287 | }
|
282 | 288 | })}
|
283 | 289 | error={Boolean(form.errors.primaryAmountString)}
|
284 | 290 | inputProps={{
|
285 |
| - pattern: "[0-9]*", |
| 291 | + pattern: "^[0-9]*(.[0-9]+)?$", |
286 | 292 | inputMode: "decimal",
|
287 | 293 | min: "0.0000001",
|
288 | 294 | max: maxPrimaryAmount.toFixed(7),
|
@@ -320,7 +326,6 @@ function TradingForm(props: Props) {
|
320 | 326 | )}
|
321 | 327 | required
|
322 | 328 | style={{ flexGrow: 1, flexShrink: 1, width: "55%" }}
|
323 |
| - type="number" |
324 | 329 | />
|
325 | 330 | </HorizontalLayout>
|
326 | 331 | <HorizontalLayout margin="8px 0 32px">
|
@@ -394,6 +399,12 @@ function TradingForm(props: Props) {
|
394 | 399 | }
|
395 | 400 | control={form.control}
|
396 | 401 | name="manualPrice"
|
| 402 | + rules={{ |
| 403 | + validate: value => { |
| 404 | + const valid = isValidAmount(value) |
| 405 | + return valid || t<string>("trading.validation.invalid-price") |
| 406 | + } |
| 407 | + }} |
397 | 408 | valueName="manualPrice"
|
398 | 409 | />
|
399 | 410 | </ExpansionPanelDetails>
|
|
0 commit comments