@@ -3,17 +3,12 @@ import { Button } from "@shared/uis/Button";
33import SearchIcon from "@mui/icons-material/Search" ;
44import { STUDY_REGIONS , CATEGORIES } from "../../../../../domain/types" ;
55import type { Category , StudyRegion } from "../../../../../domain/types" ;
6-
7- type SearchQuery = {
8- region ?: StudyRegion ;
9- category ?: Category ;
10- keyword ?: string ;
11- } ;
6+ import type { SearchValues } from "@features/top/components/HeritageSearchForm.tsx" ;
127
138type Props = {
14- title : string ;
15- onSearch ?: ( q : SearchQuery ) => void ;
16- onKeywordChange ?: ( keyword : string ) => void ;
9+ value ?: SearchValues ;
10+ onChange ?: ( next : SearchValues ) => void ;
11+ onSubmit ?: ( query : Partial < SearchValues > ) => void ;
1712} ;
1813
1914function Divider ( ) {
@@ -37,19 +32,33 @@ const isCategory = (value: string): value is Category => {
3732 return CATEGORIES . includes ( value as Category ) ;
3833} ;
3934
40- export function HeritageSubHeader ( { title , onSearch , onKeywordChange } : Props ) : React . JSX . Element {
35+ export function HeritageSubHeader ( { value , onChange , onSubmit } : Props ) : React . JSX . Element {
4136 const regionOptions = useMemo ( ( ) => [ "" , ...STUDY_REGIONS ] as const , [ ] ) ;
4237 const categoryOptions = useMemo ( ( ) => [ "" , ...CATEGORIES ] as const , [ ] ) ;
4338
44- const [ region , setRegion ] = useState < StudyRegion | "" > ( "" ) ;
45- const [ category , setCategory ] = useState < Category | "" > ( "" ) ;
46- const [ keyword , setKeyword ] = useState ( "" ) ;
39+ const [ internal , setInternal ] = useState < SearchValues > ( {
40+ region : value ?. region ?? "" ,
41+ category : value ?. category ?? "" ,
42+ keyword : value ?. keyword ?? "" ,
43+ yearInscribedFrom : value ?. yearInscribedFrom ?? "" ,
44+ yearInscribedTo : value ?. yearInscribedTo ?? "" ,
45+ } ) ;
46+
47+ const current = value ?? internal ;
48+
49+ const set = ( patch : Partial < SearchValues > ) => {
50+ const next : SearchValues = { ...current , ...patch } ;
51+ if ( ! value ) setInternal ( next ) ;
52+ onChange ?.( next ) ;
53+ } ;
4754
4855 const submit = ( ) => {
49- onSearch ?.( {
50- region : region === "" ? undefined : region ,
51- category : category === "" ? undefined : category ,
52- keyword : keyword . trim ( ) === "" ? undefined : keyword . trim ( ) ,
56+ onSubmit ?.( {
57+ region : current . region ,
58+ category : current . category ,
59+ keyword : current . keyword ,
60+ yearInscribedFrom : current . yearInscribedFrom ,
61+ yearInscribedTo : current . yearInscribedTo ,
5362 } ) ;
5463 } ;
5564
@@ -61,8 +70,6 @@ export function HeritageSubHeader({ title, onSearch, onKeywordChange }: Props):
6170 < form
6271 onSubmit = { ( e ) => {
6372 e . preventDefault ( ) ;
64- console . log ( title ) ;
65- onKeywordChange ?.( keyword ) ;
6673 submit ( ) ;
6774 } }
6875 className = "rounded-2xl border border-zinc-200 bg-white px-4 py-3 shadow-sm"
@@ -71,17 +78,17 @@ export function HeritageSubHeader({ title, onSearch, onKeywordChange }: Props):
7178 < div className = "flex items-center gap-3 md:w-[180px]" >
7279 < FieldLabel title = "Region" subtitle = "Area" />
7380 < select
74- value = { region }
81+ value = { current . region }
7582 onChange = { ( e ) => {
76- const value = e . target . value ;
77- setRegion ( value === "" || isStudyRegion ( value ) ? value : "" ) ;
83+ const v = e . target . value ;
84+ set ( { region : v === "" || isStudyRegion ( v ) ? v : "" } ) ;
7885 } }
7986 className = "h-10 w-full rounded-xl bg-transparent px-2 text-sm font-semibold text-zinc-900 hover:bg-zinc-50 focus:outline-none"
8087 aria-label = "Region"
8188 >
82- { regionOptions . map ( ( value , index ) => (
83- < option key = { `${ value || "all" } -${ index } ` } value = { value } >
84- { value || "All" }
89+ { regionOptions . map ( ( opt , i ) => (
90+ < option key = { `${ opt || "all" } -${ i } ` } value = { opt } >
91+ { opt || "All" }
8592 </ option >
8693 ) ) }
8794 </ select >
@@ -92,17 +99,17 @@ export function HeritageSubHeader({ title, onSearch, onKeywordChange }: Props):
9299 < div className = "flex items-center gap-3 md:w-[220px]" >
93100 < FieldLabel title = "Category" subtitle = "Type" />
94101 < select
95- value = { category }
102+ value = { current . category }
96103 onChange = { ( e ) => {
97- const value = e . target . value ;
98- setCategory ( value === "" || isCategory ( value ) ? value : "" ) ;
104+ const v = e . target . value ;
105+ set ( { category : v === "" || isCategory ( v ) ? v : "" } ) ;
99106 } }
100107 className = "h-10 w-full rounded-xl bg-transparent px-2 text-sm font-semibold text-zinc-900 hover:bg-zinc-50 focus:outline-none"
101108 aria-label = "Category"
102109 >
103- { categoryOptions . map ( ( value , index ) => (
104- < option key = { `${ value || "all" } -${ index } ` } value = { value } >
105- { value || "All" }
110+ { categoryOptions . map ( ( opt , i ) => (
111+ < option key = { `${ opt || "all" } -${ i } ` } value = { opt } >
112+ { opt || "All" }
106113 </ option >
107114 ) ) }
108115 </ select >
@@ -113,8 +120,8 @@ export function HeritageSubHeader({ title, onSearch, onKeywordChange }: Props):
113120 < div className = "flex items-center gap-3 md:flex-1" >
114121 < FieldLabel title = "Keyword" subtitle = "Name / Country" />
115122 < input
116- value = { keyword }
117- onChange = { ( e ) => setKeyword ( e . target . value ) }
123+ value = { current . keyword }
124+ onChange = { ( e ) => set ( { keyword : e . target . value } ) }
118125 placeholder = "Search the List"
119126 className = "h-10 w-full rounded-xl bg-transparent px-2 text-sm text-zinc-900 placeholder:text-zinc-400 hover:bg-zinc-50 focus:outline-none"
120127 aria-label = "Keyword"
0 commit comments