From a0801ea4bf32cf499fd45d903a51da6f865a1be5 Mon Sep 17 00:00:00 2001 From: Pablo Gracia Gil Date: Thu, 7 Nov 2024 18:55:15 +0100 Subject: [PATCH] Add state to toggle --- .../solicitudes/_components/PageContent.tsx | 4 +-- src/components/Toggle.tsx | 35 +++++++++++++------ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/app/casos-activos/solicitudes/_components/PageContent.tsx b/src/app/casos-activos/solicitudes/_components/PageContent.tsx index 59d551e4..51fcb2d7 100644 --- a/src/app/casos-activos/solicitudes/_components/PageContent.tsx +++ b/src/app/casos-activos/solicitudes/_components/PageContent.tsx @@ -52,8 +52,8 @@ export function PageContent({ currentCount, data, filters, towns }: PageContentP updateFilterAndUrlParams('currentPage', newPage); } - const handleVoluntariosChange: ChangeEventHandler = useCallback((e) => { - updateFilterAndUrlParams('soloSinVoluntarios', e.target.checked); + const handleVoluntariosChange = useCallback((state: boolean) => { + updateFilterAndUrlParams('soloSinVoluntarios', state); }, []); return ( diff --git a/src/components/Toggle.tsx b/src/components/Toggle.tsx index bf52a258..e16cb228 100644 --- a/src/components/Toggle.tsx +++ b/src/components/Toggle.tsx @@ -1,8 +1,19 @@ -import React from 'react'; +'use client'; -type ToggleProps = { handleChange: React.ChangeEventHandler; checked: boolean; label: string }; +import React, { useCallback, useEffect, useState } from 'react'; + +type ToggleProps = { handleChange: (state: boolean) => void; initiallyChecked: boolean; label: string }; + +export const Toggle = ({ initiallyChecked, handleChange, label }: ToggleProps) => { + const [isChecked, setChecked] = useState(initiallyChecked); + const onChange = useCallback(() => { + setChecked((prev) => !prev); + }, []); + + useEffect(() => { + handleChange(isChecked); + }, [isChecked]); -export const Toggle = ({ checked, handleChange, label }: ToggleProps) => { return (
- +
);