-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: update input styles - [x] Update input styles for the application - [x] Add manual import for identity * fix: add missing exports --------- Co-authored-by: 0xmad <0xmad@users.noreply.github.com>
- Loading branch information
Showing
28 changed files
with
644 additions
and
335 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
67 changes: 67 additions & 0 deletions
67
packages/app/src/ui/components/BigNumberInput/BigNumberInput.tsx
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,67 @@ | ||
import Box from "@mui/material/Box"; | ||
import InputAdornment from "@mui/material/InputAdornment"; | ||
import Tooltip from "@mui/material/Tooltip"; | ||
import Typography from "@mui/material/Typography"; | ||
import { bigintToHex } from "bigint-conversion"; | ||
import omit from "lodash/omit"; | ||
import { forwardRef, type Ref } from "react"; | ||
|
||
import { ellipsify } from "@src/util/account"; | ||
|
||
import type { TextFieldProps } from "@mui/material/TextField"; | ||
|
||
import { Input } from "../Input"; | ||
|
||
import { useBigNumberInput } from "./useBigNumberInput"; | ||
|
||
export interface IBigNumberInputProps extends TextFieldProps<"filled"> { | ||
label: string; | ||
errorMessage?: string; | ||
} | ||
|
||
const BigNumberInputUI = ( | ||
{ | ||
id, | ||
label, | ||
value, | ||
onBlur: onBlurHandler = undefined, | ||
onFocus: onFocusHandler = undefined, | ||
...rest | ||
}: IBigNumberInputProps, | ||
inputRef: Ref<HTMLInputElement | HTMLDivElement>, | ||
): JSX.Element => { | ||
const { isFocused, isInitialized, isHex, onBlur, onFocus, onToggleHex } = useBigNumberInput({ | ||
onBlurHandler, | ||
onFocusHandler, | ||
}); | ||
const formattedValue = isHex && typeof value === "string" ? bigintToHex(BigInt(value)) : (value as string); | ||
|
||
return ( | ||
<Box> | ||
<Tooltip title={!isFocused ? formattedValue : ""}> | ||
<Input | ||
id={id} | ||
inputRef={isInitialized ? inputRef : undefined} | ||
label={label} | ||
value={!isFocused ? ellipsify(formattedValue) : formattedValue} | ||
onBlur={onBlur} | ||
onFocus={onFocus} | ||
{...omit(rest, ["ref"])} | ||
InputProps={{ | ||
...rest.InputProps, | ||
readOnly: isHex || rest.InputProps?.readOnly, | ||
endAdornment: ( | ||
<InputAdornment position="end"> | ||
<Typography color="primary.main" sx={{ cursor: "pointer" }} onClick={onToggleHex}> | ||
{isHex ? "Dec" : "Hex"} | ||
</Typography> | ||
</InputAdornment> | ||
), | ||
}} | ||
/> | ||
</Tooltip> | ||
</Box> | ||
); | ||
}; | ||
|
||
export const BigNumberInput = forwardRef<HTMLInputElement | HTMLDivElement, IBigNumberInputProps>(BigNumberInputUI); |
87 changes: 87 additions & 0 deletions
87
packages/app/src/ui/components/BigNumberInput/__tests__/BigNumberInput.test.tsx
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,87 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import { render } from "@testing-library/react"; | ||
import { bigintToHex } from "bigint-conversion"; | ||
|
||
import { mockDefaultIdentitySecret, mockDefaultIdentitySecretHex } from "@src/config/mock/zk"; | ||
import { ellipsify } from "@src/util/account"; | ||
|
||
import { IBigNumberInputProps, BigNumberInput } from "../BigNumberInput"; | ||
import { IUseBigNumberInputData, useBigNumberInput } from "../useBigNumberInput"; | ||
|
||
jest.mock("bigint-conversion", (): unknown => ({ | ||
bigintToHex: jest.fn(), | ||
})); | ||
|
||
jest.mock("../useBigNumberInput", (): unknown => ({ | ||
useBigNumberInput: jest.fn(), | ||
})); | ||
|
||
describe("ui/components/BigNumberInput", () => { | ||
const defaultProps: IBigNumberInputProps = { | ||
label: "Label", | ||
value: mockDefaultIdentitySecret, | ||
variant: "filled", | ||
}; | ||
|
||
const defaultHookData: IUseBigNumberInputData = { | ||
isFocused: false, | ||
isInitialized: true, | ||
isHex: false, | ||
onBlur: jest.fn(), | ||
onFocus: jest.fn(), | ||
onToggleHex: jest.fn(), | ||
}; | ||
|
||
beforeEach(() => { | ||
(bigintToHex as jest.Mock).mockReturnValue(mockDefaultIdentitySecretHex); | ||
|
||
(useBigNumberInput as jest.Mock).mockReturnValue(defaultHookData); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test("should render focused state properly", async () => { | ||
(useBigNumberInput as jest.Mock).mockReturnValue({ ...defaultHookData, isFocused: true, isInitialized: false }); | ||
|
||
const { findByText, findByDisplayValue } = render(<BigNumberInput {...defaultProps} />); | ||
|
||
const label = await findByText(defaultProps.label); | ||
const hex = await findByText("Hex"); | ||
const value = await findByDisplayValue(mockDefaultIdentitySecret); | ||
|
||
expect(label).toBeInTheDocument(); | ||
expect(hex).toBeInTheDocument(); | ||
expect(value).toBeInTheDocument(); | ||
}); | ||
|
||
test("should render dec properly", async () => { | ||
const { findByText, findByDisplayValue } = render(<BigNumberInput {...defaultProps} />); | ||
|
||
const label = await findByText(defaultProps.label); | ||
const hex = await findByText("Hex"); | ||
const value = await findByDisplayValue(ellipsify(mockDefaultIdentitySecret)); | ||
|
||
expect(label).toBeInTheDocument(); | ||
expect(hex).toBeInTheDocument(); | ||
expect(value).toBeInTheDocument(); | ||
}); | ||
|
||
test("should render hex properly", async () => { | ||
(useBigNumberInput as jest.Mock).mockReturnValue({ ...defaultHookData, isHex: true }); | ||
|
||
const { findByText, findByDisplayValue } = render(<BigNumberInput {...defaultProps} />); | ||
|
||
const label = await findByText(defaultProps.label); | ||
const hex = await findByText("Dec"); | ||
const value = await findByDisplayValue(ellipsify(mockDefaultIdentitySecretHex)); | ||
|
||
expect(label).toBeInTheDocument(); | ||
expect(hex).toBeInTheDocument(); | ||
expect(value).toBeInTheDocument(); | ||
}); | ||
}); |
47 changes: 47 additions & 0 deletions
47
packages/app/src/ui/components/BigNumberInput/__tests__/useBigNumberInput.test.ts
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,47 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import { act, renderHook, waitFor } from "@testing-library/react"; | ||
import { type FocusEvent as ReactFocusEvent } from "react"; | ||
|
||
import { IUseBigNumberInputArgs, useBigNumberInput } from "../useBigNumberInput"; | ||
|
||
describe("ui/components/BigNumberInput/useBigNumberInput", () => { | ||
const defaultArgs: IUseBigNumberInputArgs = { | ||
onBlurHandler: jest.fn(), | ||
onFocusHandler: jest.fn(), | ||
}; | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test("should return initial data", async () => { | ||
const { result } = renderHook(() => useBigNumberInput(defaultArgs)); | ||
await waitFor(() => result.current.isInitialized); | ||
|
||
expect(result.current.isFocused).toBe(false); | ||
expect(result.current.isHex).toBe(false); | ||
}); | ||
|
||
test("should toogle dec/hex value properly", async () => { | ||
const { result } = renderHook(() => useBigNumberInput(defaultArgs)); | ||
await waitFor(() => result.current.isInitialized); | ||
|
||
act(() => result.current.onToggleHex()); | ||
|
||
expect(result.current.isHex).toBe(true); | ||
}); | ||
|
||
test("should toogle focus value properly", async () => { | ||
const { result } = renderHook(() => useBigNumberInput(defaultArgs)); | ||
await waitFor(() => result.current.isInitialized); | ||
|
||
act(() => result.current.onFocus({} as ReactFocusEvent<HTMLInputElement | HTMLTextAreaElement>)); | ||
expect(result.current.isFocused).toBe(true); | ||
|
||
act(() => result.current.onBlur({} as ReactFocusEvent<HTMLInputElement | HTMLTextAreaElement>)); | ||
expect(result.current.isFocused).toBe(false); | ||
}); | ||
}); |
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 @@ | ||
export { BigNumberInput } from "./BigNumberInput"; |
59 changes: 59 additions & 0 deletions
59
packages/app/src/ui/components/BigNumberInput/useBigNumberInput.ts
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,59 @@ | ||
import { useCallback, useState, type FocusEvent as ReactFocusEvent, useEffect } from "react"; | ||
|
||
type FocusEventType = ReactFocusEvent<HTMLInputElement | HTMLTextAreaElement>; | ||
|
||
export interface IUseBigNumberInputArgs { | ||
onBlurHandler?: (event: FocusEventType) => void; | ||
onFocusHandler?: (event: FocusEventType) => void; | ||
} | ||
|
||
export interface IUseBigNumberInputData { | ||
isFocused: boolean; | ||
isInitialized: boolean; | ||
isHex: boolean; | ||
onBlur: (event: FocusEventType) => void; | ||
onFocus: (event: FocusEventType) => void; | ||
onToggleHex: () => void; | ||
} | ||
|
||
export const useBigNumberInput = ({ | ||
onBlurHandler, | ||
onFocusHandler, | ||
}: IUseBigNumberInputArgs): IUseBigNumberInputData => { | ||
const [isInitialized, setIsInitialized] = useState(false); | ||
const [isHex, setIsHex] = useState(false); | ||
const [isFocused, setIsFocused] = useState(false); | ||
|
||
const onFocus = useCallback( | ||
(event: FocusEventType) => { | ||
setIsFocused(true); | ||
onFocusHandler?.(event); | ||
}, | ||
[setIsFocused, onFocusHandler], | ||
); | ||
|
||
const onBlur = useCallback( | ||
(event: FocusEventType) => { | ||
setIsFocused(false); | ||
onBlurHandler?.(event); | ||
}, | ||
[setIsFocused, onBlurHandler], | ||
); | ||
|
||
const onToggleHex = useCallback(() => { | ||
setIsHex((value) => !value); | ||
}, [setIsHex]); | ||
|
||
useEffect(() => { | ||
setIsInitialized(true); | ||
}, [setIsInitialized]); | ||
|
||
return { | ||
isFocused, | ||
isInitialized, | ||
isHex, | ||
onBlur, | ||
onFocus, | ||
onToggleHex, | ||
}; | ||
}; |
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
Oops, something went wrong.