-
Notifications
You must be signed in to change notification settings - Fork 6
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 #1185 from rainlanguage/01-20-2024-migrate-inputhe…
…x-components-to-ui-components 01 20 2024 migrate inputhex components to UI components
- Loading branch information
Showing
5 changed files
with
74 additions
and
49 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,22 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { render, screen } from '@testing-library/svelte'; | ||
import InputHex from '../lib/components/input/InputHex.svelte'; | ||
|
||
describe('InputHex', () => { | ||
it('renders an input element', () => { | ||
render(InputHex); | ||
expect(screen.getByRole('textbox')).toBeTruthy(); | ||
}); | ||
|
||
it('initializes with empty string when no value provided', () => { | ||
render(InputHex); | ||
const input = screen.getByRole('textbox') as HTMLInputElement; | ||
expect(input.value).toBe(''); | ||
}); | ||
|
||
it('displays hex value when bigint is provided', () => { | ||
render(InputHex, { props: { value: 255n } }); | ||
const input = screen.getByRole('textbox') as HTMLInputElement; | ||
expect(input.value).toBe('0xff'); | ||
}); | ||
}); |
50 changes: 50 additions & 0 deletions
50
packages/ui-components/src/lib/components/input/InputHex.svelte
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,50 @@ | ||
<script lang="ts"> | ||
import type { InputMask } from 'imask'; | ||
import { imask } from '@imask/svelte'; | ||
import { fromHex, toHex } from 'viem'; | ||
import { HEX_INPUT_REGEX } from '../../utils/hex'; | ||
let valueRaw: string = ''; | ||
export let value: bigint | undefined; | ||
export let required = true; | ||
$: { | ||
if (value !== undefined) { | ||
valueRaw = toHex(value); | ||
} | ||
} | ||
const maskOptions = { | ||
// hexadecimal string, optionally starting with 0x | ||
mask: HEX_INPUT_REGEX, | ||
lazy: false | ||
}; | ||
function complete({ detail }: { detail: InputMask }) { | ||
valueRaw = detail.value; | ||
if (detail.unmaskedValue.length === 0) { | ||
value = 0n; | ||
} else { | ||
let valuePrefixed = detail.unmaskedValue; | ||
if (detail.unmaskedValue.substring(0, 2) !== '0x') { | ||
valuePrefixed = `0x${valuePrefixed}`; | ||
} | ||
try { | ||
value = fromHex(valuePrefixed as `0x${string}`, 'bigint'); | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
} catch (e) { | ||
value = 0n; | ||
} | ||
} | ||
} | ||
</script> | ||
|
||
<input | ||
{required} | ||
type="text" | ||
value={valueRaw} | ||
class="focus:border-primary-500 focus:ring-primary-500 dark:focus:border-primary-500 dark:focus:ring-primary-500 block w-full rounded-lg border-gray-300 bg-gray-50 p-2.5 text-sm text-gray-900 disabled:cursor-not-allowed disabled:opacity-50 rtl:text-right dark:border-gray-500 dark:bg-gray-600 dark:text-white dark:placeholder-gray-400" | ||
use:imask={maskOptions} | ||
on:complete={complete} | ||
/> |
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 was deleted.
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