forked from isaacplmann/sturdy-uis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect.ts
More file actions
49 lines (45 loc) · 954 Bytes
/
select.ts
File metadata and controls
49 lines (45 loc) · 954 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { assign, Machine } from 'xstate';
// The hierarchical (recursive) schema for the states
interface SelectionSchema {
states: {
unselected: {};
selected: {};
};
}
// The events that the machine handles
type SelectionEvents =
| { type: 'select'; selectedIndex: number }
| { type: 'clear' };
// The context (extended state) of the machine
interface SelectionContext {
selectedIndex?: number;
}
export const selectionMachine = Machine<
SelectionContext,
SelectionSchema,
SelectionEvents
>(
{
id: 'Selection',
initial: 'unselected',
context: {},
states: {
unselected: {
on: { select: 'selected' }
},
selected: {
entry: ['setSelectedIndex', 'notifySelection'],
on: {
select: 'selected'
}
}
}
},
{
actions: {
setSelectedIndex: assign((ctx, event: any) => ({
selectedIndex: event.selectedIndex
}))
}
}
);