-
Couldn't load subscription status.
- Fork 0
Thanaphomh/sucu 40 component search and drop down #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ThanaphomH
merged 23 commits into
dev
from
thanaphomh/sucu-40-component-search-and-drop-down
Sep 15, 2024
Merged
Changes from 6 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
58f3cf8
feat: dropdownItem
ThanaphomH 64999f4
feat: searchBar
ThanaphomH 1383d8b
feat: dropdownList
ThanaphomH 38125e6
feat: dropdown
ThanaphomH ead4a68
feat: playground update
ThanaphomH 94df1d9
refactor: lint fix
ThanaphomH 32c1bb6
refactor: merge hover variants into default in dropdownItem
ThanaphomH 62b490c
refactor: add init value
ThanaphomH 293aac7
refactor: remove focus value
ThanaphomH 1dd0041
refactor: change focus into css
ThanaphomH 1038131
refactor: remove debug value
ThanaphomH c1bef66
refactor: remove two-way binding
ThanaphomH f1e9a32
refactor: remove placeholder to set to currentChoice
ThanaphomH 84a0930
refactor: merge reactive function
ThanaphomH ad620d1
feat: add focus check
ThanaphomH e4338f5
refactor: use cn instead
ThanaphomH 53780aa
refactor: remove isHover
ThanaphomH 4613586
refactor: use dispatch detail instead
ThanaphomH dc40739
style: lint
ThanaphomH 0dded6d
Merge branch 'dev' into thanaphomh/sucu-40-component-search-and-drop-…
ThanaphomH 56fa717
feat: add z-value to DropdownList
ThanaphomH 4459f4e
fix: handle long dropdown item
ThanaphomH 46214fe
style: lint
ThanaphomH File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <script lang="ts"> | ||
| import Fa from 'svelte-fa'; | ||
| import { faChevronDown } from '@fortawesome/free-solid-svg-icons'; | ||
| import { dropdownVariants, type DropdownProps } from '../../../styles/tailwind/dropdown'; | ||
| import DropdownList from '$lib/components/Dropdown/DropdownList.svelte'; | ||
| import { onMount } from 'svelte'; | ||
|
|
||
| export let items: string[] = []; | ||
| export let currentChoice: string | null = null; | ||
| export let outerClass: string = ''; | ||
| let isOpen = false; | ||
| let hasSelected = false; | ||
| let currentVariant: DropdownProps['variant'] = 'transparent'; | ||
| let placeholder: string = 'Select Item'; | ||
| let saveChoice: string | null = null; | ||
|
|
||
| onMount(() => { | ||
| currentChoice = items[0] || placeholder; | ||
ThanaphomH marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| saveChoice = currentChoice; | ||
| }); | ||
|
|
||
| $: { | ||
| if (isOpen) { | ||
| currentVariant = 'focus'; | ||
| } else if (hasSelected) { | ||
| currentVariant = 'default'; | ||
| } else { | ||
| currentVariant = 'transparent'; | ||
| } | ||
| } | ||
|
|
||
| $: dropdownClass = dropdownVariants({ variant: currentVariant }); | ||
|
|
||
| $: if (currentChoice) { | ||
| isOpen = false; | ||
| if (currentChoice !== saveChoice) { | ||
| hasSelected = true; | ||
| } | ||
| } | ||
|
|
||
ThanaphomH marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| function handleClickOpen() { | ||
| isOpen = !isOpen; | ||
| } | ||
|
|
||
| $: console.log(isOpen); | ||
ThanaphomH marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </script> | ||
|
|
||
| <div class={`relative w-full ${outerClass}`}> | ||
ThanaphomH marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <button | ||
| class={`w-full px-4 py-3 flex flex-row justify-between items-center rounded-sm ${dropdownClass}`} | ||
ThanaphomH marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| on:click={handleClickOpen} | ||
| > | ||
| <span>{currentChoice || placeholder}</span> | ||
| <Fa icon={faChevronDown} class={`w-6 h-6 transition-transform ${isOpen && 'rotate-180'}`} /> | ||
ThanaphomH marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </button> | ||
|
|
||
| {#if isOpen} | ||
| <div class="absolute top-[calc(100%+8px)] w-full"> | ||
| <DropdownList bind:items bind:currentChoice /> | ||
| </div> | ||
| {/if} | ||
| </div> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| <script lang="ts"> | ||
| import { | ||
| dropdownItemVariants, | ||
| type DropdownItemProps | ||
| } from '../../../styles/tailwind/dropdownItem'; | ||
|
|
||
| export let text: string; | ||
| export let disabled: boolean = false; | ||
| export let currentSelectedChoice: string | null = null; | ||
|
|
||
| let currentVariant: DropdownItemProps['variant'] = 'default'; | ||
| let isHover: boolean = false; | ||
ThanaphomH marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let isSelected = false; | ||
|
|
||
| $: isSelected = currentSelectedChoice === text; | ||
|
|
||
| $: { | ||
| if (disabled) { | ||
| currentVariant = 'disabled'; | ||
| } else if (isHover) { | ||
| currentVariant = 'hover'; | ||
ThanaphomH marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } else if (isSelected) { | ||
| currentVariant = 'pressed'; | ||
| } else { | ||
| currentVariant = 'default'; | ||
| } | ||
| } | ||
|
|
||
| function handleClick() { | ||
| isHover = false; | ||
| currentSelectedChoice = text; | ||
| } | ||
ThanaphomH marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| function handleMouseEnter() { | ||
| if (currentVariant === 'default') isHover = true; | ||
| } | ||
| function handleMouseLeave() { | ||
| isHover = false; | ||
| } | ||
|
|
||
ThanaphomH marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $: dropdownClass = dropdownItemVariants({ variant: isHover ? 'hover' : currentVariant }); | ||
| </script> | ||
|
|
||
| <button | ||
| class={dropdownClass} | ||
| on:click={handleClick} | ||
| on:mouseenter={handleMouseEnter} | ||
| on:mouseleave={handleMouseLeave} | ||
ThanaphomH marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| {disabled} | ||
| > | ||
| {text} | ||
| </button> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <script lang="ts"> | ||
| import DropdownItem from '$lib/components/Dropdown/DropdownItem.svelte'; | ||
|
|
||
| export let items: string[] = ['Hi', 'jpoas', 'adfsd', 'asdafsds']; | ||
ThanaphomH marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| export let currentChoice: string | null = null; | ||
| </script> | ||
|
|
||
| <div | ||
| class="flex flex-col outline outline-1 outline-sucu-gray rounded-sm gap-1 overflow-hidden bg-white" | ||
| > | ||
| {#each items as item} | ||
| <DropdownItem text={item} bind:currentSelectedChoice={currentChoice} /> | ||
ThanaphomH marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| {/each} | ||
| </div> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <script lang="ts"> | ||
| import { faSearch } from '@fortawesome/free-solid-svg-icons'; | ||
| import Fa from 'svelte-fa'; | ||
|
|
||
| export let value: string; | ||
ThanaphomH marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let inputRef: HTMLInputElement; | ||
| let isFocus = false; | ||
| function handleFocus() { | ||
| isFocus = true; | ||
| } | ||
| function handleBlur() { | ||
| isFocus = false; | ||
| } | ||
|
|
||
| function handleClickIcon() { | ||
| inputRef.focus(); | ||
| } | ||
| </script> | ||
|
|
||
| <div | ||
| class={`w-full flex flex-row items-center gap-3 px-4 py-3 rounded-sm border ${isFocus ? 'border-sucu-pink-03' : 'border-sucu-gray-light'}`} | ||
| > | ||
| <button on:click={handleClickIcon}> | ||
| <Fa icon={faSearch} class="w-5 h-5" /> | ||
| </button> | ||
|
|
||
| <input | ||
| type="text" | ||
| class="w-full text-black focus:border-none focus:outline-none" | ||
| placeholder="ค้นหา" | ||
| bind:value | ||
| bind:this={inputRef} | ||
| on:focus={handleFocus} | ||
| on:blur={handleBlur} | ||
| /> | ||
| </div> | ||
ThanaphomH marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { tv, type VariantProps } from 'tailwind-variants'; | ||
| import { typography } from './typography'; | ||
|
|
||
| export const dropdownVariants = tv({ | ||
| base: `w-full ${typography({ variant: 'body-normal' })} transition-colors duration-200 px-4 py-2 text-left`, | ||
| variants: { | ||
| variant: { | ||
| default: `text-sucu-gray-dark border border-sucu-gray`, | ||
| focus: `text-sucu-gray border border-sucu-pink-03`, | ||
| transparent: `text-sucu-gray-light border border-sucu-gray-light` | ||
| } | ||
| }, | ||
| defaultVariants: { | ||
| variant: 'default' | ||
| } | ||
| }); | ||
|
|
||
| export type Variant = VariantProps<typeof dropdownVariants>['variant']; | ||
|
|
||
| export type DropdownProps = { | ||
| variant?: Variant; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { tv, type VariantProps } from 'tailwind-variants'; | ||
| import { typography } from './typography'; | ||
|
|
||
| export const dropdownItemVariants = tv({ | ||
| base: `w-full ${typography({ variant: 'body-normal' })} transition-colors duration-200 px-4 py-2 text-left`, | ||
| variants: { | ||
| variant: { | ||
| default: `bg-white text-black`, | ||
| hover: `bg-sucu-pink-05 text-sucu-pink-01`, | ||
| pressed: `bg-sucu-pink-01 text-white`, | ||
| disabled: `bg-white text-sucu-gray-dark` | ||
ThanaphomH marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| }, | ||
| defaultVariants: { | ||
| variant: 'default' | ||
| } | ||
| }); | ||
|
|
||
| export type Variant = VariantProps<typeof dropdownItemVariants>['variant']; | ||
|
|
||
| export type DropdownItemProps = { | ||
| variant?: Variant; | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.