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: Better handling of missing onClickItem prop #67402

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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 };
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
}

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
Loading