Skip to content

Commit

Permalink
DataViews: Support defining field headers/names as React elements
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Aug 20, 2024
1 parent 07c2a8e commit 5b2a41a
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 6 deletions.
4 changes: 4 additions & 0 deletions packages/dataviews/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

## New features

- Support using a component for field headers or names by providing a `header` property in the field object. The string `label` property (or `id`) is still mandatory.

## 4.1.0 (2024-08-07)

## Internal
Expand Down
14 changes: 13 additions & 1 deletion packages/dataviews/src/components/dataviews/stories/fixtures.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { trash } from '@wordpress/icons';
import { trash, image, Icon, category } from '@wordpress/icons';
import {
Button,
__experimentalText as Text,
Expand Down Expand Up @@ -173,6 +173,12 @@ export const fields = [
{
label: 'Image',
id: 'image',
header: (
<HStack justify="start">
<Icon icon={ image } />
<span>Image</span>
</HStack>
),
render: ( { item } ) => {
return (
<img src={ item.image } alt="" style={ { width: '100%' } } />
Expand Down Expand Up @@ -217,6 +223,12 @@ export const fields = [
{
label: 'Categories',
id: 'categories',
header: (
<HStack justify="start">
<Icon icon={ category } />
<span>Categories</span>
</HStack>
),
elements: [
{ value: 'Space', label: 'Space' },
{ value: 'NASA', label: 'NASA' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useState, useMemo } from '@wordpress/element';
*/
import DataViews from '../index';
import { DEFAULT_VIEW, actions, data, fields } from './fixtures';
import { LAYOUT_GRID, LAYOUT_TABLE } from '../../../constants';
import { LAYOUT_GRID, LAYOUT_LIST, LAYOUT_TABLE } from '../../../constants';
import { filterSortAndPaginate } from '../../../filter-and-sort-data-view';

const meta = {
Expand Down Expand Up @@ -61,5 +61,11 @@ Default.args = {
primaryField: 'title',
},
},
[ LAYOUT_LIST ]: {
layout: {
mediaField: 'image',
primaryField: 'title',
},
},
},
};
2 changes: 1 addition & 1 deletion packages/dataviews/src/dataviews-layouts/grid/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ function GridItem< Item >( {
>
<>
<FlexItem className="dataviews-view-grid__field-name">
{ field.label }
{ field.header }
</FlexItem>
<FlexItem
className="dataviews-view-grid__field-value"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const _HeaderMenu = forwardRef( function HeaderMenu< Item >(
);
const index = view.fields?.indexOf( fieldId ) as number;
if ( !! combinedField ) {
return combinedField.label;
return combinedField.header || combinedField.label;
}
const field = fields.find( ( f ) => f.id === fieldId );
if ( ! field ) {
Expand All @@ -92,7 +92,7 @@ const _HeaderMenu = forwardRef( function HeaderMenu< Item >(
!! operators.length &&
! field.filterBy?.isPrimary;
if ( ! isSortable && ! isHidable && ! canAddFilter ) {
return field.label;
return field.header;
}
return (
<DropdownMenu
Expand All @@ -104,7 +104,7 @@ const _HeaderMenu = forwardRef( function HeaderMenu< Item >(
ref={ ref }
variant="tertiary"
>
{ field.label }
{ field.header }
{ view.sort && isSorted && (
<span aria-hidden="true">
{ sortArrows[ view.sort.direction ] }
Expand Down
1 change: 1 addition & 0 deletions packages/dataviews/src/normalize-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export function normalizeFields< Item >(
return {
...field,
label: field.label || field.id,
header: field.header || field.label || field.id,
getValue,
render,
sort,
Expand Down
9 changes: 9 additions & 0 deletions packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ export type Field< Item > = {
*/
label?: string;

/**
* The header of the field. Defaults to the label.
* It allows the usage of a React Element to render the field labels.
*/
header?: string | ReactElement;

/**
* A description of the field.
*/
Expand Down Expand Up @@ -151,6 +157,7 @@ export type Field< Item > = {

export type NormalizedField< Item > = Field< Item > & {
label: string;
header: string | ReactElement;
getValue: ( args: { item: Item } ) => any;
render: ComponentType< { item: Item } >;
Edit: ComponentType< DataFormControlProps< Item > >;
Expand Down Expand Up @@ -289,6 +296,8 @@ export interface CombinedField {

label: string;

header?: string | ReactElement;

/**
* The fields to use as columns.
*/
Expand Down

0 comments on commit 5b2a41a

Please sign in to comment.