-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(widget-relation): target file collections #3754
Conversation
@erezrokah Using I planed to query all entries in a file collections then filter them by slug in What I ended up doing is this: const file = fields.get('file');
const queryPromise = file
? query(forID, collection, ['slug'], file) // get a single entry with exact match
: query(forID, collection, searchFieldsArray, term); I'm also temporary using # target a simple list
- label: Tags
name: tags
widget: relation
collection: settings
file: general
searchFields: simpleList
# target an object list
- label: Categories
name: categories
widget: relation
collection: settings
file: general
searchFields: objectList
valueField: id
displayFields: '{{id}} | {{description}}'
# somewhere else...
collections:
- name: settings
files:
- name: general
fields:
- { label: 'Tags', name: 'simpleList', widget: 'list' }
- label: Categories,
name: objectList
widget: list
fields:
- { label: 'id', name: 'id', widget: 'string' }
- { label: 'desc', name: 'description', widget: 'string' }
Overall I prefer this over loadEntry, since it's much more inline with the current relation widget. |
@@ -187,7 +205,8 @@ export default class RelationControl extends React.Component { | |||
const isClearable = !field.get('required', true) || isMultiple; | |||
|
|||
const hits = queryHits.get(forID, []); | |||
const options = this.allOptions || this.parseHitOptions(hits); | |||
const options = | |||
this.allOptions || this.parseHitOptions(hits, { isFile: Boolean(field.get('file')) }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah oops this is from a previous experiment, I'll delete this code
label: labelReturn, | ||
}; | ||
}); | ||
let data = hits; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variable naming here is aweful, but I have a hard time coming up with something better. Perhaps this whole block could be rewritten to be better
}); | ||
let data = hits; | ||
if (field.get('file')) { | ||
data = get(hits, `[0].data.${searchFields}`, []).map(item => ({ data: item })); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I understand why the field that will be searched for in the file collection is assumed to be an array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, I should add an array check here, thanks for spotting this!
8c4e509
to
90a143a
Compare
@d4rekanguok, I rebased the branch and added a commit with the collections:
- name: posts
label: Posts
label_singular: 'Post'
folder: content/posts
create: true
fields:
- label: Title
name: title
widget: string
- label: Relation
name: relation
widget: relation
collection: files
valueField: categories.*.id
searchFields: [categories.*.name] # not really used
displayFields: [categories.*.name]
file: categories
- name: files
label: Files
label_singular: 'File'
files:
- label: Categories
name: categories
file: _data/_cats.json
fields:
- label: Categories
name: categories
widget: list
allow_add: true
fields:
- label: Name
name: name
widget: string
- label: Id
name: id
widget: string This keeps the same semantics for the fields, and allows nesting in a generic way. |
@@ -128,24 +155,26 @@ export default class RelationControl extends React.Component { | |||
parseHitOptions = hits => { | |||
const { field } = this.props; | |||
const valueField = field.get('valueField'); | |||
const displayField = field.get('displayFields') || field.get('valueField'); | |||
const displayField = field.get('displayFields') || List(field.get('valueField')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sure displayField
is always a list to avoid checking it later like https://github.com/netlify/netlify-cms/pull/3754/files#diff-1c6e2a499213935b7c1b04dbb4e1ca1cL135
Neat, yes this looks great! |
.join(' '); | ||
const value = this.parseNestedFields(hit, valuesPaths[i]); | ||
acc.push({ data: hit.data, value, label }); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh this is really neat 👍
(This PR continues the discussion in #3717 & may replace it)
Summary
Allowing relation widget to target file collections, using
query
as suggested by @erezrokahTest plan
A picture of a cute animal (not mandatory but encouraged)
closes #800
closes #3717