Skip to content

Commit

Permalink
DataViews: Better handling of missing onClickItem prop (#67402)
Browse files Browse the repository at this point in the history
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: jsnajdr <[email protected]>
  • Loading branch information
3 people authored Dec 2, 2024
1 parent e07fe5c commit 39a4d1c
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 29 deletions.
5 changes: 2 additions & 3 deletions packages/dataviews/src/components/dataviews-context/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type DataViewsContextType< Item > = {
openedFilter: string | null;
setOpenedFilter: ( openedFilter: string | null ) => void;
getItemId: ( item: Item ) => string;
onClickItem: ( item: Item ) => void;
onClickItem?: ( item: Item ) => void;
isItemClickable: ( item: Item ) => boolean;
};

Expand All @@ -44,8 +44,7 @@ const DataViewsContext = createContext< DataViewsContextType< any > >( {
setOpenedFilter: () => {},
openedFilter: null,
getItemId: ( item ) => item.id,
onClickItem: () => {},
isItemClickable: () => false,
isItemClickable: () => true,
} );

export default DataViewsContext;
5 changes: 2 additions & 3 deletions packages/dataviews/src/components/dataviews/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ type DataViewsProps< Item > = {
: { getItemId: ( item: Item ) => string } );

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

export default function DataViews< Item >( {
Expand All @@ -70,7 +69,7 @@ export default function DataViews< Item >( {
defaultLayouts,
selection: selectionProperty,
onChangeSelection,
onClickItem = defaultOnClickItem,
onClickItem,
isItemClickable = defaultIsItemClickable,
header,
}: DataViewsProps< Item > ) {
Expand Down
14 changes: 7 additions & 7 deletions packages/dataviews/src/dataviews-layouts/grid/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ interface GridItemProps< Item > {
selection: string[];
onChangeSelection: SetSelection;
getItemId: ( item: Item ) => string;
onClickItem: ( item: Item ) => void;
onClickItem?: ( item: Item ) => void;
isItemClickable: ( item: Item ) => boolean;
item: Item;
actions: Action< Item >[];
Expand Down Expand Up @@ -66,19 +66,19 @@ function GridItem< Item >( {
<primaryField.render item={ item } />
) : null;

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

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

return (
<VStack
Expand Down
16 changes: 8 additions & 8 deletions packages/dataviews/src/dataviews-layouts/table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ interface TableColumnFieldProps< Item > {
field: NormalizedField< Item >;
item: Item;
isItemClickable: ( item: Item ) => boolean;
onClickItem: ( item: Item ) => void;
onClickItem?: ( item: Item ) => void;
}

interface TableColumnCombinedProps< Item > {
Expand All @@ -52,7 +52,7 @@ interface TableColumnCombinedProps< Item > {
item: Item;
view: ViewTableType;
isItemClickable: ( item: Item ) => boolean;
onClickItem: ( item: Item ) => void;
onClickItem?: ( item: Item ) => void;
}

interface TableColumnProps< Item > {
Expand All @@ -62,7 +62,7 @@ interface TableColumnProps< Item > {
column: string;
view: ViewTableType;
isItemClickable: ( item: Item ) => boolean;
onClickItem: ( item: Item ) => void;
onClickItem?: ( item: Item ) => void;
}

interface TableRowProps< Item > {
Expand All @@ -77,7 +77,7 @@ interface TableRowProps< Item > {
getItemId: ( item: Item ) => string;
onChangeSelection: SetSelection;
isItemClickable: ( item: Item ) => boolean;
onClickItem: ( item: Item ) => void;
onClickItem?: ( item: Item ) => void;
}

function TableColumn< Item >( {
Expand Down Expand Up @@ -118,12 +118,12 @@ function TableColumnField< Item >( {
const isItemClickableField = ( i: Item ) =>
isItemClickable( i ) && isPrimaryField;

const clickableProps = getClickableItemProps(
const clickableProps = getClickableItemProps( {
item,
isItemClickableField,
isItemClickable: isItemClickableField,
onClickItem,
'dataviews-view-table__cell-content'
);
className: 'dataviews-view-table__cell-content',
} );

return (
<div
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
export default function getClickableItemProps< Item >(
item: Item,
isItemClickable: ( item: Item ) => boolean,
onClickItem: ( item: Item ) => void,
className: string
) {
if ( ! isItemClickable( item ) ) {
export default function getClickableItemProps< Item >( {
item,
isItemClickable,
onClickItem,
className,
}: {
item: Item;
isItemClickable: ( item: Item ) => boolean;
onClickItem?: ( item: Item ) => void;
className: string;
} ) {
if ( ! isItemClickable( item ) || ! onClickItem ) {
return { className };
}

Expand Down
2 changes: 1 addition & 1 deletion packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ export interface ViewBaseProps< Item > {
onChangeSelection: SetSelection;
selection: string[];
setOpenedFilter: ( fieldId: string ) => void;
onClickItem: ( item: Item ) => void;
onClickItem?: ( item: Item ) => void;
isItemClickable: ( item: Item ) => boolean;
view: View;
}
Expand Down

0 comments on commit 39a4d1c

Please sign in to comment.