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: Type the ViewList component #61246

Merged
merged 4 commits into from
May 6, 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
28 changes: 25 additions & 3 deletions packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
*/
import type { ReactNode } from 'react';

type Item = Record< string, any >;

interface Option {
value: any;
label: string;
Expand All @@ -17,6 +15,8 @@ interface filterByConfig {

type Operator = 'is' | 'isNot' | 'isAny' | 'isNone' | 'isAll' | 'isNotAll';

export type Item = Record< string, any >;
youknowriad marked this conversation as resolved.
Show resolved Hide resolved

export interface Field {
/**
* The unique identifier of the field.
Expand Down Expand Up @@ -101,7 +101,7 @@ export interface Filter {
value: any;
}

export interface View {
interface ViewBase {
/**
* The layout of the view.
*/
Expand Down Expand Up @@ -141,4 +141,26 @@ export interface View {
* The number of items per page
*/
perPage?: number;

/**
* The hidden fields.
*/
hiddenFields: string[];
}
export interface ViewList extends ViewBase {
type: 'list';

layout: {
/**
* The field to use as the primary field.
*/
primaryField: string;

/**
* The field to use as the media field.
*/
mediaField: string;
};
}

export type View = ViewList | ViewBase;
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,33 @@ import { __ } from '@wordpress/i18n';
* Internal dependencies
*/
import { unlock } from './lock-unlock';
import type {
Data,
Item,
NormalizedField,
ViewList as ViewListType,
} from './types';

interface ListViewProps {
view: ViewListType;
fields: NormalizedField[];
data: Data;
isLoading: boolean;
getItemId: ( item: Item ) => string;
onSelectionChange: ( selection: Item[] ) => void;
selection: Item[];
id: string;
}

interface ListViewItemProps {
id: string;
item: Item;
isSelected: boolean;
onSelect: ( item: Item ) => void;
mediaField?: NormalizedField;
primaryField?: NormalizedField;
visibleFields: NormalizedField[];
}

const {
useCompositeStoreV2: useCompositeStore,
Expand All @@ -37,8 +64,8 @@ function ListItem( {
mediaField,
primaryField,
visibleFields,
} ) {
const itemRef = useRef( null );
}: ListViewItemProps ) {
const itemRef = useRef< HTMLElement >( null );
const labelId = `${ id }-label`;
const descriptionId = `${ id }-description`;

Expand Down Expand Up @@ -120,16 +147,17 @@ function ListItem( {
);
}

export default function ViewList( {
view,
fields,
data,
isLoading,
getItemId,
onSelectionChange,
selection,
id: preferredId,
} ) {
export default function ViewList( props: ListViewProps ) {
const {
view,
fields,
data,
isLoading,
getItemId,
onSelectionChange,
selection,
id: preferredId,
} = props;
const baseId = useInstanceId( ViewList, 'view-list', preferredId );
const selectedItem = data?.findLast( ( item ) =>
selection.includes( item.id )
Expand All @@ -150,12 +178,12 @@ export default function ViewList( {
);

const onSelect = useCallback(
( item ) => onSelectionChange( [ item ] ),
( item: Item ) => onSelectionChange( [ item ] ),
[ onSelectionChange ]
);

const getItemDomId = useCallback(
( item ) => ( item ? `${ baseId }-${ getItemId( item ) }` : undefined ),
( item?: Item ) => ( item ? `${ baseId }-${ getItemId( item ) }` : '' ),
Copy link
Member

Choose a reason for hiding this comment

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

Returning an empty string instead of undefined changes the behavior of getItemDomId breaking the keyboard navigation. Fix at #61478

[ baseId, getItemId ]
);

Expand Down
6 changes: 4 additions & 2 deletions packages/dataviews/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"declarationDir": "build-types"
"declarationDir": "build-types",
"skipLibCheck": true,
"checkJs": false
},
"references": [
{ "path": "../a11y" },
Expand All @@ -16,5 +18,5 @@
{ "path": "../primitives" },
{ "path": "../private-apis" }
],
"include": [ "src/**/*.ts", "src/**/*.tsx" ]
"include": [ "src" ]
}
Loading