Skip to content
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

reference field-custom-filter-api (PR from TinaCMS) #2229

Merged
merged 20 commits into from
Oct 3, 2024
Merged
Changes from 15 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion content/docs/reference/types/reference.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: The "reference" field
last_edited: '2024-09-12T01:50:26.823Z'
last_edited: '2024-10-03T00:46:00.053Z'
next: ''
previous: ''
---
Expand Down Expand Up @@ -141,6 +141,69 @@ You can search your reference with reference selector.
}
```

### Filter reference

If you have a long list of reference items, you can filter them in the schema using the `ui.collectionFilter` field with any property you defined in the referenced collection.
Ben0189 marked this conversation as resolved.
Show resolved Hide resolved

> Note: Current `collectionFilter`only support property of type string and **case sensitive.** ( e.g. Using the example below, If you enter `australia` it will not work)).
Ben0189 marked this conversation as resolved.
Show resolved Hide resolved

> Senario: You have a list of author in different location, and you only want the reference select in your post collection to show the desired location.
Ben0189 marked this conversation as resolved.
Show resolved Hide resolved

```typescript
const referenceField = {
label: 'Author',
name: 'author',
type: 'reference',
ui: {
collectionFilter: {
author: {
location: 'Australia'
}
},
},
collections: ['author'],
}
```

It's also possible to filter references options based on a list of values.

Ben0189 marked this conversation as resolved.
Show resolved Hide resolved
```typescript
const referenceField = {
label: 'Author',
name: 'author',
type: 'reference',
ui: {
collectionFilter: {
author: {
location: ['Australia','United States']
}
},
},
collections: ['author'],
}
```

Reference fields on multiple collections can be filtered by the values in either collection.

Ben0189 marked this conversation as resolved.
Show resolved Hide resolved
```typescript
const referenceField = {
label: 'Author & Post',
name: 'author and post',
type: 'reference',
ui: {
collectionFilter: {
author: {
location: ['Australia','United States']
},
post: {
status: 'published',
},
},
Ben0189 marked this conversation as resolved.
Show resolved Hide resolved
},
collections: ['author', 'post'],
}
```

### Customizing Reference Selector with optionComponent

The default reference selector displays the file path, there are cases where you may want to customize what is displayed in the dropdown to provide a better user experience. For example, showing the author's name instead of the file name can make the selection process more intuitive.
Expand Down
Loading