Make custom Selects reactive
This commit is contained in:
46
src/components/molecules/Select.svelte
Normal file
46
src/components/molecules/Select.svelte
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { createEventDispatcher } from 'svelte';
|
||||||
|
|
||||||
|
interface Option {
|
||||||
|
value: string
|
||||||
|
name: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export let id: string
|
||||||
|
export let name: string
|
||||||
|
export let base: Option
|
||||||
|
export let options: Option[]
|
||||||
|
|
||||||
|
const dispatch = createEventDispatcher()
|
||||||
|
|
||||||
|
let current: number = 0
|
||||||
|
let currentOptionEl: HTMLElement
|
||||||
|
$: currentOption = options[current]
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When changing select value
|
||||||
|
*/
|
||||||
|
const handleChange = ({ target: { value }}: any) => {
|
||||||
|
current = options.findIndex(option => option.value === value)
|
||||||
|
|
||||||
|
// Dispatch event to parent
|
||||||
|
dispatch('change', options[current].value)
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="select">
|
||||||
|
<slot />
|
||||||
|
|
||||||
|
<span bind:this={currentOptionEl}>
|
||||||
|
{currentOption.name}
|
||||||
|
</span>
|
||||||
|
|
||||||
|
<select {name} {id} on:change={handleChange}>
|
||||||
|
{#each options as { value, name }}
|
||||||
|
<option {value}>
|
||||||
|
{name}
|
||||||
|
</option>
|
||||||
|
{/each}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
@@ -4,12 +4,31 @@
|
|||||||
import Metas from '$components/Metas.svelte'
|
import Metas from '$components/Metas.svelte'
|
||||||
import Button from '$components/atoms/Button.svelte'
|
import Button from '$components/atoms/Button.svelte'
|
||||||
import DiscoverText from '$components/atoms/DiscoverText.svelte'
|
import DiscoverText from '$components/atoms/DiscoverText.svelte'
|
||||||
|
import Select from '$components/molecules/Select.svelte'
|
||||||
import Shop from '$components/organisms/Shop.svelte'
|
import Shop from '$components/organisms/Shop.svelte'
|
||||||
import Newsletter from '$components/organisms/Newsletter.svelte'
|
import Newsletter from '$components/organisms/Newsletter.svelte'
|
||||||
|
|
||||||
export let photos: any
|
export let photos: any
|
||||||
|
|
||||||
const { country: countries }: any = getContext('global')
|
const { country: countries }: any = getContext('global')
|
||||||
|
|
||||||
|
let filtered: boolean = false
|
||||||
|
let buttonReset: HTMLElement
|
||||||
|
let buttonShuffle: HTMLElement
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Select change events
|
||||||
|
*/
|
||||||
|
// Location select
|
||||||
|
const handleLocationChange = ({ detail: value }) => {
|
||||||
|
console.log(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort select
|
||||||
|
const handleSortChange = ({ detail: value }) => {
|
||||||
|
console.log(value)
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<Metas
|
<Metas
|
||||||
@@ -30,29 +49,44 @@
|
|||||||
<div class="filter__bar">
|
<div class="filter__bar">
|
||||||
<ul>
|
<ul>
|
||||||
<li>
|
<li>
|
||||||
<div class="select">
|
<Select
|
||||||
|
name="location" id="filter_location"
|
||||||
|
base={{ value: 'all', name: 'Worldwide' }}
|
||||||
|
options={[
|
||||||
|
{ value: 'all', name: 'Worldwide' },
|
||||||
|
...countries.map(({ slug, name }) => ({
|
||||||
|
value: slug,
|
||||||
|
name
|
||||||
|
}))
|
||||||
|
]}
|
||||||
|
on:change={handleLocationChange}
|
||||||
|
>
|
||||||
<img src="/images/icons/earth.svg" alt="Earth">
|
<img src="/images/icons/earth.svg" alt="Earth">
|
||||||
<span>Worldwide</span>
|
</Select>
|
||||||
<select name="location" id="filter_location">
|
|
||||||
<option value="worldwide">Worldwide</option>
|
|
||||||
</select>
|
|
||||||
</div>
|
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<div class="select">
|
<Select
|
||||||
|
name="sort" id="filter_sort"
|
||||||
|
base={{ value: 'all', name: 'Worldwide' }}
|
||||||
|
options={[
|
||||||
|
{ value: 'latest', name: 'Latest photos' },
|
||||||
|
{ value: 'oldest', name: 'Oldest photos' },
|
||||||
|
]}
|
||||||
|
on:change={handleSortChange}
|
||||||
|
>
|
||||||
<img src="/images/icons/sort.svg" alt="Sort">
|
<img src="/images/icons/sort.svg" alt="Sort">
|
||||||
<span>Worldwide</span>
|
</Select>
|
||||||
<select name="location" id="filter_location">
|
|
||||||
<option value="worldwide">Worldwide</option>
|
|
||||||
</select>
|
|
||||||
</div>
|
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="filter__actions">
|
<div class="filter__actions">
|
||||||
<a href="#">Reset</a>
|
{#if filtered}
|
||||||
<button class="shuffle">
|
<button class="reset button-link" bind:this={buttonReset}>
|
||||||
|
Reset
|
||||||
|
</button>
|
||||||
|
{/if}
|
||||||
|
<button class="shuffle" bind:this={buttonShuffle}>
|
||||||
<img src="/images/icons/shuffle.svg" alt="">
|
<img src="/images/icons/shuffle.svg" alt="">
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user