Skip to content

Commit

Permalink
DataViews: Add story (#56761)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Dec 5, 2023
1 parent 338ae24 commit 96305e9
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 3 deletions.
126 changes: 126 additions & 0 deletions packages/dataviews/src/stories/fixtures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/**
* WordPress dependencies
*/
import { trash } from '@wordpress/icons';
import {
Button,
__experimentalText as Text,
__experimentalHStack as HStack,
__experimentalVStack as VStack,
} from '@wordpress/components';

/**
* Internal dependencies
*/
import { LAYOUT_TABLE } from '../constants';

export const data = [
{
id: 1,
title: 'Apollo',
description: 'Apollo description',
image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
},
{
id: 2,
title: 'Space',
description: 'Space description',
image: 'https://live.staticflickr.com/5678/21911065441_92e2d44708_b.jpg',
},
{
id: 3,
title: 'NASA',
description: 'NASA photo',
image: 'https://live.staticflickr.com/742/21712365770_8f70a2c91e_b.jpg',
},
{
id: 4,
title: 'Neptune',
description: 'Neptune description',
image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
},
{
id: 5,
title: 'Mercury',
description: 'Mercury description',
image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
},
{
id: 6,
title: 'Venus',
description: 'Venus description',
image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
},
{
id: 7,
title: 'Earth',
description: 'Earth description',
image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
},
{
id: 8,
title: 'Mars',
description: 'Mars description',
image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
},
{
id: 9,
title: 'Jupiter',
description: 'Jupiter description',
image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
},
{
id: 10,
title: 'Saturn',
description: 'Saturn description',
image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
},
{
id: 11,
title: 'Uranus',
description: 'Uranus description',
image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
},
];

export const DEFAULT_VIEW = {
type: LAYOUT_TABLE,
search: '',
page: 1,
perPage: 10,
hiddenFields: [ 'image' ],
layout: {},
filters: [],
};

export const actions = [
{
id: 'delete',
label: 'Delete item',
isPrimary: true,
icon: trash,
hideModalHeader: true,
RenderModal: ( { item, closeModal } ) => {
return (
<VStack spacing="5">
<Text>
{ `Are you sure you want to delete "${ item.title }"?` }
</Text>
<HStack justify="right">
<Button variant="tertiary" onClick={ closeModal }>
Cancel
</Button>
<Button variant="primary" onClick={ closeModal }>
Delete
</Button>
</HStack>
</VStack>
);
},
},
{
id: 'secondary',
label: 'Secondary action',
callback() {},
},
];
137 changes: 137 additions & 0 deletions packages/dataviews/src/stories/index.story.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/**
* WordPress dependencies
*/
import { useState, useMemo, useCallback } from '@wordpress/element';

/**
* Internal dependencies
*/
import { DataViews, LAYOUT_GRID, LAYOUT_TABLE } from '../index';

import { DEFAULT_VIEW, actions, data } from './fixtures';

const meta = {
title: 'DataViews (Experimental)/DataViews',
component: DataViews,
};
export default meta;

const defaultConfigPerViewType = {
[ LAYOUT_TABLE ]: {},
[ LAYOUT_GRID ]: {
mediaField: 'image',
primaryField: 'title',
},
};

function normalizeSearchInput( input = '' ) {
return input.trim().toLowerCase();
}

const fields = [
{
header: 'Image',
id: 'image',
render: ( { item } ) => {
return (
<img src={ item.image } alt="" style={ { width: '100%' } } />
);
},
width: 50,
enableSorting: false,
},
{
header: 'Title',
id: 'title',
getValue: ( { item } ) => item.title,
maxWidth: 400,
enableHiding: false,
},
{
header: 'Description',
id: 'description',
getValue: ( { item } ) => item.description,
maxWidth: 200,
enableSorting: false,
},
];

export const Default = ( props ) => {
const [ view, setView ] = useState( DEFAULT_VIEW );
const { shownData, paginationInfo } = useMemo( () => {
let filteredData = [ ...data ];
// Handle global search.
if ( view.search ) {
const normalizedSearch = normalizeSearchInput( view.search );
filteredData = filteredData.filter( ( item ) => {
return [
normalizeSearchInput( item.title ),
normalizeSearchInput( item.description ),
].some( ( field ) => field.includes( normalizedSearch ) );
} );
}
// Handle sorting.
if ( view.sort ) {
const stringSortingFields = [ 'title' ];
const fieldId = view.sort.field;
if ( stringSortingFields.includes( fieldId ) ) {
const fieldToSort = fields.find( ( field ) => {
return field.id === fieldId;
} );
filteredData.sort( ( a, b ) => {
const valueA = fieldToSort.getValue( { item: a } ) ?? '';
const valueB = fieldToSort.getValue( { item: b } ) ?? '';
return view.sort.direction === 'asc'
? valueA.localeCompare( valueB )
: valueB.localeCompare( valueA );
} );
}
}
// Handle pagination.
const start = ( view.page - 1 ) * view.perPage;
const totalItems = filteredData?.length || 0;
filteredData = filteredData?.slice( start, start + view.perPage );
return {
shownData: filteredData,
paginationInfo: {
totalItems,
totalPages: Math.ceil( totalItems / view.perPage ),
},
};
}, [ view ] );
const onChangeView = useCallback(
( viewUpdater ) => {
let updatedView =
typeof viewUpdater === 'function'
? viewUpdater( view )
: viewUpdater;
if ( updatedView.type !== view.type ) {
updatedView = {
...updatedView,
layout: {
...defaultConfigPerViewType[ updatedView.type ],
},
};
}

setView( updatedView );
},
[ view, setView ]
);
return (
<DataViews
{ ...props }
paginationInfo={ paginationInfo }
data={ shownData }
view={ view }
fields={ fields }
onChangeView={ onChangeView }
/>
);
};
Default.args = {
actions,
getItemId: ( item ) => item.id,
isLoading: false,
supportedLayouts: [ LAYOUT_TABLE, LAYOUT_GRID ],
};
3 changes: 2 additions & 1 deletion packages/dataviews/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
height: calc(100% - #{$grid-unit-40} * 2);
overflow: auto;
padding: $grid-unit-40 $grid-unit-40 0;
box-sizing: border-box;

> div {
min-height: 100%;
Expand Down Expand Up @@ -100,7 +101,7 @@
}
}

.dataviews-view-grid__title {
.dataviews-view-grid__primary-field {
min-height: $grid-unit-30;

a {
Expand Down
7 changes: 5 additions & 2 deletions packages/dataviews/src/view-grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import {
FlexBlock,
__experimentalGrid as Grid,
__experimentalHStack as HStack,
__experimentalVStack as VStack,
Expand Down Expand Up @@ -45,10 +46,12 @@ export default function ViewGrid( { data, fields, view, actions, getItemId } ) {
{ mediaField?.render( { item, view } ) }
</div>
<HStack
className="dataviews-view-grid__title"
className="dataviews-view-grid__primary-field"
justify="space-between"
>
{ primaryField?.render( { item, view } ) }
<FlexBlock>
{ primaryField?.render( { item, view } ) }
</FlexBlock>
<ItemActions
item={ item }
actions={ actions }
Expand Down
1 change: 1 addition & 0 deletions storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const stories = [
'../packages/components/src/**/stories/*.story.@(js|tsx|mdx)',
'../packages/icons/src/**/stories/*.story.@(js|tsx|mdx)',
'../packages/edit-site/src/**/stories/*.story.@(js|tsx|mdx)',
'../packages/dataviews/src/**/stories/*.story.@(js|tsx|mdx)',
].filter( Boolean );

module.exports = {
Expand Down
7 changes: 7 additions & 0 deletions storybook/package-styles/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import formatLibraryLtr from '../package-styles/format-library-ltr.lazy.scss';
import formatLibraryRtl from '../package-styles/format-library-rtl.lazy.scss';
import editSiteLtr from '../package-styles/edit-site-ltr.lazy.scss';
import editSiteRtl from '../package-styles/edit-site-rtl.lazy.scss';
import dataviewsLtr from '../package-styles/dataviews-ltr.lazy.scss';
import dataviewsRtl from '../package-styles/dataviews-rtl.lazy.scss';

/**
* Stylesheets to lazy load when the story's context.componentId matches the
Expand Down Expand Up @@ -51,6 +53,11 @@ const CONFIG = [
ltr: [ componentsLtr ],
rtl: [ componentsRtl ],
},
{
componentIdMatcher: /^dataviews-/,
ltr: [ dataviewsLtr, componentsLtr ],
rtl: [ dataviewsRtl, componentsRtl ],
},
];

export default CONFIG;
1 change: 1 addition & 0 deletions storybook/package-styles/dataviews-ltr.lazy.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import "../../packages/dataviews/build-style/style";
1 change: 1 addition & 0 deletions storybook/package-styles/dataviews-rtl.lazy.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import "../../packages/dataviews/build-style/style-rtl";

1 comment on commit 96305e9

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 96305e9.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7097434058
📝 Reported issues:

Please sign in to comment.