|
| 1 | +/* eslint-disable @typescript-eslint/no-unused-vars */ |
| 2 | +import React from 'react'; |
| 3 | +import { render } from '@testing-library/react'; |
| 4 | +import Form, { Field, List } from '../src'; |
| 5 | +import type { NamePath } from '../src/interface'; |
| 6 | + |
| 7 | +describe('nameTypeCheck', () => { |
| 8 | + it('typescript', () => { |
| 9 | + type FieldType = { |
| 10 | + a: string; |
| 11 | + b?: string[]; |
| 12 | + c?: { c1?: string; c2?: string[]; c3?: boolean[] }[]; |
| 13 | + d?: { d1?: string[]; d2?: string }; |
| 14 | + e?: { e1?: { e2?: string; e3?: string[]; e4: { e5: { e6: string } } } }; |
| 15 | + list?: { age?: string }[]; |
| 16 | + }; |
| 17 | + |
| 18 | + type fieldType = NamePath<FieldType>; |
| 19 | + |
| 20 | + const Demo: React.FC = () => { |
| 21 | + return ( |
| 22 | + <Form> |
| 23 | + {/* 无类型 */} |
| 24 | + <Field name={[]} /> |
| 25 | + <Field name={'a'} /> |
| 26 | + <Field name={['a']} /> |
| 27 | + <Field name={12} /> |
| 28 | + <Field name={[11]} /> |
| 29 | + <Field name={['d', 1]} /> |
| 30 | + <Field name={['d', 'd1']} /> |
| 31 | + <Field name={[1, 2]} /> |
| 32 | + <Field name={[true, false]} /> |
| 33 | + {/* <Field name={{ aa: '111' }} /> */} |
| 34 | + {/* 有类型 */} |
| 35 | + <Field<FieldType> name={'a'} /> |
| 36 | + <Field<FieldType> name={'b'} /> |
| 37 | + <Field<FieldType> name={'c'} /> |
| 38 | + <Field<FieldType> name={'d'} /> |
| 39 | + <Field<FieldType> name={'e'} /> |
| 40 | + <Field<FieldType> name={['a']} /> |
| 41 | + <Field<FieldType> name={['b']} /> |
| 42 | + <Field<FieldType> name={['c']} /> |
| 43 | + <Field<FieldType> name={['d']} /> |
| 44 | + <Field<FieldType> name={['e']} /> |
| 45 | + <Field<FieldType> name={['b', 1]} /> |
| 46 | + <Field<FieldType> name={['c', 1]} /> |
| 47 | + <Field<FieldType> name={['c', 1, 'c1']} /> |
| 48 | + <Field<FieldType> name={['c', 1, 'c2']} /> |
| 49 | + <Field<FieldType> name={['c', 1, 'c2', 1]} /> |
| 50 | + <Field<FieldType> name={['c', 1, 'c3']} /> |
| 51 | + <Field<FieldType> name={['c', 1, 'c3', 1]} /> |
| 52 | + <Field<FieldType> name={['d', 'd1']} /> |
| 53 | + <Field<FieldType> name={['d', 'd1', 1]} /> |
| 54 | + <Field<FieldType> name={['d', 'd2']} /> |
| 55 | + <Field<FieldType> name={['e', 'e1']} /> |
| 56 | + <Field<FieldType> name={['e', 'e1', 'e2']} /> |
| 57 | + <Field<FieldType> name={['e', 'e1', 'e3']} /> |
| 58 | + <Field<FieldType> name={['e', 'e1', 'e3', 1]} /> |
| 59 | + <Field<FieldType> name={['e', 'e1', 'e4']} /> |
| 60 | + <Field<FieldType> name={['e', 'e1', 'e4', 'e5']} /> |
| 61 | + <Field<FieldType> name={['e', 'e1', 'e4', 'e5', 'e6']} /> |
| 62 | + {/* list */} |
| 63 | + <List<FieldType> name={'list'}> |
| 64 | + {fields => { |
| 65 | + return fields.map(field => ( |
| 66 | + <Field<FieldType['list']> {...field} name={[1, 'age']} key={field.key} /> |
| 67 | + )); |
| 68 | + }} |
| 69 | + </List> |
| 70 | + </Form> |
| 71 | + ); |
| 72 | + }; |
| 73 | + render(<Demo />); |
| 74 | + }); |
| 75 | + it('type inference', () => { |
| 76 | + interface Props<T = any> { |
| 77 | + data?: T[]; |
| 78 | + list?: { name?: NamePath<T> }[]; |
| 79 | + } |
| 80 | + function func<T = any>(props: Props<T>) { |
| 81 | + return props; |
| 82 | + } |
| 83 | + func({ data: [{ a: { b: 'c' } }], list: [{ name: ['a', 'b'] }] }); |
| 84 | + }); |
| 85 | + it('more type', () => { |
| 86 | + // Moment |
| 87 | + type t1 = NamePath<{ a: { b: string; func: Moment } }>; |
| 88 | + // Function |
| 89 | + type t2 = NamePath<{ a: { b: string; func: () => { c: string } } }>; |
| 90 | + |
| 91 | + interface Moment { |
| 92 | + func2: Function; |
| 93 | + format: (format?: string) => string; |
| 94 | + } |
| 95 | + }); |
| 96 | + it('tree', () => { |
| 97 | + type t1 = NamePath<{ a: TreeNode }>; |
| 98 | + |
| 99 | + interface TreeNode { |
| 100 | + child: TreeNode[]; |
| 101 | + } |
| 102 | + }); |
| 103 | +}); |
0 commit comments