-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #211 from stabilitydao/4-vault
invest form fix
- Loading branch information
Showing
9 changed files
with
757 additions
and
665 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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,27 @@ | ||
const handleInputKeyDown = ( | ||
evt: React.KeyboardEvent<HTMLInputElement>, | ||
currentValue: string | ||
): void => { | ||
if ( | ||
!/[\d.]/.test(evt.key) && | ||
evt.key !== "Backspace" && | ||
evt.key !== "ArrowLeft" && | ||
evt.key !== "ArrowRight" | ||
) { | ||
evt.preventDefault(); | ||
} | ||
|
||
if (evt.key === "0" && currentValue === "0") { | ||
evt.preventDefault(); | ||
} | ||
|
||
if (/^\d/.test(evt.key) && currentValue === "0" && evt.key !== ".") { | ||
evt.preventDefault(); | ||
} | ||
|
||
if (evt.key === "." && currentValue && currentValue.includes(".")) { | ||
evt.preventDefault(); | ||
} | ||
}; | ||
|
||
export { handleInputKeyDown }; |
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 |
---|---|---|
@@ -1,5 +1,11 @@ | ||
import { getAssetAllowance } from "./getAssetAllowance"; | ||
import { getPlatformBalance } from "./getPlatformBalance"; | ||
import { getAssetsBalances } from "./getAssetsBalances"; | ||
import { handleInputKeyDown } from "./handleInputKeyDown"; | ||
|
||
export { getAssetAllowance, getPlatformBalance, getAssetsBalances }; | ||
export { | ||
getAssetAllowance, | ||
getPlatformBalance, | ||
getAssetsBalances, | ||
handleInputKeyDown, | ||
}; |
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,79 @@ | ||
import { describe, it, expect, vi } from "vitest"; | ||
import { handleInputKeyDown } from "../functions"; | ||
|
||
describe("handleInputKeyDown", () => { | ||
it("should allow digits to be entered", () => { | ||
const preventDefault = vi.fn(); | ||
const event = { | ||
key: "1", | ||
preventDefault, | ||
} as unknown as React.KeyboardEvent<HTMLInputElement>; | ||
|
||
handleInputKeyDown(event, ""); | ||
|
||
expect(preventDefault).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should prevent multiple leading zeroes", () => { | ||
const preventDefault = vi.fn(); | ||
const event = { | ||
key: "0", | ||
preventDefault, | ||
} as unknown as React.KeyboardEvent<HTMLInputElement>; | ||
|
||
handleInputKeyDown(event, "0"); | ||
|
||
expect(preventDefault).toHaveBeenCalled(); | ||
}); | ||
|
||
it("should allow a single decimal point", () => { | ||
const preventDefault = vi.fn(); | ||
const event = { | ||
key: ".", | ||
preventDefault, | ||
} as unknown as React.KeyboardEvent<HTMLInputElement>; | ||
|
||
handleInputKeyDown(event, "123"); | ||
|
||
expect(preventDefault).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should prevent multiple decimal points", () => { | ||
const preventDefault = vi.fn(); | ||
const event = { | ||
key: ".", | ||
preventDefault, | ||
} as unknown as React.KeyboardEvent<HTMLInputElement>; | ||
|
||
handleInputKeyDown(event, "123."); | ||
|
||
expect(preventDefault).toHaveBeenCalled(); | ||
}); | ||
|
||
it("should allow backspace and arrow keys", () => { | ||
const preventDefault = vi.fn(); | ||
|
||
["Backspace", "ArrowLeft", "ArrowRight"].forEach((key) => { | ||
const event = { | ||
key, | ||
preventDefault, | ||
} as unknown as React.KeyboardEvent<HTMLInputElement>; | ||
|
||
handleInputKeyDown(event, "123"); | ||
|
||
expect(preventDefault).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
it("should prevent invalid characters", () => { | ||
const preventDefault = vi.fn(); | ||
const event = { | ||
key: "a", | ||
preventDefault, | ||
} as unknown as React.KeyboardEvent<HTMLInputElement>; | ||
|
||
handleInputKeyDown(event, "123"); | ||
|
||
expect(preventDefault).toHaveBeenCalled(); | ||
}); | ||
}); |
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
Oops, something went wrong.