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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
63 changes: 63 additions & 0 deletions content/docs/reference/types/reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,69 @@ You can search your reference with reference selector.
}
```

### Filter reference

If you have a long list of reference item, you can filter them in the schema before build time using `ui.collectionFilter`
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.
Copy link
Contributor

@isaaclombardssw isaaclombardssw Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
> 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.
> Senario: You have a collection of authors in different locations, and you only want the references selected from that collection to be authors from one location.


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

**Figure: **Show author at location `Australia`
Ben0189 marked this conversation as resolved.
Show resolved Hide resolved

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'],
}
```

**Figure: **Show author at location `Australia` and `United State`
Ben0189 marked this conversation as resolved.
Show resolved Hide resolved

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',
},
},
},
collections: ['author', 'post'],
}
```

**Figure: **Multiple filter for multiple referenced collection
Ben0189 marked this conversation as resolved.
Show resolved Hide resolved

### 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