-
Notifications
You must be signed in to change notification settings - Fork 4.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
Add initial docs for the DataViews
component
#55435
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# DataView | ||
|
||
This file aims to document the main APIs related to the DataView component. | ||
|
||
## View | ||
|
||
The view is responsible for configuring how the dataset is visible to the user. For example: | ||
|
||
```js | ||
{ | ||
type: 'list', | ||
page: 1, | ||
perPage: 5, | ||
sort: { | ||
field: 'date', | ||
direction: 'desc', | ||
}, | ||
filters: { | ||
search: '', | ||
author: 2, | ||
status: 'publish, draft' | ||
}, | ||
visibleFilters: [ 'search', 'author', 'status' ], | ||
hiddenFields: [ 'date', 'featured-image' ], | ||
layout: {}, | ||
} | ||
``` | ||
|
||
- `type`: one of `list` or `grid`. | ||
- `page`: the current page. | ||
- `perPage`: number of records per page. | ||
- `sort.field`: field used for sorting. | ||
- `sort.direction`: one of `asc` or `desc`. | ||
- `filters`: the filters applied to the dataset. | ||
- `visibleFilters`: the `id` of the filters that are visible in the UI. | ||
- `hiddenFields`: the `id` of the fields that are hidden in the UI. | ||
- `layout`: ... | ||
|
||
The view configuration is used to retrieve the corresponding entity that holds the dataset: | ||
|
||
```js | ||
const { | ||
records: pages, | ||
isLoading: isLoadingPages, | ||
totalItems, | ||
totalPages | ||
} = useEntityRecords( 'postType', 'page', { | ||
per_page: view.perPage, | ||
page: view.page, | ||
order: view.sort?.direction, | ||
orderby: view.sort?.field | ||
...view.filters | ||
} ); | ||
``` | ||
|
||
## Fields | ||
|
||
The fields describe the dataset. For example: | ||
|
||
```js | ||
[ | ||
{ | ||
id: 'author', | ||
header: __( 'Author' ), | ||
getValue: ( { item } ) => item.author, | ||
render: ( {item} ) => { | ||
return ( | ||
<a href="...">{ item.author }</a> | ||
); | ||
}, | ||
elements: [ | ||
{ value: 1, label: 'admin' } | ||
{ value: 2, label: 'user' } | ||
] | ||
filters: [ | ||
{ id: 'author', type: 'enumeration' }, | ||
{ id: 'author_search', type: 'search' } | ||
], | ||
}, | ||
] | ||
``` | ||
|
||
- `id`: identifier for the field. Unique. | ||
- `header`: the field name for the UI. | ||
- `getValue`: function that returns the value of the field. | ||
- `render`: function that renders the field. | ||
- `elements`: a set of valid values for the field. | ||
- `filters`: what filters are available. A filter is an object with `id` and `type` as properties: | ||
- `id`: unique identifier for the filter. Matches the entity query param. | ||
- `type`: the type of filter. One of `search` or `enumeration`. | ||
|
||
|
||
## DataViews | ||
|
||
The UI component responsible for rendering the dataset. | ||
|
||
```js | ||
<DataViews | ||
data={ pages } | ||
isLoading= { isLoadingPages } | ||
fields={ fields } | ||
view={ view } | ||
onChangeView={ onChangeView } | ||
actions={ [ trashPostAction ] } | ||
paginationInfo={ { totalItems, totalPages } } | ||
/> | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do you think we should also add the type definitions for the interfaces used in the main component or somewhere, now?
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.
At some point, it'd be good to have them, sure.
Right now, my main concern is to have a high-level map of the code, so to speak. I'd like to give people a little orientation about how to navigate the hundreds of lines of code. Writing things down is how I think, so I am biased towards believing this may be helpful to some others as well.