|
| 1 | +import * as React from 'react'; |
| 2 | +import polyglotI18nProvider from 'ra-i18n-polyglot'; |
| 3 | +import englishMessages from 'ra-language-english'; |
| 4 | + |
| 5 | +import { CoreAdmin } from '../../core/CoreAdmin'; |
| 6 | +import { Resource } from '../../core/Resource'; |
| 7 | +import { CreateBase } from '../../controller/create/CreateBase'; |
| 8 | +import { testDataProvider } from '../../dataProvider/testDataProvider'; |
| 9 | +import { DataProvider } from '../../types'; |
| 10 | +import { Form } from '../../form/Form'; |
| 11 | +import { useInput } from '../../form/useInput'; |
| 12 | +import { InputProps } from '../../form/types'; |
| 13 | +import { TestMemoryRouter } from '../../routing/TestMemoryRouter'; |
| 14 | +import { |
| 15 | + ReferenceArrayInputBase, |
| 16 | + ReferenceArrayInputBaseProps, |
| 17 | +} from './ReferenceArrayInputBase'; |
| 18 | +import { ChoicesProps, useChoicesContext } from '../../form'; |
| 19 | +import { useGetRecordRepresentation } from '../..'; |
| 20 | + |
| 21 | +export default { title: 'ra-core/controller/ReferenceArrayInputBase' }; |
| 22 | + |
| 23 | +const tags = [ |
| 24 | + { id: 0, name: '3D' }, |
| 25 | + { id: 1, name: 'Architecture' }, |
| 26 | + { id: 2, name: 'Design' }, |
| 27 | + { id: 3, name: 'Painting' }, |
| 28 | + { id: 4, name: 'Photography' }, |
| 29 | +]; |
| 30 | + |
| 31 | +const defaultDataProvider = testDataProvider({ |
| 32 | + getList: () => |
| 33 | + // @ts-ignore |
| 34 | + Promise.resolve({ |
| 35 | + data: tags, |
| 36 | + total: tags.length, |
| 37 | + }), |
| 38 | + // @ts-ignore |
| 39 | + getMany: (resource, params) => { |
| 40 | + if (process.env.NODE_ENV !== 'test') { |
| 41 | + console.log('getMany', resource, params); |
| 42 | + } |
| 43 | + return Promise.resolve({ |
| 44 | + data: params.ids.map(id => tags.find(tag => tag.id === id)), |
| 45 | + }); |
| 46 | + }, |
| 47 | +}); |
| 48 | + |
| 49 | +const i18nProvider = polyglotI18nProvider(() => englishMessages); |
| 50 | + |
| 51 | +const CheckboxGroupInput = ( |
| 52 | + props: Omit<InputProps, 'source'> & ChoicesProps |
| 53 | +) => { |
| 54 | + const { allChoices, isPending, error, resource, source, total } = |
| 55 | + useChoicesContext(props); |
| 56 | + const input = useInput({ ...props, source }); |
| 57 | + const getRecordRepresentation = useGetRecordRepresentation(resource); |
| 58 | + |
| 59 | + if (isPending) { |
| 60 | + return <span>Loading...</span>; |
| 61 | + } |
| 62 | + |
| 63 | + if (error) { |
| 64 | + return <span>Error: {error.message}</span>; |
| 65 | + } |
| 66 | + |
| 67 | + return ( |
| 68 | + <div> |
| 69 | + {allChoices.map(choice => ( |
| 70 | + <label key={choice.id}> |
| 71 | + <input |
| 72 | + type="checkbox" |
| 73 | + // eslint-disable-next-line eqeqeq |
| 74 | + checked={input.field.value.some(id => id == choice.id)} |
| 75 | + onChange={() => { |
| 76 | + const newValue = input.field.value.some( |
| 77 | + // eslint-disable-next-line eqeqeq |
| 78 | + id => id == choice.id |
| 79 | + ) |
| 80 | + ? input.field.value.filter( |
| 81 | + // eslint-disable-next-line eqeqeq |
| 82 | + id => id != choice.id |
| 83 | + ) |
| 84 | + : [...input.field.value, choice.id]; |
| 85 | + input.field.onChange(newValue); |
| 86 | + }} |
| 87 | + /> |
| 88 | + {getRecordRepresentation(choice)} |
| 89 | + </label> |
| 90 | + ))} |
| 91 | + <div> |
| 92 | + Selected {resource}: {input.field.value.join(', ')} |
| 93 | + </div> |
| 94 | + <div> |
| 95 | + Total {resource}: {total} |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + ); |
| 99 | +}; |
| 100 | + |
| 101 | +export const Basic = ({ |
| 102 | + dataProvider = defaultDataProvider, |
| 103 | + meta, |
| 104 | + ...props |
| 105 | +}: Partial<ReferenceArrayInputBaseProps> & { |
| 106 | + dataProvider?: DataProvider; |
| 107 | + meta?: boolean; |
| 108 | +}) => ( |
| 109 | + <TestMemoryRouter initialEntries={['/posts/create']}> |
| 110 | + <CoreAdmin dataProvider={dataProvider} i18nProvider={i18nProvider}> |
| 111 | + <Resource |
| 112 | + name="posts" |
| 113 | + create={ |
| 114 | + <CreateBase resource="posts" record={{ tags_ids: [1, 3] }}> |
| 115 | + <h1>Create Post</h1> |
| 116 | + <Form> |
| 117 | + <ReferenceArrayInputBase |
| 118 | + reference="tags" |
| 119 | + resource="posts" |
| 120 | + source="tags_ids" |
| 121 | + queryOptions={ |
| 122 | + meta ? { meta: { foo: 'bar' } } : {} |
| 123 | + } |
| 124 | + {...props} |
| 125 | + > |
| 126 | + <CheckboxGroupInput /> |
| 127 | + </ReferenceArrayInputBase> |
| 128 | + </Form> |
| 129 | + </CreateBase> |
| 130 | + } |
| 131 | + /> |
| 132 | + </CoreAdmin> |
| 133 | + </TestMemoryRouter> |
| 134 | +); |
| 135 | + |
| 136 | +Basic.args = { |
| 137 | + meta: false, |
| 138 | +}; |
| 139 | + |
| 140 | +Basic.argTypes = { |
| 141 | + meta: { control: 'boolean' }, |
| 142 | +}; |
| 143 | + |
| 144 | +export const WithError = () => ( |
| 145 | + <TestMemoryRouter initialEntries={['/posts/create']}> |
| 146 | + <CoreAdmin |
| 147 | + dataProvider={ |
| 148 | + { |
| 149 | + getList: () => Promise.reject(new Error('fetch error')), |
| 150 | + getMany: () => |
| 151 | + Promise.resolve({ data: [{ id: 5, name: 'test1' }] }), |
| 152 | + } as unknown as DataProvider |
| 153 | + } |
| 154 | + i18nProvider={i18nProvider} |
| 155 | + > |
| 156 | + <Resource |
| 157 | + name="posts" |
| 158 | + create={ |
| 159 | + <CreateBase resource="posts" record={{ tags_ids: [1, 3] }}> |
| 160 | + <h1>Create Post</h1> |
| 161 | + <Form |
| 162 | + onSubmit={() => {}} |
| 163 | + defaultValues={{ tag_ids: [1, 3] }} |
| 164 | + > |
| 165 | + <ReferenceArrayInputBase |
| 166 | + reference="tags" |
| 167 | + resource="posts" |
| 168 | + source="tag_ids" |
| 169 | + > |
| 170 | + <CheckboxGroupInput /> |
| 171 | + </ReferenceArrayInputBase> |
| 172 | + </Form> |
| 173 | + </CreateBase> |
| 174 | + } |
| 175 | + /> |
| 176 | + </CoreAdmin> |
| 177 | + </TestMemoryRouter> |
| 178 | +); |
0 commit comments