|
| 1 | +import { Meta, Description, Source } from '@storybook/addon-docs/blocks'; |
| 2 | +import Readme from '../README.md'; |
| 3 | + |
| 4 | +<Meta title="Usage|Integrations/Ant Design" /> |
| 5 | + |
| 6 | +# Ant Design integration |
| 7 | +This page shows how an input and a preview component with Ant Design (`>= 4`) can be built and connected to this component. |
| 8 | + |
| 9 | +First the imports need to be declared. |
| 10 | + |
| 11 | +<Source language='tsx' code={` |
| 12 | +import * as React from 'react'; |
| 13 | +import { DSVImport as Import, ColumnsType, useDSVImport } from 'react-dsv-import'; |
| 14 | +import { Form, Input, Table } from 'antd'; |
| 15 | +`} /> |
| 16 | + |
| 17 | +## Input component |
| 18 | +<Source language='tsx' code={` |
| 19 | +const TextareaInput: React.FC = () => { |
| 20 | + const [, dispatch] = useDSVImport(); |
| 21 | + |
| 22 | + const handleChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => { |
| 23 | + dispatch({ type: 'setRaw', raw: event.target.value }); |
| 24 | + }; |
| 25 | + |
| 26 | + return <Input.TextArea rows={15} onChange={handleChange} />; |
| 27 | +}; |
| 28 | +`} /> |
| 29 | + |
| 30 | +## Preview component |
| 31 | +<Source language='tsx' code={` |
| 32 | +const TablePreview: React.FC = () => { |
| 33 | + const [context] = useDSVImport(); |
| 34 | + |
| 35 | + const getRowKey = (record: { [key: string]: string }) => { |
| 36 | + return context.parsed.indexOf(record); |
| 37 | + }; |
| 38 | + |
| 39 | + return ( |
| 40 | + <Table pagination={false} dataSource={context.parsed} rowKey={getRowKey}> |
| 41 | + {context.columns.map((r) => { |
| 42 | + return <Table.Column key={r.key} dataIndex={r.key} title={r.label ? r.label : r.key} />; |
| 43 | + })} |
| 44 | + </Table> |
| 45 | + ); |
| 46 | +}; |
| 47 | +`} /> |
| 48 | + |
| 49 | +## Create context |
| 50 | + |
| 51 | +<Source language='tsx' code={` |
| 52 | +export interface Props<T> { |
| 53 | + onChange?: (value: T[]) => void; |
| 54 | + columns: ColumnsType<T>; |
| 55 | +} |
| 56 | + |
| 57 | +export const DSVImport = <T extends { [key: string]: string }>(props: Props<T>) => { |
| 58 | + const intl = useIntl(); |
| 59 | + |
| 60 | + return ( |
| 61 | + <Form layout='vertical'> |
| 62 | + <Import<T> columns={props.columns} onChange={props.onChange}> |
| 63 | + <Form.Item label='Input'> |
| 64 | + <TextareaInput /> |
| 65 | + </Form.Item> |
| 66 | + <Form.Item label='Preview'> |
| 67 | + <TablePreview /> |
| 68 | + </Form.Item> |
| 69 | + </Import> |
| 70 | + </Form> |
| 71 | + ); |
| 72 | +}; |
| 73 | +`} /> |
0 commit comments