Newer
Older
import * as React from "react";
import {
Admin,
Resource,
List,
Datagrid,
Filter,
FilterProps,
SearchInput,
TextField,
NumberField,
Edit,
SimpleForm,
TextInput,
NumberInput,
AutocompleteArrayInput,
ReferenceArrayInput,
FunctionField,
SingleFieldList,
} from "react-admin";
import { client } from "@logilab/cwclientlibjs";
import { formHelpers } from "./formHelpers";
<Show {...props}>
<SimpleShowLayout>
<TextField source="id" />
<TextField source="name" />
<TextField source="postal_address" />
<ReferenceField label="City" link="show" reference="City" source="is_in">
<TextField source="name" />
</ReferenceField>
<ReferenceArrayField
label="Director"
reference="Person"
source="director"
>
<SingleFieldList>
<FunctionField<{ id: string; name: string }>
render={(person) => person && <Chip label={`${person.name}`} />}
/>
</SingleFieldList>
</ReferenceArrayField>
<NumberField source="longitude" />
<NumberField source="latitude" />
</SimpleShowLayout>
</Show>
);
const MuseumEdit = (props: EditProps) => {
const inputProps = formHelpers["Museum"].getInputProps;
return (
<Edit {...props}>
<SimpleForm>
<NumberField source="id" />
<TextInput {...inputProps("name")} />
<NumberInput {...inputProps("latitude")} />
<NumberInput {...inputProps("longitude")} />
<TextInput {...inputProps("postal_address")} />
</SimpleForm>
</Edit>
);
};
const MuseumCreate = (props: CreateProps) => {
const inputProps = formHelpers["Museum"].getInputProps;
return (
<Create {...props}>
<SimpleForm>
<TextInput {...inputProps("name")} />
<NumberInput {...inputProps("latitude")} />
<NumberInput {...inputProps("longitude")} />
<TextInput {...inputProps("postal_address")} />
<ReferenceInput
label="City"
source="is_in"
reference="City"
sort={{ field: "name", order: "ASC" }}
filterToQuery={(text: string) => ({ name: text })}
>
<AutocompleteInput optionText="name" />
</ReferenceInput>
<ReferenceArrayInput
label="Director"
source="director"
reference="Person"
sort={{ field: "name", order: "ASC" }}
filterToQuery={(text: string) => ({ name: text })}
>
<AutocompleteArrayInput optionText="name" />
</ReferenceArrayInput>
</SimpleForm>
</Create>
);
};
const MuseumFilter = (props: Omit<FilterProps, "children">) => (
<Filter {...props}>
<SearchInput source="name" alwaysOn />
</Filter>
);
<List {...props} filters={<MuseumFilter />}>
<NumberField source="id" />
<TextField source="name" />
<NumberField source="latitude" />
<NumberField source="longitude" />
<TextField source="postal_address" />
</Datagrid>
</List>
);
<List {...props}>
<Datagrid rowClick="show">
<TextField source="name" />
</Datagrid>
</List>
);
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const CityEdit = (props: ListProps) => {
const inputProps = formHelpers["City"].getInputProps;
return (
<Edit {...props}>
<SimpleForm>
<TextInput {...inputProps("name")} />
<NumberInput {...inputProps("zip_code")} />
<TextInput {...inputProps("country")} />
</SimpleForm>
</Edit>
);
};
const CityCreate = (props: ListProps) => {
const inputProps = formHelpers["City"].getInputProps;
return (
<Create {...props}>
<SimpleForm>
<TextInput {...inputProps("name")} />
<NumberInput {...inputProps("zip_code")} />
<TextInput {...inputProps("country")} />
</SimpleForm>
</Create>
);
};
<Show {...props}>
<SimpleShowLayout>
<TextField source="name" />
<ReferenceArrayField
label="Museum"
reference="Museum"
source="reverse_is_in"
>
</Datagrid>
</ReferenceArrayField>
</SimpleShowLayout>
</Show>
);
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
const PersonList = (props: ListProps) => (
<List {...props}>
<Datagrid rowClick="show">
<TextField source="id" />
<TextField source="name" />
<TextField source="email" />
</Datagrid>
</List>
);
const PersonEdit = (props: ListProps) => {
const inputProps = formHelpers["Person"].getInputProps;
return (
<Edit {...props}>
<SimpleForm>
<TextInput {...inputProps("name")} />
<TextInput {...inputProps("email")} />
</SimpleForm>
</Edit>
);
};
const PersonCreate = (props: ListProps) => {
const inputProps = formHelpers["Person"].getInputProps;
return (
<Create {...props}>
<SimpleForm>
<TextInput {...inputProps("name")} />
<TextInput {...inputProps("email")} />
</SimpleForm>
</Create>
);
};
const PersonShow = (props: ShowProps) => (
<Show {...props}>
<SimpleShowLayout>
<TextField source="id" />
<TextField source="name" />
<TextField source="email" />
<ReferenceArrayField
label="Museum"
reference="Museum"
source="reverse_director"
>
<Datagrid rowClick="show">
<TextField source="name" />
</Datagrid>
</ReferenceArrayField>
</SimpleShowLayout>
</Show>
);
const httpClient = new client.CwSimpleHttpClient("http://localhost:8080", true);
const rqlClient = new client.CwRqlClient(httpClient);
const dataProvider = createDataProvider(rqlClient, schema);
const App = (): JSX.Element => (
{
// authProvider={authProvider}
// use cwclientlibjs to get list of CWETypes
// and add one <Resource per type
}
<Resource
name="City"
list={CityList}
show={CityShow}
edit={CityEdit}
create={CityCreate}
/>
<Resource
name="Person"
list={PersonList}
show={PersonShow}
edit={PersonEdit}
create={PersonCreate}
/>
</Admin>
);
export default App;