|
1 | | ---- |
2 | | -import { uniqueId } from "./uniqueid"; |
3 | | -import Field from "./Field.astro"; |
4 | | -
|
5 | | -export type Props = { |
6 | | - name: string; |
7 | | - defaultValue: string; |
8 | | - options: string[]; |
9 | | -}; |
10 | | -
|
11 | | -const cid = uniqueId("EnumField-"); |
12 | | -
|
13 | | -const { name = "", defaultValue = false, options = [] } = Astro.props; |
14 | | ---- |
15 | | - |
16 | | -<Field cid={cid} name={name} fieldType="EnumField"> |
17 | | - <select id={cid} name={name}> |
18 | | - { |
19 | | - options.map((o) => ( |
20 | | - <option value={o} selected={o === defaultValue}> |
21 | | - {o} |
22 | | - </option> |
23 | | - )) |
24 | | - } |
25 | | - </select> |
26 | | -</Field> |
27 | | - |
28 | | -<style> |
29 | | - select { |
30 | | - border: 1px solid var(--sl-color-gray-5); |
31 | | - font-family: var(--sl-font); |
32 | | - font-size: var(--sl-text-sm); |
33 | | - } |
34 | | -</style> |
35 | | - |
36 | | -<script> |
37 | | - function selectChange(ev: Event) { |
38 | | - const field = (ev.target as HTMLOptionElement).closest<HTMLDivElement>( |
39 | | - '[data-astro-component="EnumField"]' |
40 | | - )!; |
41 | | - |
42 | | - updateFieldTarget(field); |
43 | | - } |
44 | | - |
45 | | - function updateFieldTarget(field: HTMLDivElement) { |
46 | | - const root = field.closest<HTMLFieldSetElement>( |
47 | | - '[data-astro-component="Controls"]' |
48 | | - ); |
49 | | - const select = field.querySelector("select")!; |
50 | | - |
51 | | - if (root) { |
52 | | - const target = root.dataset.target!; |
53 | | - const targetEl = document.querySelector(target); |
54 | | - |
55 | | - if (targetEl) { |
56 | | - targetEl[select.name] = select.value; |
57 | | - } |
58 | | - } |
59 | | - } |
60 | | - |
61 | | - document |
62 | | - .querySelectorAll<HTMLDivElement>('[data-astro-component="EnumField"]') |
63 | | - .forEach((el) => { |
64 | | - const select = el.querySelector("select"); |
65 | | - select?.addEventListener("change", selectChange); |
66 | | - updateFieldTarget(el); |
67 | | - }); |
68 | | -</script> |
| 1 | +--- |
| 2 | +import { uniqueId } from "./uniqueid"; |
| 3 | +import Field from "./Field.astro"; |
| 4 | +
|
| 5 | +export type Props = { |
| 6 | + name: string; |
| 7 | + defaultValue: string; |
| 8 | + options: string[]; |
| 9 | +}; |
| 10 | +
|
| 11 | +const cid = uniqueId("EnumField-"); |
| 12 | +
|
| 13 | +const { name = "", defaultValue = false, options = [] } = Astro.props; |
| 14 | +--- |
| 15 | + |
| 16 | +<Field cid={cid} name={name} fieldType="EnumField"> |
| 17 | + <select id={cid} name={name}> |
| 18 | + { |
| 19 | + options.map((o) => ( |
| 20 | + <option value={o} selected={o === defaultValue}> |
| 21 | + {o} |
| 22 | + </option> |
| 23 | + )) |
| 24 | + } |
| 25 | + </select> |
| 26 | +</Field> |
| 27 | + |
| 28 | +<style> |
| 29 | + select { |
| 30 | + background-color: var(--vscode-settings-dropdownBackground); |
| 31 | + border: 1px solid var(--vscode-settings-dropdownBorder); |
| 32 | + color: var(--vscode-foreground); |
| 33 | + font-family: var(--vscode-font-family); |
| 34 | + font-size: var(--vscode-font-size); |
| 35 | + font-weight: var(--vscode-font-weight); |
| 36 | + line-height: 18px; |
| 37 | + padding: 3px 4px; |
| 38 | + } |
| 39 | +</style> |
| 40 | + |
| 41 | +<script> |
| 42 | + function selectChange(ev: Event) { |
| 43 | + const field = (ev.target as HTMLOptionElement).closest<HTMLDivElement>( |
| 44 | + '[data-astro-component="EnumField"]' |
| 45 | + )!; |
| 46 | + |
| 47 | + updateFieldTarget(field); |
| 48 | + } |
| 49 | + |
| 50 | + function updateFieldTarget(field: HTMLDivElement) { |
| 51 | + const root = field.closest<HTMLFieldSetElement>( |
| 52 | + '[data-astro-component="Controls"]' |
| 53 | + ); |
| 54 | + const select = field.querySelector("select")!; |
| 55 | + |
| 56 | + if (root) { |
| 57 | + const target = root.dataset.target!; |
| 58 | + const targetEl = document.querySelector(target); |
| 59 | + |
| 60 | + if (targetEl) { |
| 61 | + targetEl[select.name] = select.value; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + document |
| 67 | + .querySelectorAll<HTMLDivElement>('[data-astro-component="EnumField"]') |
| 68 | + .forEach((el) => { |
| 69 | + const select = el.querySelector("select"); |
| 70 | + select?.addEventListener("change", selectChange); |
| 71 | + updateFieldTarget(el); |
| 72 | + }); |
| 73 | +</script> |
0 commit comments