forked from MapComplete/MapComplete
		
	TabbedGroup now supports more tabs
This commit is contained in:
		
							parent
							
								
									5440dfb516
								
							
						
					
					
						commit
						2df9aa8564
					
				
					 1 changed files with 86 additions and 50 deletions
				
			
		|  | @ -1,32 +1,34 @@ | |||
| <script lang="ts"> | ||||
|     /** | ||||
|      * Thin wrapper around 'TabGroup' which binds the state | ||||
|      */ | ||||
|   /** | ||||
|    * Thin wrapper around 'TabGroup' which binds the state | ||||
|    */ | ||||
| 
 | ||||
|     import { Tab, TabGroup, TabList, TabPanel, TabPanels } from "@rgossiaux/svelte-headlessui"; | ||||
|     import { ImmutableStore, Store, UIEventSource } from "../../Logic/UIEventSource"; | ||||
|     import { twJoin } from "tailwind-merge"; | ||||
|   import { Tab, TabGroup, TabList, TabPanel, TabPanels } from "@rgossiaux/svelte-headlessui"; | ||||
|   import { ImmutableStore, Store, UIEventSource } from "../../Logic/UIEventSource"; | ||||
|   import { twJoin } from "tailwind-merge"; | ||||
| 
 | ||||
|     /** | ||||
|      * If a condition is given for a certain tab, it will only be shown if this condition is true. | ||||
|      * E.g. | ||||
|      * condition3 = new ImmutableStore(false) will always hide tab3 (the fourth tab) | ||||
|      */ | ||||
|     let tr = new ImmutableStore(true) | ||||
|     export let condition0: Store<boolean> = tr | ||||
|     export let condition1: Store<boolean> = tr | ||||
|     export let condition2: Store<boolean> = tr | ||||
|     export let condition3: Store<boolean> = tr | ||||
|     export let condition4: Store<boolean> = tr | ||||
|      | ||||
|     export let tab: UIEventSource<number> = new UIEventSource<number>(0); | ||||
|     let tabElements: HTMLElement[] = []; | ||||
|     $: tabElements[$tab]?.click(); | ||||
|     $: { | ||||
|         if (tabElements[tab.data]) { | ||||
|             window.setTimeout(() => tabElements[tab.data].click(), 50); | ||||
|         } | ||||
|   /** | ||||
|    * If a condition is given for a certain tab, it will only be shown if this condition is true. | ||||
|    * E.g. | ||||
|    * condition3 = new ImmutableStore(false) will always hide tab3 (the fourth tab) | ||||
|    */ | ||||
|   let tr = new ImmutableStore(true); | ||||
|   export let condition0: Store<boolean> = tr; | ||||
|   export let condition1: Store<boolean> = tr; | ||||
|   export let condition2: Store<boolean> = tr; | ||||
|   export let condition3: Store<boolean> = tr; | ||||
|   export let condition4: Store<boolean> = tr; | ||||
|   export let condition5: Store<boolean> = tr; | ||||
| 
 | ||||
|   export let condition6: Store<boolean> = tr; | ||||
|   export let tab: UIEventSource<number> = new UIEventSource<number>(0); | ||||
|   let tabElements: HTMLElement[] = []; | ||||
|   $: tabElements[$tab]?.click(); | ||||
|   $: { | ||||
|     if (tabElements[tab.data]) { | ||||
|       window.setTimeout(() => tabElements[tab.data].click(), 50); | ||||
|     } | ||||
|   } | ||||
| </script> | ||||
| 
 | ||||
| <div class="tabbedgroup flex h-full w-full"> | ||||
|  | @ -41,31 +43,55 @@ | |||
|   > | ||||
|     <div class="interactive sticky top-0 flex items-center justify-between"> | ||||
|       <TabList class="flex flex-wrap"> | ||||
|         <Tab class={({ selected }) => twJoin("tab", selected && "primary", !$condition0 && "hidden")}> | ||||
|           <div bind:this={tabElements[0]} class="flex"> | ||||
|             <slot name="title0">Tab 0</slot> | ||||
|           </div> | ||||
|         </Tab> | ||||
|         <Tab class={({ selected }) => twJoin("tab", selected && "primary", !$condition1 && "hidden")}> | ||||
|           <div bind:this={tabElements[1]} class="flex"> | ||||
|             <slot name="title1" /> | ||||
|           </div> | ||||
|         </Tab> | ||||
|         <Tab class={({ selected }) => twJoin("tab", selected && "primary", !$condition2 && "hidden")}> | ||||
|           <div bind:this={tabElements[2]} class="flex"> | ||||
|             <slot name="title2" /> | ||||
|           </div> | ||||
|         </Tab> | ||||
|         <Tab class={({ selected }) => twJoin("tab", selected && "primary", !$condition3 && "hidden")}> | ||||
|           <div bind:this={tabElements[3]} class="flex"> | ||||
|             <slot name="title3" /> | ||||
|           </div> | ||||
|         </Tab> | ||||
|         <Tab class={({ selected }) => twJoin("tab", selected && "primary", !$condition4 && "hidden")}> | ||||
|           <div bind:this={tabElements[4]} class="flex"> | ||||
|             <slot name="title4" /> | ||||
|           </div> | ||||
|         </Tab> | ||||
|         {#if $$slots.title0} | ||||
|           <Tab class={({ selected }) => twJoin("tab", selected && "primary", !$condition0 && "hidden")}> | ||||
|             <div bind:this={tabElements[0]} class="flex"> | ||||
|               <slot name="title0">Tab 0</slot> | ||||
|             </div> | ||||
|           </Tab> | ||||
|         {/if} | ||||
|         {#if $$slots.title1} | ||||
|           <Tab class={({ selected }) => twJoin("tab", selected && "primary", !$condition1 && "hidden")}> | ||||
|             <div bind:this={tabElements[1]} class="flex"> | ||||
|               <slot name="title1" /> | ||||
|             </div> | ||||
|           </Tab> | ||||
|         {/if} | ||||
|         {#if $$slots.title2} | ||||
|           <Tab class={({ selected }) => twJoin("tab", selected && "primary", !$condition2 && "hidden")}> | ||||
|             <div bind:this={tabElements[2]} class="flex"> | ||||
|               <slot name="title2" /> | ||||
|             </div> | ||||
|           </Tab> | ||||
|         {/if} | ||||
|         {#if $$slots.title3} | ||||
|           <Tab class={({ selected }) => twJoin("tab", selected && "primary", !$condition3 && "hidden")}> | ||||
|             <div bind:this={tabElements[3]} class="flex"> | ||||
|               <slot name="title3" /> | ||||
|             </div> | ||||
|           </Tab> | ||||
|         {/if} | ||||
|         {#if $$slots.title4} | ||||
|           <Tab class={({ selected }) => twJoin("tab", selected && "primary", !$condition4 && "hidden")}> | ||||
|             <div bind:this={tabElements[4]} class="flex"> | ||||
|               <slot name="title4" /> | ||||
|             </div> | ||||
|           </Tab> | ||||
|         {/if} | ||||
|         {#if $$slots.title5} | ||||
|           <Tab class={({ selected }) => twJoin("tab", selected && "primary", !$condition5 && "hidden")}> | ||||
|             <div bind:this={tabElements[5]} class="flex"> | ||||
|               <slot name="title5" /> | ||||
|             </div> | ||||
|           </Tab> | ||||
|         {/if} | ||||
|         {#if $$slots.title6} | ||||
|           <Tab class={({ selected }) => twJoin("tab", selected && "primary", !$condition6 && "hidden")}> | ||||
|             <div bind:this={tabElements[6]} class="flex"> | ||||
|               <slot name="title6" /> | ||||
|             </div> | ||||
|           </Tab> | ||||
|         {/if} | ||||
|       </TabList> | ||||
|       <slot name="post-tablist" /> | ||||
|     </div> | ||||
|  | @ -96,6 +122,16 @@ | |||
|             <div /> | ||||
|           </slot> | ||||
|         </TabPanel> | ||||
|         <TabPanel class="tabpanel"> | ||||
|           <slot name="content5"> | ||||
|             <div /> | ||||
|           </slot> | ||||
|         </TabPanel> | ||||
|         <TabPanel class="tabpanel"> | ||||
|           <slot name="content6"> | ||||
|             <div /> | ||||
|           </slot> | ||||
|         </TabPanel> | ||||
|       </TabPanels> | ||||
|     </div> | ||||
|   </TabGroup> | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue