Skip to content

Commit e9f0d86

Browse files
committed
Update react code with latest way to do
1 parent d703c93 commit e9f0d86

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

admin/getting-started.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const myApiDocumentationParser = entrypoint => parseHydraDocumentation(entrypoin
9191
addField: true,
9292
addLabel: true
9393
};
94-
94+
9595
return { api };
9696
})
9797
;
@@ -126,7 +126,7 @@ const myApiDocumentationParser = entrypoint => parseHydraDocumentation(entrypoin
126126
.then( ({ api }) => {
127127
128128
const books = api.resources.find(({ name }) => 'books' === name);
129-
const description = books.fields.find(f => 'description' === f.name);
129+
const description = books.fields.find(({ name }) => 'description' === name);
130130
131131
description.input = props => (
132132
<RichTextInput {...props} source="description" />
@@ -194,11 +194,11 @@ __Note__: In this example, we choose to send the file via a multi-part form data
194194

195195
### Using a Custom Validation Function or Inject Custom Props
196196

197-
You can use `fieldProps` and `inputProps` to respectively inject custom properties to fields and inputs generated by API
198-
Platform Admin. This is particularly useful to add custom validation rules:
197+
Example to add a minLength validator on the `description` field:
199198

200199
```javascript
201200
import React, { Component } from 'react';
201+
import { minLength, RichTextInput } from 'react-admin';
202202
import { AdminBuilder, hydraClient } from '@api-platform-admin';
203203
import parseHydraDocumentation from '@api-platform/api-doc-parser/lib/hydra/parseHydraDocumentation';
204204

@@ -208,12 +208,13 @@ export default class extends Component {
208208
state = { api: null };
209209

210210
componentDidMount() {
211-
parseHydraDocumentation(entrypoint).then( ({ api }) => {
212-
const books = api.resources.find(r => 'books' === r.name);
211+
parseHydraDocumentation(entrypoint).then(({ api }) => {
212+
const books = api.resources.find(({ name }) => 'books' === name);
213213

214-
books.writableFields.find(f => 'description' === f.name).inputProps = {
215-
validate: value => value.length >= 30 ? undefined : 'Minimum length: 30';
216-
};
214+
const description = books.fields.find(({ name }) => 'description' === name)
215+
description.input = props => (
216+
<RichTextInput source={description.name} label="Description" validate={minLength(30)} {...props} />
217+
)
217218

218219
this.setState({ api });
219220
});

admin/handling-relations-to-collections.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,27 +87,28 @@ import parseHydraDocumentation from '@api-platform/api-doc-parser/lib/hydra/pars
8787
const entrypoint = 'https://demo.api-platform.com';
8888

8989
export default class extends Component {
90-
state = { api: null };
90+
state = { api: null }
9191

9292
componentDidMount() {
93-
parseHydraDocumentation(entrypoint).then( ({api}) => {
94-
const books = api.resources.find(r => 'books' === r.name);
93+
parseHydraDocumentation(entrypoint).then(({api}) => {
94+
const books = api.resources.find(({ name }) => 'books' === name)
95+
const authors = books.fields.find(({ name }) => 'authors' === name)
9596

9697
// Set the field in the list and the show views
97-
books.readableFields.find(f => 'authors' === f.name).fieldComponent =
98-
<ReferenceArrayField label="Authors" reference="people" source="authors" key="authors">
98+
authors.field = props => (
99+
<ReferenceArrayField source={authors.name} reference={authors.reference.name} key={authors.name} {...props}>
99100
<SingleFieldList>
100101
<ChipField source="name" key="name"/>
101102
</SingleFieldList>
102103
</ReferenceArrayField>
103-
;
104+
);
104105

105106
// Set the input in the edit and create views
106-
books.writableFields.find(f => 'authors' === f.name).inputComponent =
107-
<ReferenceArrayInput label="Authors" reference="people" source="authors" key="authors">
107+
authors.input = props => (
108+
<ReferenceArrayInput source={authors.name} reference={authors.reference.name} label="Authors" key={authors.name} {...props} allowEmpty>
108109
<SelectArrayInput optionText="name"/>
109110
</ReferenceArrayInput>
110-
;
111+
);
111112

112113
this.setState({ api });
113114
}
@@ -162,11 +163,11 @@ Then edit the configuration of API Platform Admin to pass a `filterToQuery` prop
162163
// ...
163164

164165
// Set the input in the edit and create views
165-
books.writableFields.find(f => 'authors' === f.name).inputComponent =
166-
<ReferenceArrayInput label="Authors" reference="people" source="authors" key="authors" filterToQuery={searchText => ({ name: searchText })}>
167-
<SelectArrayInput optionText="name"/>
168-
</ReferenceArrayInput>
169-
;
166+
authors.input = props => (
167+
<ReferenceArrayInput source={authors.name} reference={authors.reference.name} label="Authors" key={authors.name} filterToQuery={searchText => ({ name: searchText })} {...props} allowEmpty>
168+
<SelectArrayInput optionText="name"/>
169+
</ReferenceArrayInput>
170+
);
170171

171172
// ...
172173
}

0 commit comments

Comments
 (0)