index.tsx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. import type { ISchema } from '@formily/json-schema';
  2. import { createSchemaField } from '@formily/react';
  3. import {
  4. ArrayItems,
  5. Form,
  6. FormButtonGroup,
  7. FormGrid,
  8. FormItem,
  9. FormTab,
  10. Input,
  11. PreviewText,
  12. Select,
  13. } from '@formily/antd';
  14. import { createForm } from '@formily/core';
  15. import GroupNameControl from '@/components/SearchComponent/GroupNameControl';
  16. import { DeleteOutlined, DoubleRightOutlined } from '@ant-design/icons';
  17. import { Button, Dropdown, Menu, message, Popconfirm, Popover, Input as AInput } from 'antd';
  18. import { useState } from 'react';
  19. import type { ProColumns } from '@jetlinks/pro-table';
  20. import type { EnumData } from '@/utils/typings';
  21. import styles from './index.less';
  22. import Service from '@/components/SearchComponent/service';
  23. const ui2Server = (source: SearchTermsUI): SearchTermsServer => [
  24. { terms: source.terms1, type: source.type },
  25. { terms: source.terms2 },
  26. ];
  27. const server2Ui = (source: SearchTermsServer): SearchTermsUI => ({
  28. terms1: source[0].terms,
  29. terms2: source[1].terms,
  30. type: source[0].type || 'and',
  31. });
  32. interface Props<T> {
  33. field: ProColumns<T>[];
  34. onSearch: (params: any) => void;
  35. target: string;
  36. }
  37. const termType = [
  38. { label: '=', value: 'eq' },
  39. { label: '!=', value: 'not' },
  40. { label: '包含', value: 'like' },
  41. { label: '不包含', value: 'not like' },
  42. { label: '>', value: 'gt' },
  43. { label: '>=', value: 'gte' },
  44. { label: '<', value: 'lt' },
  45. { label: '<=', value: 'lte' },
  46. { label: '属于', value: 'in' },
  47. { label: '不属于', value: 'not in' },
  48. // { label: '为空', value: '=\'\'' },
  49. // { label: '不为空', value: '!=\'\'' },
  50. // { label: 'isnull', value: 'is null' },
  51. // { label: 'notnull', value: 'not null' },
  52. // { label: '介于', value: 'between' },
  53. // { label: '不介于', value: 'not between' },
  54. ];
  55. const service = new Service();
  56. const SearchComponent = <T extends Record<string, any>>({ field, onSearch, target }: Props<T>) => {
  57. const [expand, setExpand] = useState<boolean>(true);
  58. const [logVisible, setLogVisible] = useState<boolean>(false);
  59. const [alias, setAlias] = useState<string>('');
  60. const [aliasVisible, setAliasVisible] = useState<boolean>(false);
  61. const [initParams, setInitParams] = useState<SearchTermsServer>([
  62. { terms: [{ column: null }], type: 'and' },
  63. { terms: [{ column: null }] },
  64. ]);
  65. const [history, setHistory] = useState([]);
  66. const form = createForm<SearchTermsUI>({
  67. validateFirst: true,
  68. initialValues: server2Ui(initParams),
  69. });
  70. const queryHistory = async () => {
  71. const response = await service.history.query(target);
  72. if (response.status === 200) {
  73. setHistory(response.result);
  74. }
  75. };
  76. // useEffect(() => {
  77. // (queryHistory)();
  78. // }, [target]);
  79. const handleExpand = () => {
  80. const value = form.values;
  81. if (!expand) {
  82. value.terms1.splice(1, 2);
  83. value.terms2.splice(1, 2);
  84. } else {
  85. value.terms2.push({ column: null }, { column: null });
  86. value.terms1.push({ column: null }, { column: null });
  87. }
  88. setInitParams(ui2Server(value));
  89. setExpand(!expand);
  90. };
  91. const SchemaField = createSchemaField({
  92. components: {
  93. FormItem,
  94. FormTab,
  95. Input,
  96. Select,
  97. FormGrid,
  98. ArrayItems,
  99. PreviewText,
  100. GroupNameControl,
  101. },
  102. });
  103. const filterSearchTerm = (): EnumData[] =>
  104. field
  105. .filter((item) => item.dataIndex)
  106. .filter((item) => !['index', 'option'].includes(item.dataIndex as string))
  107. .map((i) => ({ label: i.title, value: i.dataIndex } as EnumData));
  108. const createGroup = (name: string): ISchema => ({
  109. 'x-decorator': 'FormItem',
  110. 'x-decorator-props': {
  111. gridSpan: 4,
  112. },
  113. 'x-component': 'ArrayItems',
  114. type: 'array',
  115. 'x-value': new Array(expand ? 1 : 3).fill({ column: null }),
  116. items: {
  117. type: 'object',
  118. 'x-component': 'FormGrid',
  119. 'x-component-props': {
  120. minColumns: 6,
  121. maxColumns: 6,
  122. },
  123. properties: {
  124. type: {
  125. 'x-decorator': 'FormItem',
  126. 'x-component': 'GroupNameControl',
  127. 'x-decorator-props': {
  128. gridSpan: 1,
  129. },
  130. 'x-component-props': {
  131. name: name,
  132. },
  133. },
  134. column: {
  135. type: 'string',
  136. 'x-decorator': 'FormItem',
  137. 'x-component': 'Select',
  138. 'x-decorator-props': {
  139. gridSpan: 2,
  140. },
  141. 'x-component-props': {
  142. placeholder: '请选择',
  143. },
  144. enum: filterSearchTerm(),
  145. },
  146. termType: {
  147. type: 'enum',
  148. 'x-decorator': 'FormItem',
  149. 'x-component': 'Select',
  150. 'x-decorator-props': {
  151. gridSpan: 1,
  152. },
  153. default: 'like',
  154. enum: termType,
  155. },
  156. value: {
  157. 'x-decorator-props': {
  158. gridSpan: 2,
  159. },
  160. 'x-decorator': 'FormItem',
  161. 'x-component': 'Input',
  162. },
  163. },
  164. },
  165. });
  166. const schema: ISchema = {
  167. type: 'object',
  168. properties: {
  169. layout: {
  170. type: 'void',
  171. 'x-component': 'FormGrid',
  172. 'x-component-props': {
  173. minColumns: 9,
  174. maxColumns: 9,
  175. },
  176. properties: {
  177. terms1: createGroup('第一组'),
  178. type: {
  179. 'x-decorator': 'FormItem',
  180. 'x-component': 'Select',
  181. 'x-decorator-props': {
  182. gridSpan: 1,
  183. style: {
  184. display: 'flex',
  185. alignItems: 'center',
  186. marginTop: '-22px',
  187. padding: '0 30px',
  188. },
  189. },
  190. default: 'and',
  191. enum: [
  192. { label: '并且', value: 'and' },
  193. { label: '或者', value: 'or' },
  194. ],
  195. },
  196. terms2: createGroup('第二组'),
  197. },
  198. },
  199. },
  200. };
  201. const menu = () => {
  202. return (
  203. <Menu>
  204. {history.map((item: any) => (
  205. <Menu.Item onClick={() => message.success(item.name)} key={item.id}>
  206. <div
  207. style={{
  208. display: 'flex',
  209. justifyContent: 'space-between',
  210. alignItems: 'center',
  211. }}
  212. >
  213. <span style={{ marginRight: '5px' }}>{item.name}</span>
  214. <Popconfirm
  215. onConfirm={async () => {
  216. const response = await service.history.remove(target, item.key);
  217. if (response.status === 200) {
  218. message.success('操作成功');
  219. const temp = history.filter((h: any) => h.key !== item.key);
  220. setHistory(temp);
  221. }
  222. }}
  223. title={'确认删除吗?'}
  224. >
  225. <DeleteOutlined />
  226. </Popconfirm>
  227. </div>
  228. </Menu.Item>
  229. ))}
  230. </Menu>
  231. );
  232. };
  233. const handleSearch = async () => {
  234. const value = await form.submit<SearchTermsUI>();
  235. // TODO
  236. value.terms1 = value.terms1.filter((item) => item.column != null).filter((item) => item.value);
  237. value.terms2 = value.terms2.filter((item) => item.column != null).filter((item) => item.value);
  238. onSearch(value);
  239. };
  240. const handleSaveLog = async () => {
  241. const value = await form.submit<SearchTermsUI>();
  242. const response = await service.history.save(target, {
  243. name: alias,
  244. content: JSON.stringify(ui2Server(value)),
  245. });
  246. if (response.status === 200) {
  247. message.success('保存成功!');
  248. } else {
  249. message.success('保存失败');
  250. }
  251. setAliasVisible(!aliasVisible);
  252. };
  253. return (
  254. <div>
  255. <Form form={form} labelCol={4} wrapperCol={18}>
  256. <SchemaField schema={schema} />
  257. <div className={styles.action}>
  258. <FormButtonGroup.FormItem labelCol={10} wrapperCol={14}>
  259. <Dropdown.Button
  260. trigger={['click']}
  261. onClick={handleSearch}
  262. visible={logVisible}
  263. onVisibleChange={async (visible) => {
  264. setLogVisible(visible);
  265. if (visible) {
  266. await queryHistory();
  267. console.log('test');
  268. }
  269. }}
  270. type="primary"
  271. overlay={menu}
  272. >
  273. 搜索
  274. </Dropdown.Button>
  275. <Popover
  276. content={
  277. <>
  278. <AInput.TextArea
  279. rows={3}
  280. value={alias}
  281. onChange={(e) => setAlias(e.target.value)}
  282. />
  283. <Button onClick={handleSaveLog} type="primary" className={styles.saveLog}>
  284. 保存
  285. </Button>
  286. </>
  287. }
  288. visible={aliasVisible}
  289. onVisibleChange={(visible) => {
  290. setAlias('');
  291. setInitParams(ui2Server(form.values));
  292. setAliasVisible(visible);
  293. }}
  294. title="搜索名称"
  295. trigger="click"
  296. >
  297. <Button block>保存</Button>
  298. </Popover>
  299. <Button block>重置</Button>
  300. </FormButtonGroup.FormItem>
  301. <div>
  302. <DoubleRightOutlined
  303. onClick={handleExpand}
  304. style={{ fontSize: 20 }}
  305. rotate={expand ? 90 : -90}
  306. />
  307. </div>
  308. </div>
  309. </Form>
  310. </div>
  311. );
  312. };
  313. export default SearchComponent;