Skip to content

Commit 67a4e4f

Browse files
authored
feat: add <checkbox> and <slider> (#1)
* feat: `<checkbox>` & `<slider>` * chore: add test
1 parent 29e78bb commit 67a4e4f

File tree

5 files changed

+72
-2
lines changed

5 files changed

+72
-2
lines changed

packages/model/src/checkbox.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { Ref } from '@vue/reactivity'
2+
import type { Context } from 'sciux-laplace'
3+
4+
import { toValue } from '@vue/reactivity'
5+
import { type } from 'arktype'
6+
import { defineComponent } from 'sciux-laplace'
7+
8+
const T = type({
9+
model: 'string',
10+
disabled: 'boolean',
11+
})
12+
13+
export default defineComponent<'input', typeof T.infer, Context>((attrs, context) => {
14+
const input = document.createElement('input')
15+
input.type = 'checkbox'
16+
if (attrs.model) {
17+
input.addEventListener('input', (e) => {
18+
(context[attrs.model!.value!] as Ref<boolean>).value = (e.target as HTMLInputElement).checked
19+
})
20+
}
21+
return {
22+
name: 'input',
23+
attrs: T,
24+
setup: () => {
25+
input.disabled = toValue(attrs.disabled)
26+
return input
27+
},
28+
}
29+
})

packages/model/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
export { default as button } from './button'
2+
export { default as checkbox } from './checkbox'
23
export { default as input } from './input'
4+
export { default as slider } from './slider'

packages/model/src/slider.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type { Ref } from '@vue/reactivity'
2+
import type { Context } from 'sciux-laplace'
3+
4+
import { toValue } from '@vue/reactivity'
5+
import { type } from 'arktype'
6+
import { defineComponent } from 'sciux-laplace'
7+
8+
const T = type({
9+
model: 'string',
10+
defaultValue: 'number',
11+
disabled: 'boolean',
12+
})
13+
14+
export default defineComponent<'input', typeof T.infer, Context>((attrs, context) => {
15+
const input = document.createElement('input')
16+
input.type = 'range'
17+
input.min = '0'
18+
input.max = '1'
19+
input.step = 'any'
20+
if (attrs.model) {
21+
input.addEventListener('input', (e) => {
22+
(context[attrs.model!.value!] as Ref<number>).value = Number.parseFloat((e.target as HTMLInputElement).value)
23+
})
24+
}
25+
return {
26+
name: 'input',
27+
attrs: T,
28+
setup: () => {
29+
input.defaultValue = toValue(attrs.defaultValue).toString()
30+
input.disabled = toValue(attrs.disabled)
31+
return input
32+
},
33+
}
34+
})

packages/test/src/example.sciux

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
<columns>
22
<let :value="2" />
3+
<let :checked="false" />
34
<flexbox>1</flexbox>
45
<flexbox>{{ value }}</flexbox>
6+
<flexbox>{{ checked }}</flexbox>
57
<rows>
68
<flexbox>1</flexbox>
79
<flexbox>1</flexbox>
810
<flexbox>
911
<input model="value"/>
12+
<checkbox model="checked"/>
1013
</flexbox>
1114
</rows>
12-
</columns>
15+
</columns>

packages/test/src/main.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { align, block, columns, flexbox, grid, rows } from '@sciux/layout'
22
// import { model } from "@sciux/model";
3-
import { button, input } from '@sciux/model'
3+
import { button, checkbox, input, slider } from '@sciux/model'
44
import { components, render } from 'sciux-laplace'
55
import source from './example.sciux?raw'
66

@@ -12,5 +12,7 @@ components.set('grid', grid)
1212
components.set('align', align)
1313
components.set('button', button)
1414
components.set('input', input)
15+
components.set('checkbox', checkbox)
16+
components.set('slider', slider)
1517

1618
render(source, document.getElementById('app')!)

0 commit comments

Comments
 (0)