Skip to content

Commit

Permalink
Fixed reset bug mixed with search and navigation functionalities
Browse files Browse the repository at this point in the history
  • Loading branch information
silversonicaxel committed Aug 26, 2024
1 parent 31a3408 commit af5c5b5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/components/search-area/search-area-summary-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { FC } from 'react'

type SearchAreaSummaryItemProps = {
label: string
value?: string
value?: string | null
options?: Record<string, string>
className?: string
}
Expand Down
21 changes: 14 additions & 7 deletions src/components/search-area/search-area.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { usePathname, useRouter, useParams, useSearchParams } from 'next/navigation'
import type { FC } from 'react'
import { useCallback } from 'react'
import { useForm } from 'react-hook-form'
import { DefaultValues, useForm } from 'react-hook-form'

import { SearchAreaSummary } from './search-area-summary'
import styles from './search-area.module.css'
Expand Down Expand Up @@ -37,10 +37,10 @@ export const SearchArea: FC<SearchAreaProps> = ({ countries }) => {
const queryParams = searchParams.get('query')
const queryValuesParams = queryParams ? JSON.parse(queryParams) : {}

const defaultValues = {
name: queryValuesParams.name ?? undefined,
iso: queryValuesParams.iso ?? undefined,
city: queryValuesParams.city ?? undefined,
const defaultValues: DefaultValues<SearchPlacesFormInput> = {
name: queryValuesParams.name ?? null,
iso: queryValuesParams.iso ?? null,
city: queryValuesParams.city ?? null,
website: queryValuesParams.website ?? '',
}

Expand All @@ -64,8 +64,15 @@ export const SearchArea: FC<SearchAreaProps> = ({ countries }) => {
}, [closeDialog, pathname, replace, searchParams])

const onReset = useCallback(() => {
reset()
onSubmit({})
const emptyValues = {
name: null,
iso: null,
city: null,
website: '',
}

reset(emptyValues)
onSubmit(emptyValues)
}, [onSubmit, reset])

const watchedIso = watch('iso')
Expand Down
8 changes: 4 additions & 4 deletions src/types/search.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { CountryCode } from './country'


export type SearchPlacesFormInput = {
name?: string
iso?: string
city?: string
website?: string
name?: string | null
iso?: string | null
city?: string | null
website?: string | null
}

export type SearchPlacesFormInputOptions = {
Expand Down

0 comments on commit af5c5b5

Please sign in to comment.