-
-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathCheckbox.svelte
More file actions
204 lines (179 loc) · 5.02 KB
/
Copy pathCheckbox.svelte
File metadata and controls
204 lines (179 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<script>
/**
* @template [T=any]
* @restProps {div}
* @event {boolean} check
*/
/**
* Specify the value of the checkbox.
* @type {T}
*/
export let value = /** @type {T} */ ("");
/**
* Specify whether the checkbox is checked.
* @bindable writable
*/
export let checked = false;
/**
* Specify the bound group.
* @type {ReadonlyArray<T> | undefined}
* @bindable writable
*/
export let group = undefined;
/**
* Specify whether the checkbox is indeterminate.
* @bindable writable
*/
export let indeterminate = false;
/** Set to `true` to display the skeleton state */
export let skeleton = false;
/** Set to `true` to mark the field as required */
export let required = false;
/** Set to `true` for the checkbox to be read-only */
export let readonly = false;
/** Set to `true` to disable the checkbox */
export let disabled = false;
/** Specify the label text */
export let labelText = "";
/** Set to `true` to visually hide the label text */
export let hideLabel = false;
/** Specify the helper text */
export let helperText = "";
/** Set a name for the input element */
export let name = "";
/**
* Specify the title attribute for the label element.
* @type {string}
* @bindable readonly
*/
export let title = undefined;
/** Set an id for the input label */
export let id = `ccs-${Math.random().toString(36)}`;
/**
* Obtain a reference to the input HTML element.
* @bindable readonly
*/
export let ref = null;
import { createEventDispatcher, getContext } from "svelte";
import { readable } from "svelte/store";
import { overflowTitle } from "../utils/overflowTitle.js";
import CheckboxSkeleton from "./CheckboxSkeleton.svelte";
const dispatch = createEventDispatcher();
const ctx = getContext("carbon:CheckboxGroup");
const {
selectedValues,
groupName,
groupRequired,
readonly: groupReadonly,
update: ctxUpdate,
} = ctx ?? {
selectedValues: readable([]),
groupName: readable(undefined),
groupRequired: readable(undefined),
readonly: readable(false),
};
$: useGroup = !ctx && Array.isArray(group);
$: if (ctx) checked = $selectedValues.includes(value);
$: if (useGroup) checked = group.includes(value);
$: effectiveName = ctx ? ($groupName ?? name) : name;
$: effectiveRequired = ctx ? ($groupRequired ?? required) : required;
$: effectiveReadonly = $groupReadonly || readonly;
// Track previous checked value to avoid duplicate dispatches in Svelte 5
// The reactive statement will only dispatch when checked changes externally (e.g., via bind:checked)
let previousChecked = checked;
$: {
const hasChanged = previousChecked !== checked;
if (hasChanged) {
previousChecked = checked;
dispatch("check", checked);
}
}
let refLabel = null;
$: helperId = `helper-${id}`;
</script>
<!-- svelte-ignore a11y-mouse-events-have-key-events -->
{#if skeleton}
<CheckboxSkeleton
{...$$restProps}
on:click
on:mouseover
on:mouseenter
on:mouseleave
/>
{:else}
<!-- svelte-ignore a11y-no-static-element-interactions -->
<div
class:bx--form-item={true}
class:bx--checkbox-wrapper={true}
class:bx--checkbox-wrapper--readonly={effectiveReadonly}
{...$$restProps}
on:click
on:mouseover
on:mouseenter
on:mouseleave
>
<input
bind:this={ref}
type="checkbox"
{value}
{checked}
{disabled}
{id}
bind:indeterminate
name={effectiveName}
required={effectiveRequired}
aria-readonly={effectiveReadonly || undefined}
aria-describedby={helperText ? helperId : undefined}
class:bx--checkbox={true}
on:click={(event) => {
if (effectiveReadonly) {
event.preventDefault();
}
}}
on:change={(event) => {
if (effectiveReadonly) {
event.stopImmediatePropagation();
return;
}
if (ctxUpdate) {
ctxUpdate(value, !checked);
} else if (useGroup) {
group = group.includes(value)
? group.filter((_value) => _value !== value)
: [...group, value];
} else {
const newChecked = !checked;
previousChecked = newChecked;
checked = newChecked;
// Dispatch directly for user-initiated changes to avoid duplicate events in Svelte 5
dispatch("check", newChecked);
}
}}
on:change
on:focus
on:blur
>
<label
for={id}
use:overflowTitle={{ title, measure: refLabel }}
class:bx--checkbox-label={true}
>
<span
bind:this={refLabel}
class:bx--checkbox-label-text={true}
class:bx--visually-hidden={hideLabel}
>
<slot name="labelChildren"> {labelText} </slot>
</span>
</label>
{#if helperText}
<div
id={helperId}
class:bx--form__helper-text={true}
class:bx--form__helper-text--disabled={disabled}
>
{helperText}
</div>
{/if}
</div>
{/if}