-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
DataViews: Add story #56761
Merged
Merged
DataViews: Add story #56761
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,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() {}, | ||
}, | ||
]; |
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,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 ], | ||
}; |
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
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
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
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
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 @@ | ||
@import "../../packages/dataviews/build-style/style"; |
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 @@ | ||
@import "../../packages/dataviews/build-style/style-rtl"; |
Oops, something went wrong.
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.
There is an issue with storybook iframe's height that results in
sticky
headers not to work properly. I'm not sure if this should be treated as storybook issue or it indicates that we should handle somehow such a use case. --cc @ciampoThere 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.
How can we handle it in a way that works everywhere (regardless of how dataviews is used)
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.
You can test the story outside of the Storybook canvas iframe (example) to understand if the iframe could be the issue here