11/* eslint-disable max-lines-per-function */
22import React , { FC , useState , useEffect } from 'react'
3- import { Typography , Form , Select , SelectProps } from 'antd'
3+ import { Typography , Form , Select , SelectProps , Input } from 'antd'
44import type { CustomTagProps } from 'rc-select/lib/BaseSelect'
55import { useLocation , useSearchParams } from 'react-router-dom'
66import { getKinds } from 'api/bff/search/getKinds'
@@ -20,10 +20,16 @@ export const Search: FC<TSearchProps> = ({ cluster }) => {
2020 const [ searchParams , setSearchParams ] = useSearchParams ( )
2121 const location = useLocation ( )
2222
23- const FIELD_NAME = 'kinds' // the Form.Item name
23+ const FIELD_NAME = 'kinds'
24+ const FIELD_NAME_STRING = 'name'
25+ const FIELD_NAME_MULTIPLE = 'labels'
26+ const TYPE_SELECTOR = 'TYPE_SELECTOR'
2427 const QUERY_KEY = 'kinds' // the query param name
2528
2629 const watchedKinds = Form . useWatch < string [ ] | undefined > ( FIELD_NAME , form )
30+ const watchedName = Form . useWatch < string | undefined > ( FIELD_NAME_STRING , form )
31+ const watchedMultiple = Form . useWatch < string [ ] | undefined > ( FIELD_NAME_MULTIPLE , form )
32+ const watchedTypedSelector = Form . useWatch < string | undefined > ( TYPE_SELECTOR , form )
2733
2834 // eslint-disable-next-line @typescript-eslint/no-unused-vars
2935 const [ kindIndex , setKindIndex ] = useState < TKindIndex > ( )
@@ -97,7 +103,7 @@ export const Search: FC<TSearchProps> = ({ cluster }) => {
97103 return (
98104 < Styled . CatContainer >
99105 < Form form = { form } layout = "vertical" >
100- < Styled . SelectContainer >
106+ < Styled . FormContainer >
101107 < Form . Item name = { FIELD_NAME } label = "Kinds" >
102108 < Select
103109 mode = "multiple"
@@ -109,9 +115,59 @@ export const Search: FC<TSearchProps> = ({ cluster }) => {
109115 tagRender = { tagRender }
110116 />
111117 </ Form . Item >
112- </ Styled . SelectContainer >
113- { /* Example of "watching" the value for display or side-effects */ }
114- < div > Current: { ( watchedKinds || [ ] ) . join ( ', ' ) } </ div >
118+ < Form . Item name = { TYPE_SELECTOR } label = "Type" >
119+ < Select
120+ placeholder = "Select"
121+ options = { [
122+ { label : 'Name' , value : 'name' } ,
123+ { label : 'Labels' , value : 'labels' } ,
124+ ] }
125+ defaultValue = "name"
126+ filterOption = { filterSelectOptions }
127+ showSearch
128+ />
129+ </ Form . Item >
130+ < Styled . HideableContainer $isHidden = { watchedTypedSelector === 'labels' } >
131+ < Form . Item name = { FIELD_NAME_STRING } label = "Name" >
132+ < Input allowClear />
133+ </ Form . Item >
134+ </ Styled . HideableContainer >
135+ < Styled . HideableContainer $isHidden = { watchedTypedSelector === 'name' || watchedTypedSelector === undefined } >
136+ < Form . Item
137+ name = { FIELD_NAME_MULTIPLE }
138+ label = "Labels"
139+ validateTrigger = "onBlur"
140+ rules = { [
141+ ( ) => ( {
142+ validator ( _ , value ) {
143+ if (
144+ Array . isArray ( value ) &&
145+ value . every ( str => typeof str === 'string' && str . includes ( '=' ) && ! str . startsWith ( '=' ) )
146+ ) {
147+ return Promise . resolve ( )
148+ }
149+ return Promise . reject ( new Error ( 'Please enter key=value style' ) )
150+ } ,
151+ } ) ,
152+ ] }
153+ >
154+ < Select
155+ mode = "tags"
156+ allowClear
157+ placeholder = "Select"
158+ // dropdownStyle={{ display: 'none' }}
159+ tokenSeparators = { [ ',' , ' ' , ' ' ] }
160+ suffixIcon = { null }
161+ filterOption = { filterSelectOptions }
162+ tagRender = { tagRender }
163+ />
164+ </ Form . Item >
165+ </ Styled . HideableContainer >
166+ { /* Example of "watching" the value for display or side-effects */ }
167+ < div > Current: { ( watchedKinds || [ ] ) . join ( ', ' ) } </ div >
168+ < div > Current name: { watchedName } </ div >
169+ < div > Current labels: { ( watchedMultiple || [ ] ) . join ( ', ' ) } </ div >
170+ </ Styled . FormContainer >
115171 </ Form >
116172 < Typography . Title > To Be Done</ Typography . Title >
117173 </ Styled . CatContainer >
0 commit comments