|
1 | | ---- |
2 | | -import { uniqueId } from "./uniqueid"; |
3 | | -import Field from "./Field.astro"; |
4 | | -
|
5 | | -export type Props = { |
6 | | - name: string; |
7 | | - defaultValue: boolean; |
8 | | -}; |
9 | | -
|
10 | | -const cid = uniqueId("BooleanField-"); |
11 | | -
|
12 | | -const { name = "", defaultValue = false } = Astro.props; |
13 | | ---- |
14 | | - |
15 | | -<Field cid={cid} name={name} fieldType="BooleanField"> |
16 | | - <input type="checkbox" id={cid} name={name} checked={defaultValue} /> |
17 | | -</Field> |
18 | | - |
19 | | -<style> |
20 | | - input { |
21 | | - border: 1px solid var(--sl-color-gray-5); |
22 | | - } |
23 | | -</style> |
24 | | - |
25 | | -<script> |
26 | | - function checkboxChange(ev: Event) { |
27 | | - const field = (ev.target as HTMLInputElement).closest<HTMLDivElement>( |
28 | | - '[data-astro-component="BooleanField"]' |
29 | | - )!; |
30 | | - |
31 | | - updateFieldTarget(field); |
32 | | - } |
33 | | - |
34 | | - function updateFieldTarget(field: HTMLDivElement) { |
35 | | - const root = field.closest<HTMLFieldSetElement>( |
36 | | - '[data-astro-component="Controls"]' |
37 | | - ); |
38 | | - const cb = field.querySelector("input")!; |
39 | | - |
40 | | - if (root) { |
41 | | - const target = root.dataset.target!; |
42 | | - const targetEl = document.querySelector(target); |
43 | | - |
44 | | - if (targetEl) { |
45 | | - targetEl[cb.name] = cb.checked; |
46 | | - } |
47 | | - } |
48 | | - } |
49 | | - |
50 | | - document |
51 | | - .querySelectorAll<HTMLDivElement>('[data-astro-component="BooleanField"]') |
52 | | - .forEach((el) => { |
53 | | - const cb = el.querySelector("input"); |
54 | | - cb?.addEventListener("change", checkboxChange); |
55 | | - updateFieldTarget(el); |
56 | | - }); |
57 | | -</script> |
| 1 | +--- |
| 2 | +import { uniqueId } from "./uniqueid"; |
| 3 | +import Field from "./Field.astro"; |
| 4 | +
|
| 5 | +export type Props = { |
| 6 | + name: string; |
| 7 | + defaultValue: boolean; |
| 8 | +}; |
| 9 | +
|
| 10 | +const cid = uniqueId("BooleanField-"); |
| 11 | +
|
| 12 | +const { name = "", defaultValue = false } = Astro.props; |
| 13 | +--- |
| 14 | + |
| 15 | +<Field cid={cid} name={name} fieldType="BooleanField"> |
| 16 | + <input type="checkbox" id={cid} name={name} checked={defaultValue} /> |
| 17 | +</Field> |
| 18 | + |
| 19 | +<style> |
| 20 | + input { |
| 21 | + border: 1px solid var(--sl-color-gray-5); |
| 22 | + } |
| 23 | +</style> |
| 24 | + |
| 25 | +<script> |
| 26 | + function checkboxChange(ev: Event) { |
| 27 | + const field = (ev.target as HTMLInputElement).closest<HTMLDivElement>( |
| 28 | + '[data-astro-component="BooleanField"]' |
| 29 | + )!; |
| 30 | + |
| 31 | + updateFieldTarget(field); |
| 32 | + } |
| 33 | + |
| 34 | + function updateFieldTarget(field: HTMLDivElement) { |
| 35 | + const root = field.closest<HTMLFieldSetElement>( |
| 36 | + '[data-astro-component="Controls"]' |
| 37 | + ); |
| 38 | + const cb = field.querySelector("input")!; |
| 39 | + |
| 40 | + if (root) { |
| 41 | + const target = root.dataset.target!; |
| 42 | + const targetEl = document.querySelector(target); |
| 43 | + |
| 44 | + if (targetEl) { |
| 45 | + // @ts-ignore |
| 46 | + targetEl[cb.name] = cb.checked; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + document |
| 52 | + .querySelectorAll<HTMLDivElement>('[data-astro-component="BooleanField"]') |
| 53 | + .forEach((el) => { |
| 54 | + const cb = el.querySelector("input"); |
| 55 | + cb?.addEventListener("change", checkboxChange); |
| 56 | + updateFieldTarget(el); |
| 57 | + }); |
| 58 | +</script> |
0 commit comments