Skip to content

Commit 53857ef

Browse files
committed
refactor: machines and solid
1 parent cff8dfc commit 53857ef

24 files changed

+169
-112
lines changed

examples/solid-ts/src/components/controls.tsx

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,47 @@ import type { UseControlsReturn } from "~/hooks/use-controls"
44

55
export function Controls({ store }: { store: UseControlsReturn }) {
66
const { config, state, setState } = store
7+
const items = createMemo(() => {
8+
const keys = Object.keys(store.config)
9+
return keys.map((key) => {
10+
const { type, label = key, options, placeholder, min, max } = (config[key] ?? {}) as any
11+
const value = createMemo(() => deepGet(state(), key))
12+
return { key, type, label, options, placeholder, min, max, value }
13+
})
14+
})
15+
716
return (
817
<div class="controls-container">
9-
<For each={Object.keys(store.config)}>
10-
{(key) => {
11-
const { type, label = key, options, placeholder, min, max } = (config[key] ?? {}) as any
12-
const value = createMemo(() => deepGet(state(), key))
13-
switch (type) {
18+
<For each={items()}>
19+
{(item) => {
20+
switch (item.type) {
1421
case "boolean":
1522
return (
1623
<div class="checkbox">
1724
<input
18-
data-testid={key}
19-
id={label}
25+
data-testid={item.key}
26+
id={item.label}
2027
type="checkbox"
21-
checked={value()}
28+
checked={item.value()}
2229
onChange={(e) => {
23-
setState(key, e.currentTarget.checked)
30+
setState(item.key, e.currentTarget.checked)
2431
}}
2532
/>
26-
<label for={label}>{label}</label>
33+
<label for={item.label}>{item.label}</label>
2734
</div>
2835
)
2936
case "string":
3037
return (
3138
<div class="text">
32-
<label style={{ "margin-right": "10px" }}>{label}</label>
39+
<label style={{ "margin-right": "10px" }}>{item.label}</label>
3340
<input
34-
data-testid={key}
41+
data-testid={item.key}
3542
type="text"
36-
placeholder={placeholder}
37-
value={value()}
43+
placeholder={item.placeholder}
44+
value={item.value()}
3845
onKeyDown={(e) => {
3946
if (e.key === "Enter") {
40-
setState(key, e.currentTarget.value)
47+
setState(item.key, e.currentTarget.value)
4148
}
4249
}}
4350
/>
@@ -46,39 +53,39 @@ export function Controls({ store }: { store: UseControlsReturn }) {
4653
case "select":
4754
return (
4855
<div class="text">
49-
<label for={label} style={{ "margin-right": "10px" }}>
50-
{label}
56+
<label for={item.label} style={{ "margin-right": "10px" }}>
57+
{item.label}
5158
</label>
5259
<select
53-
data-testid={key}
54-
id={label}
55-
value={value()}
60+
data-testid={item.key}
61+
id={item.label}
62+
value={item.value()}
5663
onChange={(e) => {
57-
setState(key, e.currentTarget.value)
64+
setState(item.key, e.currentTarget.value)
5865
}}
5966
>
6067
<option>-----</option>
61-
<For each={options as any[]}>{(option) => <option value={option}>{option}</option>}</For>
68+
<For each={item.options as any[]}>{(option) => <option value={option}>{option}</option>}</For>
6269
</select>
6370
</div>
6471
)
6572
case "number":
6673
return (
6774
<div class="text">
68-
<label for={label} style={{ "margin-right": "10px" }}>
69-
{label}
75+
<label for={item.label} style={{ "margin-right": "10px" }}>
76+
{item.label}
7077
</label>
7178
<input
72-
data-testid={key}
73-
id={label}
79+
data-testid={item.key}
80+
id={item.label}
7481
type="number"
75-
min={min}
76-
max={max}
77-
value={value()}
82+
min={item.min}
83+
max={item.max}
84+
value={item.value()}
7885
onKeyDown={(e) => {
7986
if (e.key === "Enter") {
8087
const val = parseFloat(e.currentTarget.value)
81-
setState(key, isNaN(val) ? 0 : val)
88+
setState(item.key, isNaN(val) ? 0 : val)
8289
}
8390
}}
8491
/>

examples/solid-ts/src/routes/number-input.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { useControls } from "~/hooks/use-controls"
99
export default function Page() {
1010
const controls = useControls(numberInputControls)
1111

12-
const service = useMachine(numberInput.machine, { id: createUniqueId() })
12+
const service = useMachine(numberInput.machine, controls.mergeProps({ id: createUniqueId() }))
1313

1414
const api = createMemo(() => numberInput.connect(service, normalizeProps))
1515

examples/solid-ts/src/routes/qr-code.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ import { useControls } from "../hooks/use-controls"
99
export default function Page() {
1010
const controls = useControls(qrCodeControls)
1111

12-
const service = useMachine(qrCode.machine, {
13-
id: createUniqueId(),
14-
encoding: { ecc: "H" },
15-
})
12+
const service = useMachine(
13+
qrCode.machine,
14+
controls.mergeProps<qrCode.Props>({
15+
id: createUniqueId(),
16+
encoding: { ecc: "H" },
17+
}),
18+
)
1619

1720
const api = createMemo(() => qrCode.connect(service, normalizeProps))
1821

examples/solid-ts/src/routes/radio-group.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ import { useControls } from "~/hooks/use-controls"
1010
export default function Page() {
1111
const controls = useControls(radioControls)
1212

13-
const service = useMachine(radio.machine, {
14-
name: "fruits",
15-
id: createUniqueId(),
16-
})
13+
const service = useMachine(
14+
radio.machine,
15+
controls.mergeProps<radio.Props>({
16+
name: "fruits",
17+
id: createUniqueId(),
18+
}),
19+
)
1720

1821
const api = createMemo(() => radio.connect(service, normalizeProps))
1922

examples/solid-ts/src/routes/rating-group.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ function Star() {
3939
export default function Page() {
4040
const controls = useControls(ratingControls)
4141

42-
const service = useMachine(rating.machine, {
43-
id: createUniqueId(),
44-
defaultValue: 2.5,
45-
})
42+
const service = useMachine(
43+
rating.machine,
44+
controls.mergeProps<rating.Props>({
45+
id: createUniqueId(),
46+
defaultValue: 2.5,
47+
}),
48+
)
4649

4750
const api = createMemo(() => rating.connect(service, normalizeProps))
4851

examples/solid-ts/src/routes/segment-control.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ import { useControls } from "~/hooks/use-controls"
99
export default function Page() {
1010
const controls = useControls(radioControls)
1111

12-
const service = useMachine(radio.machine, {
13-
id: createUniqueId(),
14-
name: "fruit",
15-
orientation: "horizontal",
16-
})
12+
const service = useMachine(
13+
radio.machine,
14+
controls.mergeProps<radio.Props>({
15+
id: createUniqueId(),
16+
name: "fruit",
17+
orientation: "horizontal",
18+
}),
19+
)
1720

1821
const api = createMemo(() => radio.connect(service, normalizeProps))
1922

examples/solid-ts/src/routes/select.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ import { useControls } from "~/hooks/use-controls"
1111
export default function Page() {
1212
const controls = useControls(selectControls)
1313

14-
const service = useMachine(select.machine, {
15-
collection: select.collection({ items: selectData }),
16-
id: createUniqueId(),
17-
})
14+
const service = useMachine(
15+
select.machine,
16+
controls.mergeProps<select.Props>(() => ({
17+
collection: select.collection({ items: selectData }),
18+
id: createUniqueId(),
19+
})),
20+
)
1821

1922
const api = createMemo(() => select.connect(service, normalizeProps))
2023

examples/solid-ts/src/routes/signature-pad.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ export default function Page() {
1212

1313
const controls = useControls(signaturePadControls)
1414

15-
const service = useMachine(signaturePad.machine, {
16-
id: createUniqueId(),
17-
onDrawEnd(details) {
18-
details.getDataUrl("image/png").then(setUrl)
19-
},
20-
})
15+
const service = useMachine(
16+
signaturePad.machine,
17+
controls.mergeProps<signaturePad.Props>({
18+
id: createUniqueId(),
19+
onDrawEnd(details) {
20+
details.getDataUrl("image/png").then(setUrl)
21+
},
22+
}),
23+
)
2124

2225
const api = createMemo(() => signaturePad.connect(service, normalizeProps))
2326

examples/solid-ts/src/routes/slider.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ import { useControls } from "~/hooks/use-controls"
1010
export default function Page() {
1111
const controls = useControls(sliderControls)
1212

13-
const service = useMachine(slider.machine, {
14-
id: createUniqueId(),
15-
defaultValue: [0],
16-
})
13+
const service = useMachine(
14+
slider.machine,
15+
controls.mergeProps<slider.Props>({
16+
id: createUniqueId(),
17+
defaultValue: [0],
18+
}),
19+
)
1720

1821
const api = createMemo(() => slider.connect(service, normalizeProps))
1922

examples/solid-ts/src/routes/splitter.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ import { useControls } from "~/hooks/use-controls"
99
export default function Page() {
1010
const controls = useControls(splitterControls)
1111

12-
const service = useMachine(splitter.machine, {
13-
id: createUniqueId(),
14-
defaultSize: [
15-
{ id: "a", size: 50 },
16-
{ id: "b", size: 50 },
17-
],
18-
})
12+
const service = useMachine(
13+
splitter.machine,
14+
controls.mergeProps<splitter.Props>({
15+
id: createUniqueId(),
16+
defaultSize: [
17+
{ id: "a", size: 50 },
18+
{ id: "b", size: 50 },
19+
],
20+
}),
21+
)
1922

2023
const api = createMemo(() => splitter.connect(service, normalizeProps))
2124

0 commit comments

Comments
 (0)