Skip to content

Commit

Permalink
debounce function
Browse files Browse the repository at this point in the history
  • Loading branch information
fdodino committed Dec 11, 2024
1 parent a26397c commit 2844909
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

[![Build](https://github.com/uqbar-project/eg-paises-svelte/actions/workflows/build.yml/badge.svg)](https://github.com/uqbar-project/eg-paises-svelte/actions/workflows/build.yml)

![demo](./videos/demo.gif)
![demo](./videos/demoNueva.gif)

En este ejemplo podemos estudiar

Expand Down
Binary file added images/forms-debounce.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 26 additions & 3 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,34 @@
import Bandera from '$lib/Bandera.svelte'
let paisBusqueda = $state('')
let busquedaAutomatica = $state(false)
let paises = $state<Pais[]>([])
let buscarHabilitado = $derived(paisBusqueda.trim() !== '')
const buscar = async () => {
paises = await paisService.buscarPais(paisBusqueda)
}
const handleKeydown = async (event: { keyCode: number }) => {
const handleKeyDownBuscar = async (event: { keyCode: number }) => {
if (event.keyCode === 13) {
await buscar()
}
}
let timeout: ReturnType<typeof setTimeout>

Check failure on line 23 in src/routes/+page.svelte

View workflow job for this annotation

GitHub Actions / build

'setTimeout' is not defined
const debounce = (callback: Function, wait: number) => {
return (...args: any[]) => {
clearTimeout(timeout)
timeout = setTimeout(() => { callback(...args) }, wait)
}
}
const handleKeyUpBuscar = async () => {
if (busquedaAutomatica && buscarHabilitado) {
debounce(buscar, 1000)()
}
}
</script>

<div class='titulo'>
Expand All @@ -26,11 +42,18 @@
<div class='busqueda'>
<input
data-testid='paisBusqueda'
onkeydown={handleKeydown}
onkeydown={handleKeyDownBuscar}
onkeyup={handleKeyUpBuscar}
bind:value={paisBusqueda}
placeholder='Ingrese un valor para buscar países'
/>
<button data-testid='buscar' onclick={buscar} disabled={!buscarHabilitado}>Buscar</button>
{#if !busquedaAutomatica}
<button data-testid='buscar' onclick={buscar} disabled={!buscarHabilitado}>Buscar</button>
{/if}
</div>
<div class='check'>
<input class='checkbox' type="checkbox" name="Buscar" id="buscar" bind:checked={busquedaAutomatica}>
<label for="buscar">Buscar automáticamente</label>
</div>
<div class='paises'>
{#each paises as pais, indice}
Expand Down
4 changes: 4 additions & 0 deletions src/routes/pais/[code]/pais.css
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ img.superpoblado {
margin-top: 1.5em;
height: 3em;
font-size: large;
}

.botonera:hover {
cursor: pointer;
}
18 changes: 17 additions & 1 deletion src/routes/paises.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ button {
height: 2rem;
}

button:hover {
cursor: pointer;
}

.check {
display: flex;
flex-direction: row;
align-items: center;
}

.checkbox {
width: auto;
margin: 0 0.5rem;
}


.paises {
margin-top: 1rem;
display: grid;
Expand Down Expand Up @@ -46,7 +62,7 @@ button {
color: darkslategray;
}

@media(min-width: 440px) {
@media(max-width: 500px) {
.paises {
grid-template-columns: 1fr 1fr;
}
Expand Down
Binary file removed videos/demo.gif
Binary file not shown.
Binary file added videos/demoNueva.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2844909

Please sign in to comment.