|
| 1 | +import { useDisclosureData } from 'src'; |
| 2 | + |
| 3 | +import { For, Show } from 'solid-js'; |
| 4 | +import { useMDXComponents } from 'solid-jsx'; |
| 5 | +import { ExampleBase } from '../example-base'; |
| 6 | +import Code from './use-disclosure-data.code.mdx'; |
| 7 | + |
| 8 | +export function UseDisclosureDataExample() { |
| 9 | + const items = [ |
| 10 | + { id: '1', title: 'Item 1' }, |
| 11 | + { id: '2', title: 'Item 2' }, |
| 12 | + ]; |
| 13 | + |
| 14 | + // If you have multiple modals, |
| 15 | + // I recommend prefixing `data`, `open`, `handlers` with the same name. |
| 16 | + // i.e. editModalData, editModalOpen, editModalHandlers |
| 17 | + const [data, open, handlers] = useDisclosureData<(typeof items)[number]>(null); |
| 18 | + |
| 19 | + // @ts-ignore |
| 20 | + const components: any = useMDXComponents(); |
| 21 | + return ( |
| 22 | + <ExampleBase |
| 23 | + title="useDisclosureData" |
| 24 | + description="For complex usecases where we only need to pass data to the disclosure. I.e. Edit Modals, Confirm Alerts with details about the item clicked, etc." |
| 25 | + code={<Code components={components} />} |
| 26 | + > |
| 27 | + <div class="flex h-full w-full items-center justify-center gap-x-1 rounded-md border p-3 py-10 text-center text-sm"> |
| 28 | + <ul class="flex flex-col gap-2"> |
| 29 | + <For each={items}> |
| 30 | + {item => ( |
| 31 | + <li> |
| 32 | + <button |
| 33 | + onClick={() => { |
| 34 | + handlers.open(item); |
| 35 | + }} |
| 36 | + class="rounded bg-blue-500 px-4 py-2 text-white" |
| 37 | + > |
| 38 | + Open Dialog for {item.title} |
| 39 | + </button> |
| 40 | + </li> |
| 41 | + )} |
| 42 | + </For> |
| 43 | + </ul> |
| 44 | + |
| 45 | + <Show when={open()}> |
| 46 | + <div class="fixed inset-0 flex items-center justify-center bg-black bg-opacity-50"> |
| 47 | + <div class="rounded-lg bg-white p-6 shadow-lg"> |
| 48 | + <h2 class="mb-4 text-lg font-bold">{data()?.title || 'Dialog Title'}</h2> |
| 49 | + <p class="mb-4"> |
| 50 | + This is an example dialog using useDisclosure and data: {data()?.id} |
| 51 | + </p> |
| 52 | + <button onClick={handlers.close} class="rounded bg-gray-500 px-4 py-2 text-white"> |
| 53 | + Close |
| 54 | + </button> |
| 55 | + </div> |
| 56 | + </div> |
| 57 | + </Show> |
| 58 | + </div> |
| 59 | + </ExampleBase> |
| 60 | + ); |
| 61 | +} |
0 commit comments