-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e56a890
commit 02fac48
Showing
4 changed files
with
160 additions
and
25 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
73 changes: 73 additions & 0 deletions
73
packages/bridge-ui-v2/src/components/Bridge/IDInput.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,73 @@ | ||
<script lang="ts"> | ||
import { createEventDispatcher } from 'svelte'; | ||
import { t } from 'svelte-i18n'; | ||
import { FlatAlert } from '$components/Alert'; | ||
import { Icon } from '$components/Icon'; | ||
export let numbersArray: number[] = []; | ||
enum State { | ||
Valid, | ||
Invalid, | ||
Neutral, | ||
} | ||
const dispatch = createEventDispatcher(); | ||
let inputId = 'numberInput'; | ||
let state = State.Neutral; | ||
let value: string = ''; | ||
$: { | ||
value = value.replace(/\s+/g, ''); | ||
if (value === '' || value.endsWith(',')) { | ||
state = State.Neutral; | ||
numbersArray = []; | ||
} else { | ||
const inputArray = value.split(','); | ||
const isValid = inputArray.every((item) => /^[0-9]+$/.test(item)); | ||
state = isValid ? State.Valid : State.Invalid; | ||
numbersArray = isValid ? inputArray.map((num) => parseInt(num)).filter(Boolean) : []; | ||
} | ||
} | ||
function validateInput(e: Event) { | ||
const target = e.target as HTMLInputElement; | ||
value = target.value.replace(/\s+/g, ''); | ||
dispatch('input', { value, numbersArray }); | ||
} | ||
function clear() { | ||
value = ''; | ||
numbersArray = []; | ||
state = State.Neutral; | ||
dispatch('input', { value, numbersArray }); | ||
} | ||
</script> | ||
|
||
<div class="f-col space-y-2"> | ||
<div class="f-between-center text-secondary-content"> | ||
<label class="body-regular" for={inputId}>{$t('inputs.nft.token_id.label')}</label> | ||
</div> | ||
<div class="relative f-items-center"> | ||
<input | ||
id={inputId} | ||
type="text" | ||
placeholder={$t('inputs.nft.token_id.placeholder')} | ||
bind:value | ||
on:input={validateInput} | ||
class="w-full input-box withValdiation py-6 pr-16 px-[26px] title-subsection-bold placeholder:text-tertiary-content | ||
{state === State.Valid ? 'success' : state === State.Invalid ? 'error' : ''}" /> | ||
<button class="absolute right-6 uppercase body-bold text-secondary-content" on:click={clear}> | ||
<Icon type="x-close-circle" fillClass="fill-primary-icon" size={24} /> | ||
</button> | ||
</div> | ||
</div> | ||
|
||
<div class="mt-5 min-h-[20px]"> | ||
{#if state === State.Invalid && value !== ''} | ||
<FlatAlert type="error" forceColumnFlow message={$t('inputs.address_input.errors.invalid')} /> | ||
{:else if state === State.Valid} | ||
<FlatAlert type="success" forceColumnFlow message={$t('inputs.address_input.success')} /> | ||
{/if} | ||
</div> |