Skip to content

Commit

Permalink
DataViews: Better handling of missing onClickItem prop
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Nov 28, 2024
1 parent 6a989b4 commit 9e01b6c
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 27 deletions.
3 changes: 1 addition & 2 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,7 +44,6 @@ const DataViewsContext = createContext< DataViewsContextType< any > >( {
setOpenedFilter: () => {},
openedFilter: null,
getItemId: ( item ) => item.id,
onClickItem: () => {},
isItemClickable: () => false,
} );

Expand Down
3 changes: 1 addition & 2 deletions packages/dataviews/src/components/dataviews/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ type DataViewsProps< Item > = {

const defaultGetItemId = ( item: ItemWithId ) => item.id;
const defaultIsItemClickable = () => false;
const defaultOnClickItem = () => {};
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 9e01b6c

Please sign in to comment.