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 Button from '$components/atoms/Button.svelte'
|
||||
import DiscoverText from '$components/atoms/DiscoverText.svelte'
|
||||
import Select from '$components/molecules/Select.svelte'
|
||||
import Shop from '$components/organisms/Shop.svelte'
|
||||
import Newsletter from '$components/organisms/Newsletter.svelte'
|
||||
|
||||
export let photos: any
|
||||
|
||||
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>
|
||||
|
||||
<Metas
|
||||
@@ -30,29 +49,44 @@
|
||||
<div class="filter__bar">
|
||||
<ul>
|
||||
<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">
|
||||
<span>Worldwide</span>
|
||||
<select name="location" id="filter_location">
|
||||
<option value="worldwide">Worldwide</option>
|
||||
</select>
|
||||
</div>
|
||||
</Select>
|
||||
</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">
|
||||
<span>Worldwide</span>
|
||||
<select name="location" id="filter_location">
|
||||
<option value="worldwide">Worldwide</option>
|
||||
</select>
|
||||
</div>
|
||||
</Select>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<div class="filter__actions">
|
||||
<a href="#">Reset</a>
|
||||
<button class="shuffle">
|
||||
{#if filtered}
|
||||
<button class="reset button-link" bind:this={buttonReset}>
|
||||
Reset
|
||||
</button>
|
||||
{/if}
|
||||
<button class="shuffle" bind:this={buttonShuffle}>
|
||||
<img src="/images/icons/shuffle.svg" alt="">
|
||||
</button>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user