|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +import { Button, Col, Form, Input, Row, Select, Space } from 'antd'; |
| 18 | +import { useEffect, useMemo } from 'react'; |
| 19 | +import { useTranslation } from 'react-i18next'; |
| 20 | + |
| 21 | +export type SearchFormValues = { |
| 22 | + name?: string; |
| 23 | + id?: string; |
| 24 | + host?: string; |
| 25 | + path?: string; |
| 26 | + description?: string; |
| 27 | + plugin?: string; |
| 28 | + labels?: string[]; |
| 29 | + version?: string; |
| 30 | + status?: string; |
| 31 | +}; |
| 32 | + |
| 33 | +type Option = { |
| 34 | + label: string; |
| 35 | + value: string; |
| 36 | +}; |
| 37 | + |
| 38 | +type SearchFormProps = { |
| 39 | + onSearch?: (values: SearchFormValues) => void; |
| 40 | + onReset?: (values: SearchFormValues) => void; |
| 41 | + labelOptions?: Option[]; |
| 42 | + versionOptions?: Option[]; |
| 43 | + statusOptions?: Option[]; |
| 44 | + initialValues?: Partial<SearchFormValues>; |
| 45 | +}; |
| 46 | + |
| 47 | +export const SearchForm = (props: SearchFormProps) => { |
| 48 | + const { |
| 49 | + onSearch, |
| 50 | + onReset, |
| 51 | + labelOptions, |
| 52 | + versionOptions, |
| 53 | + statusOptions, |
| 54 | + initialValues, |
| 55 | + } = props; |
| 56 | + |
| 57 | + const { t } = useTranslation('common'); |
| 58 | + const [form] = Form.useForm<SearchFormValues>(); |
| 59 | + |
| 60 | + const defaultStatusOptions = useMemo<Option[]>( |
| 61 | + () => [ |
| 62 | + { |
| 63 | + label: t('form.searchForm.status.all'), |
| 64 | + value: 'UnPublished/Published', |
| 65 | + }, |
| 66 | + { |
| 67 | + label: t('form.searchForm.status.published'), |
| 68 | + value: 'Published', |
| 69 | + }, |
| 70 | + { |
| 71 | + label: t('form.searchForm.status.unpublished'), |
| 72 | + value: 'UnPublished', |
| 73 | + }, |
| 74 | + ], |
| 75 | + [t] |
| 76 | + ); |
| 77 | + |
| 78 | + const mergedStatusOptions = useMemo( |
| 79 | + () => statusOptions ?? defaultStatusOptions, |
| 80 | + [defaultStatusOptions, statusOptions] |
| 81 | + ); |
| 82 | + const resolvedInitialValues = useMemo(() => { |
| 83 | + const defaultStatus = mergedStatusOptions[0]?.value ?? undefined; |
| 84 | + return { |
| 85 | + status: defaultStatus, |
| 86 | + ...initialValues, |
| 87 | + } satisfies SearchFormValues; |
| 88 | + }, [initialValues, mergedStatusOptions]); |
| 89 | + |
| 90 | + useEffect(() => { |
| 91 | + form.setFieldsValue(resolvedInitialValues); |
| 92 | + }, [form, resolvedInitialValues]); |
| 93 | + |
| 94 | + const handleFinish = (values: SearchFormValues) => { |
| 95 | + onSearch?.(values); |
| 96 | + }; |
| 97 | + |
| 98 | + const handleReset = async () => { |
| 99 | + form.resetFields(); |
| 100 | + form.setFieldsValue(resolvedInitialValues); |
| 101 | + const values = form.getFieldsValue(); |
| 102 | + onReset?.(values); |
| 103 | + onSearch?.(values); |
| 104 | + }; |
| 105 | + |
| 106 | + return ( |
| 107 | + <Form |
| 108 | + form={form} |
| 109 | + layout="vertical" |
| 110 | + autoComplete="off" |
| 111 | + onFinish={handleFinish} |
| 112 | + style={{ |
| 113 | + padding: '20px', |
| 114 | + background: '#fafafa', |
| 115 | + borderRadius: '8px', |
| 116 | + border: '1px solid #e8e8e8', |
| 117 | + }} |
| 118 | + > |
| 119 | + <Row gutter={[16, 0]}> |
| 120 | + {/* First column - spans 2 rows */} |
| 121 | + <Col xs={24} sm={12} md={6}> |
| 122 | + <Form.Item<SearchFormValues> |
| 123 | + name="name" |
| 124 | + label={t('form.searchForm.fields.name')} |
| 125 | + style={{ marginBottom: '16px' }} |
| 126 | + > |
| 127 | + <Input |
| 128 | + placeholder={t('form.searchForm.placeholders.name')} |
| 129 | + allowClear |
| 130 | + /> |
| 131 | + </Form.Item> |
| 132 | + <Form.Item<SearchFormValues> |
| 133 | + name="id" |
| 134 | + label={t('form.searchForm.fields.id')} |
| 135 | + style={{ marginBottom: '16px' }} |
| 136 | + > |
| 137 | + <Input |
| 138 | + placeholder={t('form.searchForm.placeholders.id')} |
| 139 | + allowClear |
| 140 | + /> |
| 141 | + </Form.Item> |
| 142 | + </Col> |
| 143 | + |
| 144 | + {/* Second column - first row */} |
| 145 | + <Col xs={24} sm={12} md={6}> |
| 146 | + <Form.Item<SearchFormValues> |
| 147 | + name="host" |
| 148 | + label={t('form.searchForm.fields.host')} |
| 149 | + style={{ marginBottom: '16px' }} |
| 150 | + > |
| 151 | + <Input |
| 152 | + placeholder={t('form.searchForm.placeholders.host')} |
| 153 | + allowClear |
| 154 | + /> |
| 155 | + </Form.Item> |
| 156 | + {/* Second column - second row */} |
| 157 | + <Form.Item<SearchFormValues> |
| 158 | + name="plugin" |
| 159 | + label={t('form.searchForm.fields.plugin')} |
| 160 | + style={{ marginBottom: '16px' }} |
| 161 | + > |
| 162 | + <Input |
| 163 | + placeholder={t('form.searchForm.placeholders.plugin')} |
| 164 | + allowClear |
| 165 | + /> |
| 166 | + </Form.Item> |
| 167 | + </Col> |
| 168 | + |
| 169 | + {/* Third column - first row */} |
| 170 | + <Col xs={24} sm={12} md={6}> |
| 171 | + <Form.Item<SearchFormValues> |
| 172 | + name="path" |
| 173 | + label={t('form.searchForm.fields.path')} |
| 174 | + style={{ marginBottom: '16px' }} |
| 175 | + > |
| 176 | + <Input |
| 177 | + placeholder={t('form.searchForm.placeholders.path')} |
| 178 | + allowClear |
| 179 | + /> |
| 180 | + </Form.Item> |
| 181 | + {/* Third column - second row */} |
| 182 | + <Form.Item<SearchFormValues> |
| 183 | + name="labels" |
| 184 | + label={t('form.searchForm.fields.labels')} |
| 185 | + style={{ marginBottom: '16px' }} |
| 186 | + > |
| 187 | + <Select |
| 188 | + mode="multiple" |
| 189 | + placeholder={t('form.searchForm.placeholders.labels')} |
| 190 | + allowClear |
| 191 | + options={labelOptions} |
| 192 | + /> |
| 193 | + </Form.Item> |
| 194 | + </Col> |
| 195 | + |
| 196 | + {/* Fourth column - first row */} |
| 197 | + <Col xs={24} sm={12} md={6}> |
| 198 | + <Form.Item<SearchFormValues> |
| 199 | + name="description" |
| 200 | + label={t('form.searchForm.fields.description')} |
| 201 | + style={{ marginBottom: '16px' }} |
| 202 | + > |
| 203 | + <Input |
| 204 | + placeholder={t('form.searchForm.placeholders.description')} |
| 205 | + allowClear |
| 206 | + /> |
| 207 | + </Form.Item> |
| 208 | + {/* Fourth column - second row */} |
| 209 | + <Form.Item<SearchFormValues> |
| 210 | + name="version" |
| 211 | + label={t('form.searchForm.fields.version')} |
| 212 | + style={{ marginBottom: '16px' }} |
| 213 | + > |
| 214 | + <Select |
| 215 | + placeholder={t('form.searchForm.placeholders.version')} |
| 216 | + allowClear |
| 217 | + options={versionOptions} |
| 218 | + /> |
| 219 | + </Form.Item> |
| 220 | + </Col> |
| 221 | + </Row> |
| 222 | + |
| 223 | + {/* Third row - Status and Actions */} |
| 224 | + <Row gutter={[16, 0]} align="bottom" style={{ marginTop: '8px' }}> |
| 225 | + <Col xs={24} sm={12} md={6}> |
| 226 | + <Form.Item<SearchFormValues> |
| 227 | + name="status" |
| 228 | + label={t('form.searchForm.fields.status')} |
| 229 | + style={{ marginBottom: 0 }} |
| 230 | + > |
| 231 | + <Select |
| 232 | + placeholder={t('form.searchForm.placeholders.status')} |
| 233 | + allowClear |
| 234 | + options={mergedStatusOptions} |
| 235 | + /> |
| 236 | + </Form.Item> |
| 237 | + </Col> |
| 238 | + <Col xs={24} sm={12} md={18}> |
| 239 | + <div |
| 240 | + style={{ |
| 241 | + display: 'flex', |
| 242 | + justifyContent: 'flex-end', |
| 243 | + alignItems: 'center', |
| 244 | + height: '32px', |
| 245 | + }} |
| 246 | + > |
| 247 | + <Space size="middle"> |
| 248 | + <Button onClick={handleReset} size="middle"> |
| 249 | + {t('form.searchForm.actions.reset')} |
| 250 | + </Button> |
| 251 | + <Button type="primary" htmlType="submit" size="middle"> |
| 252 | + {t('form.searchForm.actions.search')} |
| 253 | + </Button> |
| 254 | + </Space> |
| 255 | + </div> |
| 256 | + </Col> |
| 257 | + </Row> |
| 258 | + </Form> |
| 259 | + ); |
| 260 | +}; |
| 261 | + |
| 262 | +export default SearchForm; |
0 commit comments