Skip to content

Commit

Permalink
DataViews: Unify layout config (#67477)
Browse files Browse the repository at this point in the history
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: jorgefilipecosta <[email protected]>
Co-authored-by: oandregal <[email protected]>
Co-authored-by: jameskoster <[email protected]>
Co-authored-by: lsl <[email protected]>
Co-authored-by: ntsekouras <[email protected]>
  • Loading branch information
7 people authored Dec 6, 2024
1 parent d59faff commit 6b16c73
Show file tree
Hide file tree
Showing 22 changed files with 843 additions and 954 deletions.
15 changes: 15 additions & 0 deletions packages/dataviews/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

## Unreleased

## Breaking Changes

- Support showing or hiding title, media and description fields ([#67477](https://github.com/WordPress/gutenberg/pull/67477)).
- Unify the `title`, `media` and `description` fields for the different layouts. So instead of the previous `view.layout.mediaField`, `view.layout.primaryField` and `view.layout.columnFields`, all the layouts now support these three fields with the following config ([#67477](https://github.com/WordPress/gutenberg/pull/67477)):

```js
const view = {
type: 'table',
titleField: 'title',
mediaField: 'media',
descriptionField: 'description',
fields: [ 'author', 'date' ],
}
```

## Internal

- Upgraded `@ariakit/react` (v0.4.13) and `@ariakit/test` (v0.4.5) ([#65907](https://github.com/WordPress/gutenberg/pull/65907)).
Expand Down
53 changes: 14 additions & 39 deletions packages/dataviews/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ const view = {
field: 'date',
direction: 'desc',
},
titleField: 'title',
fields: [ 'author', 'status' ],
layout: {},
};
Expand All @@ -184,50 +185,22 @@ Properties:
- `field`: the field used for sorting the dataset.
- `direction`: the direction to use for sorting, one of `asc` or `desc`.
- `fields`: a list of field `id` that are visible in the UI and the specific order in which they are displayed.
- `titleField`: The id of the field reprensenting the title of the record.
- `mediaField`: The id of the field reprensenting the media of the record.
- `descriptionField`: The id of field the reprensenting the description of the record.
- `showTitle`: Whether the title should be shown in the UI. `true` by default.
- `showMedia`: Whether the media should be shown in the UI. `true` by default.
- `showDescription`: Whether the description should be shown in the UI. `true` by default.
- `fields`: a list of remaining field `id` that are visible in the UI and the specific order in which they are displayed.
- `layout`: config that is specific to a particular layout type.
##### Properties of `layout`
| Properties of `layout` | Table | Grid | List |
| --------------------------------------------------------------------------------------------------------------- | ----- | ---- | ---- |
| `primaryField`: the field's `id` to be highlighted in each layout. It's not hidable. | ✓ | ✓ | ✓ |
| `mediaField`: the field's `id` to be used for rendering each card's media. It's not hiddable. | | ✓ | ✓ |
| `columnFields`: a list of field's `id` to render vertically stacked instead of horizontally (the default). | | ✓ | |
| `badgeFields`: a list of field's `id` to render without label and styled as badges. | | ✓ | |
| `combinedFields`: a list of "virtual" fields that are made by combining others. See "Combining fields" section. | ✓ | | |
| `styles`: additional `width`, `maxWidth`, `minWidth` styles for each field column. | ✓ | | |
##### Combining fields
The `table` layout has the ability to create "virtual" fields that are made out by combining existing ones.
Each "virtual field", has to provide an `id` and `label` (optionally a `header` instead), which have the same meaning as any other field.
Additionally, they need to provide:
- `children`: a list of field's `id` to combine
- `direction`: how should they be stacked, `vertical` or `horizontal`
For example, this is how you'd define a `site` field which is a combination of a `title` and `description` fields, which are not displayed:
```js
{
fields: [ 'site', 'status' ],
layout: {
combinedFields: [
{
id: 'site',
label: 'Site',
children: [ 'title', 'description' ],
direction: 'vertical',
}
]
}
}
```
#### `onChangeView`: `function`

Callback executed when the view has changed. It receives the new view object as a parameter.
Expand Down Expand Up @@ -255,6 +228,7 @@ function MyCustomPageTable() {
value: [ 'publish', 'draft' ],
},
],
titleField: 'title',
fields: [ 'author', 'status' ],
layout: {},
} );
Expand Down Expand Up @@ -370,14 +344,15 @@ For example, this is how you'd enable only the table view type:
```js
const defaultLayouts = {
table: {
layout: {
primaryField: 'my-key',
},
showMedia: false,
},
grid: {
showMedia: true,
}
};
```
The `defaultLayouts` property should be an object that includes properties named `table`, `grid`, or `list`. Each of these properties should contain a `layout` property, which holds the configuration for each specific layout type. Check "Properties of layout" for the full list of properties available for each layout's configuration
The `defaultLayouts` property should be an object that includes properties named `table`, `grid`, or `list`. These properties are applied to the view object each time the user switches to the corresponding layout.
#### `selection`: `string[]`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface DataViewsSelectionCheckboxProps< Item > {
onChangeSelection: SetSelection;
item: Item;
getItemId: ( item: Item ) => string;
primaryField?: Field< Item >;
titleField?: Field< Item >;
disabled: boolean;
}

Expand All @@ -24,19 +24,19 @@ export default function DataViewsSelectionCheckbox< Item >( {
onChangeSelection,
item,
getItemId,
primaryField,
titleField,
disabled,
}: DataViewsSelectionCheckboxProps< Item > ) {
const id = getItemId( item );
const checked = ! disabled && selection.includes( id );
let selectionLabel;
if ( primaryField?.getValue && item ) {
if ( titleField?.getValue && item ) {
// eslint-disable-next-line @wordpress/valid-sprintf
selectionLabel = sprintf(
checked
? /* translators: %s: item title. */ __( 'Deselect item: %s' )
: /* translators: %s: item title. */ __( 'Select item: %s' ),
primaryField.getValue( { item } )
titleField.getValue( { item } )
);
} else {
selectionLabel = checked
Expand Down
Loading

0 comments on commit 6b16c73

Please sign in to comment.