|
1 | 1 | import { Component, createSignal } from "solid-js";
|
| 2 | +import { Match } from "../src/index.js" |
| 3 | + |
| 4 | +type AnimalDog = {kind: 'dog', breed: string}; |
| 5 | +type AnimalCat = {kind: 'cat', lives: number}; |
| 6 | +type AnimalBird = {kind: 'bird', canFly: boolean}; |
| 7 | + |
| 8 | +type Animal = AnimalDog | AnimalCat | AnimalBird; |
| 9 | + |
| 10 | +const DogDisplay: Component<{ animal: AnimalDog }> = (props) => ( |
| 11 | + <div class="text-center"> |
| 12 | + <div class="text-2xl mb-2">🐕</div> |
| 13 | + <div class="text-sm">Breed: {props.animal.breed}</div> |
| 14 | + </div> |
| 15 | +); |
| 16 | + |
| 17 | +const CatDisplay: Component<{ animal: AnimalCat }> = (props) => ( |
| 18 | + <div class="text-center"> |
| 19 | + <div class="text-2xl mb-2">🐱</div> |
| 20 | + <div class="text-sm">Lives: {props.animal.lives}</div> |
| 21 | + </div> |
| 22 | +); |
| 23 | + |
| 24 | +const BirdDisplay: Component<{ animal: AnimalBird }> = (props) => ( |
| 25 | + <div class="text-center"> |
| 26 | + <div class="text-2xl mb-2">🐦</div> |
| 27 | + <div class="text-sm"> |
| 28 | + {props.animal.canFly ? 'Can fly' : 'Cannot fly'} |
| 29 | + </div> |
| 30 | + </div> |
| 31 | +); |
| 32 | + |
| 33 | +const FallbackDisplay: Component = () => ( |
| 34 | + <div class="text-center"> |
| 35 | + <div class="text-2xl mb-2">❓</div> |
| 36 | + <div>Fallback content</div> |
| 37 | + </div> |
| 38 | +); |
2 | 39 |
|
3 | 40 | const App: Component = () => {
|
4 |
| - const [count, setCount] = createSignal(0); |
5 |
| - const increment = () => setCount(count() + 1); |
| 41 | + const [animal, setAnimal] = createSignal<Animal | null>(null); |
| 42 | + |
| 43 | + const animals: (Animal | null)[] = [ |
| 44 | + null, |
| 45 | + { kind: 'dog', breed: 'Golden Retriever' }, |
| 46 | + { kind: 'cat', lives: 9 }, |
| 47 | + { kind: 'bird', canFly: true }, |
| 48 | + ]; |
6 | 49 |
|
7 | 50 | return (
|
8 |
| - <div class="box-border flex min-h-screen w-full flex-col items-center justify-center space-y-4 bg-gray-800 p-24 text-white"> |
9 |
| - <div class="wrapper-v"> |
10 |
| - <h4>Counter component</h4> |
11 |
| - <p class="caption">it's very important...</p> |
12 |
| - <button class="btn" onClick={increment}> |
13 |
| - {count()} |
14 |
| - </button> |
| 51 | + <div class="min-h-screen p-8"> |
| 52 | + <div class="max-w-4xl mx-auto space-y-8"> |
| 53 | + <div class="text-center"> |
| 54 | + <h1 class="text-3xl font-bold mb-2">Match Component Demo</h1> |
| 55 | + <p>Control-flow component for matching discriminated union members</p> |
| 56 | + </div> |
| 57 | + |
| 58 | + <div class="border border-gray-500 border-2 rounded-lg p-6"> |
| 59 | + <label class="block text-sm font-medium mb-2"> |
| 60 | + Select an animal: |
| 61 | + </label> |
| 62 | + <select |
| 63 | + class="w-full p-2 border border-gray-500 border-2 rounded-md" |
| 64 | + onChange={e => {setAnimal(animals[parseInt(e.target.value)]!)}} |
| 65 | + > |
| 66 | + <option value="0">None (null)</option> |
| 67 | + <option value="1">Dog</option> |
| 68 | + <option value="2">Cat</option> |
| 69 | + <option value="3">Bird</option> |
| 70 | + </select> |
| 71 | + </div> |
| 72 | + |
| 73 | + <div class="grid grid-cols-1 md:grid-cols-2 gap-8"> |
| 74 | + <div class="border border-gray-500 border-2 rounded-lg p-6"> |
| 75 | + <h2 class="text-xl font-semibold mb-1">Complete Match</h2> |
| 76 | + <p class="text-sm mb-4">Handles all union members with fallback</p> |
| 77 | + <div class="border border-gray-500 border-2 rounded p-4 min-h-[100px] flex items-center justify-center"> |
| 78 | + <Match on={animal()} case={{ |
| 79 | + dog: v => <DogDisplay animal={v()} />, |
| 80 | + cat: v => <CatDisplay animal={v()} />, |
| 81 | + bird: v => <BirdDisplay animal={v()} />, |
| 82 | + }} fallback={<FallbackDisplay />} /> |
| 83 | + </div> |
| 84 | + </div> |
| 85 | + |
| 86 | + <div class="border border-gray-500 border-2 rounded-lg p-6"> |
| 87 | + <h2 class="text-xl font-semibold mb-1">Partial Match</h2> |
| 88 | + <p class="text-sm mb-4">Only handles dogs and cats</p> |
| 89 | + <div class="border border-gray-500 border-2 rounded p-4 min-h-[100px] flex items-center justify-center"> |
| 90 | + <Match partial on={animal()} case={{ |
| 91 | + dog: v => <DogDisplay animal={v()} />, |
| 92 | + cat: v => <CatDisplay animal={v()} />, |
| 93 | + }} fallback={<FallbackDisplay />} /> |
| 94 | + </div> |
| 95 | + </div> |
| 96 | + </div> |
15 | 97 | </div>
|
16 | 98 | </div>
|
17 | 99 | );
|
|
0 commit comments