Skip to content

Commit a063fad

Browse files
committed
docs: update checkbox
1 parent 9f260a9 commit a063fad

File tree

12 files changed

+286
-11
lines changed

12 files changed

+286
-11
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<script lang="ts">
2+
import Button from '$lib/buttons/Button.svelte';
3+
import type { ButtonColorType, SizeType } from '$lib/types';
4+
import { twMerge } from 'tailwind-merge';
5+
import type { Snippet } from 'svelte';
6+
7+
interface Props {
8+
children: Snippet;
9+
class?: string | undefined;
10+
group?: (string | number)[];
11+
value?: string | number;
12+
checked?: boolean | undefined;
13+
inline?: boolean;
14+
pill?: boolean;
15+
outline?: boolean;
16+
size?: SizeType;
17+
color?: ButtonColorType;
18+
shadow?: boolean;
19+
}
20+
let { children, class: className, group = [], value = 'on', checked, inline = true, pill, outline, size, color, shadow, ...restProps }: Props = $props();
21+
22+
// react on external group changes
23+
function init(_: HTMLElement, _group: (string | number)[]) {
24+
if (checked === undefined && value !== undefined) checked = _group.includes(value);
25+
onChange();
26+
27+
return {
28+
update(_group: (string | number)[]) {
29+
if (value !== undefined) {
30+
checked = _group.includes(value);
31+
}
32+
}
33+
};
34+
}
35+
36+
function onChange() {
37+
// There's a bug in Svelte and bind:group is not working with wrapped checkbox
38+
// This workaround is taken from:
39+
// https://svelte.dev/repl/de117399559f4e7e9e14e2fc9ab243cc?version=3.12.1
40+
const index = group.indexOf(value);
41+
42+
if (checked === undefined) checked = index >= 0;
43+
44+
if (checked) {
45+
if (index < 0) {
46+
group.push(value);
47+
group = group;
48+
}
49+
} else {
50+
if (index >= 0) {
51+
group.splice(index, 1);
52+
group = group;
53+
}
54+
}
55+
}
56+
57+
let buttonClass: string = $derived(twMerge(inline ? 'inline-flex' : 'flex', className));
58+
</script>
59+
60+
<Button tag="label" {checked} {pill} {outline} {size} {color} {shadow} class={buttonClass}>
61+
<input
62+
use:init={group}
63+
type="checkbox"
64+
bind:checked
65+
{value}
66+
{...restProps}
67+
class="sr-only"
68+
onchange={onChange}/>
69+
{@render children()}
70+
</Button>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import CheckboxButton from "./CheckboxButton.svelte";
2+
3+
4+
export { CheckboxButton };
5+
6+

src/lib/forms/checkbox-button/theme.ts

Whitespace-only changes.

src/lib/forms/checkbox/Checkbox.svelte

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
<script lang="ts">
2-
import { twMerge } from 'tailwind-merge';
3-
import { labelCls, inputCls } from '../radio/Radio.svelte';
42
import Label from '../label/Label.svelte';
53
import { type CheckboxProps as Props, checkbox } from '.';
64
@@ -12,7 +10,8 @@
1210
inline,
1311
tinted,
1412
rounded,
15-
group = [],
13+
group = $bindable([]),
14+
choices = [],
1615
checked = $bindable(false),
1716
spacing,
1817
groupLabelClass,
@@ -25,7 +24,19 @@
2524
checkbox({ color, tinted, custom, rounded, inline })
2625
);
2726
</script>
28-
27+
{#if choices.length > 0}
28+
{#each choices as {value, checkboxLabel}, i}
29+
<Label class={label({ class: groupLabelClass })} for={`checkbox-${i}`}>
30+
{ checkboxLabel }
31+
<input id={`checkbox-${i}`} type="checkbox" value={ value } bind:group {...attributes}
32+
class={base({ class: className })}
33+
/>
34+
{#if children}
35+
{@render children()}
36+
{/if}
37+
</Label>
38+
{/each}
39+
{:else}
2940
<Label class={label({ class: groupLabelClass })}>
3041
<input
3142
type="checkbox"
@@ -39,6 +50,7 @@
3950
{@render children()}
4051
{/if}
4152
</Label>
53+
{/if}
4254

4355
<!--
4456
@component

src/lib/forms/checkbox/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ import type { HTMLInputAttributes } from 'svelte/elements';
55
import { checkbox } from './theme';
66
import { type VariantProps } from 'tailwind-variants';
77

8+
type CheckboxItem = {
9+
value: string;
10+
checkboxLabel?: string;
11+
isChecked?: boolean;
12+
};
13+
814
type ColorType = VariantProps<typeof checkbox>['color'];
915
interface CheckboxProps extends HTMLInputAttributes {
1016
children?: Snippet;
@@ -15,11 +21,12 @@ interface CheckboxProps extends HTMLInputAttributes {
1521
tinted?: boolean;
1622
rounded?: boolean;
1723
group?: (string | number)[];
24+
choices?: CheckboxItem[];
1825
checked?: boolean | undefined | null;
1926
spacing?: string | undefined | null;
2027
groupLabelClass?: string | undefined;
2128
indeterminate?: boolean;
2229
class?: string | undefined | null;
2330
}
2431

25-
export { Checkbox, checkbox, type CheckboxProps };
32+
export { Checkbox, checkbox, type CheckboxProps, type CheckboxItem, type ColorType };

src/lib/forms/checkbox/theme.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { tv } from 'tailwind-variants';
22

33
export const checkbox = tv({
44
slots: {
5-
base: 'w-4 h-4 bg-gray-100 border-gray-300 dark:ring-offset-gray-800 focus:ring-2 me-2',
5+
base: 'w-4 h-4 bg-gray-100 border-gray-300 dark:ring-offset-gray-800 focus:ring-2 me-2 rounded',
66
label: ''
77
},
88
variants: {

src/lib/forms/search/Search.svelte

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<script lang="ts">
2+
// import type { FormSizeType } from '$lib/types';
3+
import {Input} from '$lib';
4+
import type { Action } from 'svelte/action';
5+
import type { Snippet } from 'svelte';
6+
7+
interface Props {
8+
children?: Snippet;
9+
right?: Snippet;
10+
size?: 'sm' | 'md' | 'lg' ;
11+
placeholder?: string;
12+
value?: any;
13+
show?: boolean;
14+
use?: Action<HTMLElement, any>;
15+
class?: string | undefined | null;
16+
}
17+
18+
let { children, right, size = 'lg', placeholder = 'Search', value, show = true, use = () => {}, class: className, ...restProps }: Props = $props();
19+
20+
// export let size: FormSizeType = 'lg';
21+
// export let placeholder: string = 'Search';
22+
// export let value: any = undefined;
23+
// export let show: boolean = true;
24+
// export let use: Action<HTMLElement, any> = () => {};
25+
26+
const sizes = {
27+
sm: 'w-3.5 h-3.5',
28+
md: 'w-5 h-5',
29+
lg: 'w-6 h-6'
30+
};
31+
</script>
32+
33+
{#if show}
34+
<div class="relative w-full" use:use>
35+
<Input bind:value type="search" {placeholder} inputSize={size} {...restProps} class={className}>
36+
{#snippet left()}
37+
<svg class={sizes[size]} fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
38+
<path fill-rule="evenodd" d="M8 4a4 4 0 100 8 4 4 0 000-8zM2 8a6 6 0 1110.89 3.476l4.817 4.817a1 1 0 01-1.414 1.414l-4.816-4.816A6 6 0 012 8z" clip-rule="evenodd" />
39+
</svg>
40+
{/snippet}
41+
{#if right}
42+
{@render right()}
43+
{/if}
44+
</Input>
45+
{#if children}
46+
<div class="flex absolute inset-y-0 end-0 items-center text-gray-500 dark:text-gray-400">
47+
{@render children()}
48+
</div>
49+
{/if}
50+
</div>
51+
{:else}
52+
{#if children}
53+
{@render children()}
54+
{/if}
55+
{/if}

src/lib/forms/search/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import Search from './Search.svelte';
2+
3+
4+
export { Search }

src/lib/forms/search/theme.ts

Whitespace-only changes.

src/lib/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export * from './video';
3636

3737
// forms
3838
export * from './forms/checkbox';
39+
export * from './forms/checkbox-button';
3940
export * from './forms/floating-label-input';
4041
export * from './forms/helper';
4142
export * from './forms/input';
@@ -45,6 +46,7 @@ export * from './forms/select';
4546
export * from './forms/radio';
4647
export * from './forms/radio-button';
4748
export * from './forms/range';
49+
export * from './forms/search';
4850
export * from './forms/textarea';
4951
export * from './forms/toggle';
5052

0 commit comments

Comments
 (0)