-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
338ae24
commit 96305e9
Showing
8 changed files
with
280 additions
and
3 deletions.
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"; |
96305e9
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.
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:
/test/e2e/specs/site-editor/font-library.spec.js