Skip to content

Commit a9220f0

Browse files
authored
Merge pull request #32 from koordinates/improve-multi-slots
2 parents 54e1654 + d2e17de commit a9220f0

File tree

7 files changed

+17
-16
lines changed

7 files changed

+17
-16
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ Slots are how invoked/spawned children of the machine are supplied to the Machin
164164

165165
Slotted machines are determined based on the id of the invoked/spawned machine. There are two types of slots, single and multi. Single slots are for invoked machines, where there will only be a single machine per slot. Multi slots are for spawned machines where there are multiple children per slot, rendered as a group; think lists. There is a set of helper functions for creating slots which in turn can be used to get the id for the slot.
166166

167-
`singleSlot` accepts the name of the slot (must not end in `s`) as the first argument and returns an object with a method `getId()` that returns the id of the slot.
168-
`multiSlot` accepts the name of the slot (must end in `s`) and returns an object with a method `getId(id: string)` that returns the id of the slot
167+
`singleSlot` accepts the name of the slot as the first argument and returns an object with a method `getId()` that returns the id of the slot.
168+
`multiSlot` accepts the name of the slot and returns an object with a method `getId(id: string)` that returns the id of the slot
169169

170170
You should always use the `getId` methods when invoking/spawning something into a slot. Each slot the machine has must be represented by a call to `singleSlot` or `multiSlot` and stored into an array of slots. These slots must be passed to the `buildView` and `buildXStateTreeMachine` functions.
171171

examples/todomvc/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ const view = buildView(
183183
<label htmlFor="toggle-all">Mark all as complete</label>
184184

185185
<ul className="todo-list">
186-
<slots.Todos />
186+
<slots.TodosMulti />
187187
</ul>
188188
</section>
189189

src/slots/slots.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ describe("slot utilities", () => {
55
it("returns an object that allows you to generate slot ids given an id", () => {
66
const slot = multiSlot("Names");
77

8-
expect(slot.getId("id")).toEqual("id-names-slots");
8+
expect(slot.getId("id")).toEqual("id-namesmulti-slots");
99
});
1010
});
1111

@@ -26,8 +26,8 @@ describe("slot utilities", () => {
2626
// should be "foo" | "bar"
2727
type Foo = GetSlotNames<typeof slots>;
2828

29-
const fooTest: "foo" = "foo" as const;
30-
const barTest: "bar" = "bar" as const;
29+
const fooTest = "fooMulti" as const;
30+
const barTest = "bar" as const;
3131
const invalidTest: "invalid" = "invalid" as const;
3232
const _fooOtherTest: Foo = fooTest;
3333
const _barOtherTest: Foo = barTest;

src/slots/slots.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export enum SlotType {
99
/**
1010
* @public
1111
*/
12-
export type SingleSlot<T> = {
12+
export type SingleSlot<T extends string> = {
1313
type: SlotType.SingleSlot;
1414
name: T;
1515
getId(): string;
@@ -18,9 +18,9 @@ export type SingleSlot<T> = {
1818
/**
1919
* @public
2020
*/
21-
export type MultiSlot<T> = {
21+
export type MultiSlot<T extends string> = {
2222
type: SlotType.MultiSlot;
23-
name: T;
23+
name: `${T}Multi`;
2424
getId(id: string): string;
2525
};
2626

@@ -46,8 +46,8 @@ export function singleSlot<T extends string>(name: T): SingleSlot<T> {
4646
export function multiSlot<T extends string>(name: T): MultiSlot<T> {
4747
return {
4848
type: SlotType.MultiSlot,
49-
name,
50-
getId: (id: string) => `${id}-${name.toLowerCase()}-slots`,
49+
name: `${name}Multi`,
50+
getId: (id: string) => `${id}-${name.toLowerCase()}multi-slots`,
5151
};
5252
}
5353

src/test-app/TodosMachine.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ const TodosView = buildView(
252252
/>
253253
<label htmlFor="toggle-all">Mark all as complete</label>
254254
<ul className="todo-list">
255-
<slots.Todos />
255+
<slots.TodosMulti />
256256
</ul>
257257
</section>
258258
<footer className="footer">

src/xstateTree.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ function useSlots<TSlots extends readonly Slot[]>(
127127
// eslint-disable-next-line react-hooks/rules-of-hooks
128128
const [__, children] = useService(interpreter);
129129

130-
if (slot.toString().endsWith("s")) {
130+
console.log(slot.toString());
131+
if (slot.toString().endsWith("Multi")) {
131132
const MultiView = getMultiSlotViewForChildren(
132133
interpreter,
133134
slot.toLowerCase()

xstate-tree.api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ export type Meta<T> = T extends {
195195
} ? TMeta : undefined;
196196

197197
// @public (undocumented)
198-
export type MultiSlot<T> = {
198+
export type MultiSlot<T extends string> = {
199199
type: SlotType.MultiSlot;
200-
name: T;
200+
name: `${T}Multi`;
201201
getId(id: string): string;
202202
};
203203

@@ -313,7 +313,7 @@ export type SharedMeta = {
313313
};
314314

315315
// @public (undocumented)
316-
export type SingleSlot<T> = {
316+
export type SingleSlot<T extends string> = {
317317
type: SlotType.SingleSlot;
318318
name: T;
319319
getId(): string;

0 commit comments

Comments
 (0)