Skip to content
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: Implement isItemClickable and onClickItem props #66365

Merged
merged 75 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
785085b
Page Edit View: Implement Featured image page field
gigitux Aug 14, 2024
dd7e0a3
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/page-…
gigitux Aug 14, 2024
77f2217
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/page-…
gigitux Aug 21, 2024
433e45f
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/page-…
gigitux Aug 21, 2024
f890963
improve flow and style
gigitux Aug 22, 2024
c32622e
fix list layout
gigitux Aug 22, 2024
9a39fe6
improve design
gigitux Aug 23, 2024
5419b0a
remove not necessary style
gigitux Aug 23, 2024
68ef300
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/page-…
gigitux Aug 26, 2024
cd923f8
improve style
gigitux Aug 27, 2024
94335a4
improve default image control
gigitux Aug 27, 2024
030a28d
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/page-…
gigitux Aug 27, 2024
baf7a82
fix style
gigitux Aug 29, 2024
4095dab
remove not necessary configuration
gigitux Aug 29, 2024
23d8695
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/page-…
gigitux Aug 29, 2024
ec715ed
format _z-index.scss file
gigitux Aug 29, 2024
ea29690
improve style
gigitux Aug 29, 2024
106af4d
remove not necessary code
gigitux Aug 29, 2024
cc46724
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/page-…
gigitux Aug 29, 2024
b11e923
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/page-…
gigitux Sep 9, 2024
c6dffd7
fix focus
gigitux Sep 9, 2024
3c262d6
remove image field type
gigitux Sep 9, 2024
c053a9a
add comment
gigitux Sep 9, 2024
57faa8f
fix warning
gigitux Sep 9, 2024
d2eeb0e
fix image for deleted pages
gigitux Sep 9, 2024
05f0d1a
remove filename
gigitux Sep 10, 2024
c4db3b9
add border-radius
gigitux Sep 10, 2024
cd9fe2f
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/page-…
gigitux Sep 18, 2024
8356977
migrate featured image to fields package
gigitux Sep 18, 2024
e27dec1
fix CSS
gigitux Sep 19, 2024
a187591
remove not necessary changes
gigitux Sep 19, 2024
8815e5a
fix type
gigitux Sep 19, 2024
a461d85
remove empty space
gigitux Sep 19, 2024
fcc2e6e
fix z-index gallery
gigitux Sep 19, 2024
577f836
fix overlapping and style
gigitux Sep 19, 2024
a3449c4
improve codestyle
gigitux Sep 19, 2024
2366429
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/page-…
gigitux Sep 20, 2024
1b84ac0
use same placeholder style grid/list layout
gigitux Sep 20, 2024
6ca01ff
fix tsconfig
gigitux Sep 20, 2024
8e612d4
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/page-…
gigitux Oct 22, 2024
c448c49
add type
gigitux Oct 22, 2024
2b595c2
remove mediaField from layout table
gigitux Oct 22, 2024
9cc6d80
update type
gigitux Oct 22, 2024
01a57a8
use text as type
gigitux Oct 22, 2024
7b1e40a
use media-utils package
gigitux Oct 22, 2024
6592d36
fix tsconfig
gigitux Oct 22, 2024
c2994e0
remove view prop
gigitux Oct 22, 2024
b039310
revert changes
gigitux Oct 22, 2024
42f90f4
generate package-lock.json
gigitux Oct 22, 2024
1c3bdb9
add onClick and isClickable props
gigitux Oct 23, 2024
ea8e941
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/page-…
gigitux Oct 23, 2024
1d0809a
generate package-lock.json
gigitux Oct 23, 2024
e6e3eb9
add documentation
gigitux Oct 23, 2024
09b2f79
fix naming
gigitux Oct 23, 2024
9e2cbf9
improve documentation
gigitux Oct 23, 2024
08ea4d5
Merge branch 'add/page-image-control' of github.com:gigitux/gutenberg…
gigitux Oct 23, 2024
e77b93b
generate package-lock.json
gigitux Oct 23, 2024
5bf9726
Merge branch 'add/page-image-control' of github.com:WordPress/gutenbe…
gigitux Oct 23, 2024
a8e3d75
update documentation
gigitux Oct 24, 2024
c9ab19d
fix prop name
gigitux Oct 30, 2024
9f7c57d
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/selec…
gigitux Nov 4, 2024
eb2a816
fix logic table view
gigitux Nov 4, 2024
f8d86b1
Rename isClickable to isItemClickable and onClick to onItemClick for …
gigitux Nov 4, 2024
5c7b0c4
fix style
gigitux Nov 4, 2024
f8036fd
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/selec…
gigitux Nov 5, 2024
419f3fa
rename onItemClick to onClickItem
gigitux Nov 5, 2024
7a79644
improve CSS
gigitux Nov 5, 2024
0071e09
fix docs
gigitux Nov 5, 2024
507d60b
add useClickableItemProps
gigitux Nov 5, 2024
ed48f39
use `isClickable` variable
gigitux Nov 6, 2024
c9e418b
improve code style
gigitux Nov 6, 2024
fdc2892
Address feedback about getClickableItemProps
oandregal Nov 12, 2024
84cc4c2
Remove defaults, they are already provided by the Provider
oandregal Nov 12, 2024
eaa3324
Cache empty actions
oandregal Nov 12, 2024
f24172b
Address feedback: simplify getClickableItemProps
oandregal Nov 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/dataviews/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,14 @@ Callback that signals the user selected one of more items. It receives the list

If `selection` and `onChangeSelection` are provided, the `DataViews` component behaves as a controlled component, otherwise, it behaves like an uncontrolled component.

### `isItemClickable`: `function`
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this one really necessary? I mean what happens if we just do nothing in onClickItem instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is this one really necessary? I mean what happens if we just do nothing in onClickItem instead?

This can be useful for differentiating the layout style of clickable primary fields from non-clickable ones. In the page view, the “primary” field is gray when the item is not clickable:

Screen.Capture.on.2024-11-04.at.14-52-41.mp4

(also, I just noticed that deleted pages aren't visible in the All pages view)


A function that determines if a media field or a primary field are clickable. It receives an item as an argument and returns a boolean value indicating whether the item can be clicked.

### `onClickItem`: `function`

A callback function that is triggered when a user clicks on a media field or primary field. This function is currently implemented only in the `grid` and `list` views.

#### `header`: React component

React component to be rendered next to the view config button.
Expand Down
4 changes: 4 additions & 0 deletions packages/dataviews/src/components/dataviews-context/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type DataViewsContextType< Item > = {
openedFilter: string | null;
setOpenedFilter: ( openedFilter: string | null ) => void;
getItemId: ( item: Item ) => string;
onClickItem: ( item: Item ) => void;
isItemClickable: ( item: Item ) => boolean;
density: number;
};

Expand All @@ -43,6 +45,8 @@ const DataViewsContext = createContext< DataViewsContextType< any > >( {
setOpenedFilter: () => {},
openedFilter: null,
getItemId: ( item ) => item.id,
onClickItem: () => {},
isItemClickable: () => false,
density: 0,
} );

Expand Down
4 changes: 4 additions & 0 deletions packages/dataviews/src/components/dataviews-layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export default function DataViewsLayout() {
onChangeSelection,
setOpenedFilter,
density,
onClickItem,
isItemClickable,
} = useContext( DataViewsContext );

const ViewComponent = VIEW_LAYOUTS.find( ( v ) => v.type === view.type )
Expand All @@ -44,6 +46,8 @@ export default function DataViewsLayout() {
onChangeSelection={ onChangeSelection }
selection={ selection }
setOpenedFilter={ setOpenedFilter }
onClickItem={ onClickItem }
isItemClickable={ isItemClickable }
view={ view }
density={ density }
/>
Expand Down
11 changes: 10 additions & 1 deletion packages/dataviews/src/components/dataviews/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,34 @@ type DataViewsProps< Item > = {
defaultLayouts: SupportedLayouts;
selection?: string[];
onChangeSelection?: ( items: string[] ) => void;
onClickItem?: ( item: Item ) => void;
isItemClickable?: ( item: Item ) => boolean;
header?: ReactNode;
} & ( Item extends ItemWithId
? { getItemId?: ( item: Item ) => string }
: { getItemId: ( item: Item ) => string } );

const defaultGetItemId = ( item: ItemWithId ) => item.id;
const defaultIsItemClickable = () => false;
const defaultOnClickItem = () => {};
const EMPTY_ARRAY: any[] = [];

export default function DataViews< Item >( {
view,
onChangeView,
fields,
search = true,
searchLabel = undefined,
actions = [],
actions = EMPTY_ARRAY,
data,
getItemId = defaultGetItemId,
isLoading = false,
paginationInfo,
defaultLayouts,
selection: selectionProperty,
onChangeSelection,
onClickItem = defaultOnClickItem,
isItemClickable = defaultIsItemClickable,
header,
}: DataViewsProps< Item > ) {
const [ selectionState, setSelectionState ] = useState< string[] >( [] );
Expand Down Expand Up @@ -110,6 +117,8 @@ export default function DataViews< Item >( {
openedFilter,
setOpenedFilter,
getItemId,
isItemClickable,
onClickItem,
density,
} }
>
Expand Down
12 changes: 9 additions & 3 deletions packages/dataviews/src/components/dataviews/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
position: sticky;
left: 0;
transition: padding ease-out 0.1s;
@include reduce-motion("transition");
@include reduce-motion( "transition" );
}

.dataviews-view-list__primary-field,
Expand Down Expand Up @@ -62,6 +62,13 @@
}
}

.dataviews-view-list__primary-field--clickable,
.dataviews-view-grid__primary-field--clickable,
.dataviews-view-grid__media--clickable,
.dataviews-view-table__primary-field > .dataviews-view-table__cell-content--clickable {
cursor: pointer;
}

.dataviews-no-results,
.dataviews-loading {
padding: 0 $grid-unit-60;
Expand All @@ -70,7 +77,7 @@
align-items: center;
justify-content: center;
transition: padding ease-out 0.1s;
@include reduce-motion("transition");
@include reduce-motion( "transition" );
}

/* stylelint-disable-next-line scss/at-rule-no-unknown -- '@container' not globally permitted */
Expand All @@ -86,4 +93,3 @@
padding-right: $grid-unit-30;
}
}

34 changes: 29 additions & 5 deletions packages/dataviews/src/dataviews-layouts/grid/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ import SingleSelectionCheckbox from '../../components/dataviews-selection-checkb
import { useHasAPossibleBulkAction } from '../../components/dataviews-bulk-actions';
import type { Action, NormalizedField, ViewGridProps } from '../../types';
import type { SetSelection } from '../../private-types';
import getClickableItemProps from '../utils/get-clickable-item-props';

interface GridItemProps< Item > {
selection: string[];
onChangeSelection: SetSelection;
getItemId: ( item: Item ) => string;
onClickItem: ( item: Item ) => void;
isItemClickable: ( item: Item ) => boolean;
item: Item;
actions: Action< Item >[];
mediaField?: NormalizedField< Item >;
Expand All @@ -41,6 +44,8 @@ interface GridItemProps< Item > {
function GridItem< Item >( {
selection,
onChangeSelection,
onClickItem,
isItemClickable,
getItemId,
item,
actions,
Expand All @@ -59,6 +64,21 @@ function GridItem< Item >( {
const renderedPrimaryField = primaryField?.render ? (
<primaryField.render item={ item } />
) : null;

const clickableMediaItemProps = getClickableItemProps(
item,
isItemClickable,
onClickItem,
'dataviews-view-grid__media'
);

const clickablePrimaryItemProps = getClickableItemProps(
item,
isItemClickable,
onClickItem,
'dataviews-view-grid__primary-field'
);

return (
<VStack
spacing={ 0 }
Expand All @@ -81,9 +101,7 @@ function GridItem< Item >( {
}
} }
>
<div className="dataviews-view-grid__media">
{ renderedMediaField }
</div>
<div { ...clickableMediaItemProps }>{ renderedMediaField }</div>
<SingleSelectionCheckbox
item={ item }
selection={ selection }
Expand All @@ -96,8 +114,10 @@ function GridItem< Item >( {
justify="space-between"
className="dataviews-view-grid__title-actions"
>
<HStack className="dataviews-view-grid__primary-field">
{ renderedPrimaryField }
<HStack>
<div { ...clickablePrimaryItemProps }>
{ renderedPrimaryField }
</div>
</HStack>
<ItemActions item={ item } actions={ actions } isCompact />
</HStack>
Expand Down Expand Up @@ -170,6 +190,8 @@ export default function ViewGrid< Item >( {
getItemId,
isLoading,
onChangeSelection,
onClickItem,
isItemClickable,
selection,
view,
density,
Expand Down Expand Up @@ -223,6 +245,8 @@ export default function ViewGrid< Item >( {
key={ getItemId( item ) }
selection={ selection }
onChangeSelection={ onChangeSelection }
onClickItem={ onClickItem }
isItemClickable={ isItemClickable }
getItemId={ getItemId }
item={ item }
actions={ actions }
Expand Down
5 changes: 5 additions & 0 deletions packages/dataviews/src/dataviews-layouts/grid/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@

.dataviews-view-grid__primary-field {
min-height: $grid-unit-40; // Preserve layout when there is no ellipsis button

&--clickable {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is &--clickable the best way to achieve this? We don't seem to do this in Gutenberg (we have only one occurrence of &--is-hex). What does this do that couldn't be achieved by targeting &[role="button"]?

Copy link
Member

Choose a reason for hiding this comment

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

I agree the way this handles the classes needs to be iterated, see related conversation #66365 (comment)

In discussing with Riad IRL the idea was to land this PR and iterate.

width: fit-content;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this needed, and why only for --clickable?

}
}


&.is-selected {
.dataviews-view-grid__fields .dataviews-view-grid__field .dataviews-view-grid__field-value {
color: $gray-900;
Expand Down
37 changes: 34 additions & 3 deletions packages/dataviews/src/dataviews-layouts/table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ import type {
import type { SetSelection } from '../../private-types';
import ColumnHeaderMenu from './column-header-menu';
import { getVisibleFieldIds } from '../index';
import getClickableItemProps from '../utils/get-clickable-item-props';

interface TableColumnFieldProps< Item > {
primaryField?: NormalizedField< Item >;
field: NormalizedField< Item >;
item: Item;
isItemClickable: ( item: Item ) => boolean;
onClickItem: ( item: Item ) => void;
}

interface TableColumnCombinedProps< Item > {
Expand All @@ -48,6 +51,8 @@ interface TableColumnCombinedProps< Item > {
field: CombinedField;
item: Item;
view: ViewTableType;
isItemClickable: ( item: Item ) => boolean;
onClickItem: ( item: Item ) => void;
}

interface TableColumnProps< Item > {
Expand All @@ -56,6 +61,8 @@ interface TableColumnProps< Item > {
item: Item;
column: string;
view: ViewTableType;
isItemClickable: ( item: Item ) => boolean;
onClickItem: ( item: Item ) => void;
}

interface TableRowProps< Item > {
Expand All @@ -69,6 +76,8 @@ interface TableRowProps< Item > {
selection: string[];
getItemId: ( item: Item ) => string;
onChangeSelection: SetSelection;
isItemClickable: ( item: Item ) => boolean;
onClickItem: ( item: Item ) => void;
}

function TableColumn< Item >( {
Expand Down Expand Up @@ -102,15 +111,29 @@ function TableColumnField< Item >( {
primaryField,
item,
field,
isItemClickable,
onClickItem,
}: TableColumnFieldProps< Item > ) {
const isPrimaryField = primaryField?.id === field.id;
const isItemClickableField = ( i: Item ) =>
isItemClickable( i ) && isPrimaryField;

const clickableProps = getClickableItemProps(
item,
isItemClickableField,
onClickItem,
'dataviews-view-table__cell-content'
);

return (
<div
className={ clsx( 'dataviews-view-table__cell-content-wrapper', {
'dataviews-view-table__primary-field':
primaryField?.id === field.id,
'dataviews-view-table__primary-field': isPrimaryField,
} ) }
>
<field.render { ...{ item } } />
<div { ...clickableProps }>
<field.render { ...{ item } } />
</div>
</div>
);
}
Expand Down Expand Up @@ -139,6 +162,8 @@ function TableRow< Item >( {
primaryField,
selection,
getItemId,
isItemClickable,
onClickItem,
onChangeSelection,
}: TableRowProps< Item > ) {
const hasPossibleBulkAction = useHasAPossibleBulkAction( actions, item );
Expand Down Expand Up @@ -214,6 +239,8 @@ function TableRow< Item >( {
<td key={ column } style={ { width, maxWidth, minWidth } }>
<TableColumn
primaryField={ primaryField }
isItemClickable={ isItemClickable }
onClickItem={ onClickItem }
fields={ fields }
item={ item }
column={ column }
Expand Down Expand Up @@ -252,6 +279,8 @@ function ViewTable< Item >( {
onChangeSelection,
selection,
setOpenedFilter,
onClickItem,
isItemClickable,
view,
}: ViewTableProps< Item > ) {
const headerMenuRefs = useRef<
Expand Down Expand Up @@ -392,6 +421,8 @@ function ViewTable< Item >( {
selection={ selection }
getItemId={ getItemId }
onChangeSelection={ onChangeSelection }
onClickItem={ onClickItem }
isItemClickable={ isItemClickable }
/>
) ) }
</tbody>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export default function getClickableItemProps< Item >(
item: Item,
isItemClickable: ( item: Item ) => boolean,
onClickItem: ( item: Item ) => void,
className: string
) {
if ( ! isItemClickable( item ) ) {
return { className };
}

return {
className: `${ className } ${ className }--clickable`,
role: 'button',
tabIndex: 0,
onClick: () => onClickItem( item ),
onKeyDown: ( event: React.KeyboardEvent ) => {
if ( event.key === 'Enter' || event.key === '' ) {
onClickItem( item );
}
},
};
}
2 changes: 2 additions & 0 deletions packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,8 @@ export interface ViewBaseProps< Item > {
onChangeSelection: SetSelection;
selection: string[];
setOpenedFilter: ( fieldId: string ) => void;
onClickItem: ( item: Item ) => void;
isItemClickable: ( item: Item ) => boolean;
view: View;
density: number;
}
Expand Down
Loading
Loading