-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Number input & form improvments (#10)
- WIP: number input. - Rearrange form setup - All around work on form components
- Loading branch information
Showing
19 changed files
with
266 additions
and
130 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 |
---|---|---|
|
@@ -76,3 +76,8 @@ | |
@apply bg-background text-foreground; | ||
} | ||
} | ||
@layer components { | ||
.super-debug--code { | ||
width: 500px; | ||
} | ||
} |
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
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
32 changes: 13 additions & 19 deletions
32
apps/svelte-gravity-forms/src/lib/components/label/label.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 |
---|---|---|
@@ -1,26 +1,20 @@ | ||
<script lang="ts"> | ||
import { Label as LabelPrimitive } from 'bits-ui'; | ||
import { getFormField } from 'formsnap'; | ||
import { cn } from '$lib/utils.js'; | ||
import { getFormField, type LabelProps } from 'formsnap'; | ||
type $$Props = LabelPrimitive.Props; | ||
type $$Events = LabelPrimitive.Events; | ||
let className: $$Props['class'] = undefined; | ||
export { className as class }; | ||
const { errors } = getFormField(); | ||
$: className = $errors ? cn($errors && 'text-destructive', className) : className; | ||
type $$Props = LabelProps; | ||
const { actions, errors, ids } = getFormField(); | ||
$: attrs = { | ||
'data-fs-label': '', | ||
'data-fs-error': $errors ? '' : undefined, | ||
for: $ids.input | ||
}; | ||
</script> | ||
|
||
<LabelPrimitive.Root | ||
class={cn( | ||
'text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70', | ||
className | ||
)} | ||
<label | ||
class="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70" | ||
use:actions.label | ||
{...$$restProps} | ||
on:mousedown | ||
{...attrs} | ||
> | ||
<slot /> | ||
</LabelPrimitive.Root> | ||
</label> |
10 changes: 10 additions & 0 deletions
10
apps/svelte-gravity-forms/src/lib/components/number/index.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,10 @@ | ||
import Root from './input.svelte'; | ||
import type { InputEvents } from '../input/index.js'; | ||
|
||
export { | ||
Root, | ||
// | ||
Root as NumberInput, | ||
// | ||
type InputEvents | ||
}; |
74 changes: 74 additions & 0 deletions
74
apps/svelte-gravity-forms/src/lib/components/number/input.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,74 @@ | ||
<script lang="ts" context="module"> | ||
import type { HTMLInputAttributes } from 'svelte/elements'; | ||
import type { InputEvents } from './index.js'; | ||
import type { NumberFormats } from '$lib/internal/types.js'; | ||
</script> | ||
|
||
<script lang="ts"> | ||
import { writable } from 'svelte/store'; | ||
import { getFormField } from 'formsnap'; | ||
import { cn } from '$lib/utils.js'; | ||
import { Input } from '../input/index.js'; | ||
type $$Props = HTMLInputAttributes & { | ||
numberFormat?: NumberFormats; | ||
}; | ||
type $$Events = InputEvents; | ||
let className: $$Props['class'] = undefined; | ||
export let numberFormat: $$Props['numberFormat'] = undefined; | ||
const { attrStore, errors, value } = getFormField(); | ||
$: attrs = { | ||
'data-svelte-gf-input': '', | ||
'data-svelte-gf-error': $errors ? '' : undefined, | ||
...$attrStore | ||
}; | ||
export { className as class }; | ||
function handleKeyPress(e: KeyboardEvent) { | ||
const charCode = e.which ? e.which : e.keyCode; | ||
if ( | ||
charCode > 31 && | ||
(charCode < 48 || charCode > 57) && | ||
charCode !== 46 && | ||
!(numberFormat === 'decimal_comma' && charCode === 44) && | ||
!(numberFormat === 'decimal_dot' && charCode === 46) | ||
) { | ||
e.preventDefault(); | ||
} | ||
} | ||
let numericValue = writable<number | unknown>(undefined); | ||
let inputValue = writable<string | number | unknown>($value); | ||
$: { | ||
if (!isNaN(Number($value)) && $value !== '') { | ||
$numericValue = Number($value); | ||
} else { | ||
$numericValue = undefined; // allow the input to be empty | ||
} | ||
if ($numericValue !== undefined && $numericValue !== '') { | ||
$value = $numericValue; | ||
} else { | ||
$value = undefined; | ||
} | ||
$inputValue = | ||
$numericValue !== undefined && $numericValue !== '' ? Number($numericValue) : undefined; | ||
} | ||
</script> | ||
|
||
<Input | ||
class={cn( | ||
'flex h-10 w-full rounded-md border border-input bg-transparent px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50', | ||
className | ||
)} | ||
{...attrs} | ||
type="number" | ||
step="1.00" | ||
bind:value={$value} | ||
on:keypress={handleKeyPress} | ||
{...$$restProps} | ||
/> |
Oops, something went wrong.