Skip to content

Commit 9322959

Browse files
authored
feat: add <select> and <option> (#7)
1 parent 3df5a3e commit 9322959

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

packages/model/src/select.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,51 @@
1+
import type { Ref } from '@vue/reactivity'
2+
import type { Context } from 'sciux-laplace'
3+
import { toValue } from '@vue/reactivity'
14
import { type } from 'arktype'
5+
import { defineComponent } from 'sciux-laplace'
26

37
export const SelectType = type({
48
model: 'string',
9+
disabled: 'boolean',
510
})
611

712
export const SelectOptionType = type({
813
value: 'string',
914
selected: 'boolean',
1015
disabled: 'boolean',
1116
})
17+
18+
export const selectComponent = defineComponent<'select', typeof SelectType.infer, Context>((attrs, context) => {
19+
const select = document.createElement('select')
20+
if (attrs.model) {
21+
select.addEventListener('select', (e) => {
22+
const target = e.target as HTMLSelectElement
23+
(context[attrs.model!.value!] as Ref<string>).value = target.value
24+
})
25+
}
26+
return {
27+
name: 'select',
28+
attrs: SelectType,
29+
setup: (children) => {
30+
select.disabled = toValue(attrs.disabled)
31+
for (const child of children()) {
32+
select.appendChild(child)
33+
}
34+
return select
35+
},
36+
}
37+
})
38+
39+
export const selectOptionComponent = defineComponent<'option', typeof SelectOptionType.infer, Context>((attrs, _context) => {
40+
const option = document.createElement('option')
41+
return {
42+
name: 'option',
43+
attrs: SelectOptionType,
44+
setup: () => {
45+
option.value = toValue(attrs.value)
46+
option.selected = toValue(attrs.selected)
47+
option.disabled = toValue(attrs.disabled)
48+
return option
49+
},
50+
}
51+
})

0 commit comments

Comments
 (0)