Skip to content

Commit

Permalink
Document filterSortAndPaginate & isItemValid utilities (#66738)
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal authored Nov 5, 2024
1 parent 6270af6 commit 2da866b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
31 changes: 31 additions & 0 deletions packages/dataviews/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,37 @@ Same as `fields` property of `DataViews`.
- `type`: either `regular` or `panel`.
- `fields`: a list of fields ids that should be rendered.

## Utilities

### `filterSortAndPaginate`

Utility to apply the view config (filters, search, sorting, and pagination) to a dataset client-side.

Parameters:

- `data`: the dataset, as described in the "data" property of DataViews.
- `view`: the view config, as described in the "view" property of DataViews.
- `fields`: the fields config, as described in the "fields" property of DataViews.

Returns an object containing:

- `data`: the new dataset, with the view config applied.
- `paginationInfo`: object containing the following properties:
- `totalItems`: total number of items for the current view config.
- `totalPages`: total number of pages for the current view config.

### `isItemValid`

Utility to determine whether or not the given item's value is valid according to the current fields and form config.

Parameters:

- `item`: the item, as described in the "data" property of DataForm.
- `fields`: the fields config, as described in the "fields" property of DataForm.
- `form`: the form config, as described in the "form" property of DataForm.

Returns a boolean indicating if the item is valid (true) or not (false).

## Contributing to this package

This is an individual package that's part of the Gutenberg project. The project is organized as a monorepo. It's made up of multiple self-contained software packages, each with a specific purpose. The packages in this monorepo are published to [npm](https://www.npmjs.com/) and used by [WordPress](https://make.wordpress.org/core/) as well as other software projects.
Expand Down
9 changes: 9 additions & 0 deletions packages/dataviews/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
import { normalizeFields } from './normalize-fields';
import type { Field, Form } from './types';

/**
* Whether or not the given item's value is valid according to the fields and form config.
*
* @param item The item to validate.
* @param fields Fields config.
* @param form Form config.
*
* @return A boolean indicating if the item is valid (true) or not (false).
*/
export function isItemValid< Item >(
item: Item,
fields: Field< Item >[],
Expand Down

0 comments on commit 2da866b

Please sign in to comment.