Skip to content

Commit

Permalink
Add type to disable bulk editing
Browse files Browse the repository at this point in the history
  • Loading branch information
louwie17 committed Dec 10, 2024
1 parent f3f720d commit 90dc201
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 13 deletions.
13 changes: 9 additions & 4 deletions packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export type FieldTypeDefinition< Item > = {
/**
* A dataview field for a specific property of a data type.
*/
export type Field< Item > = {
export type Field< Item, SupportsBulkEditing extends boolean = true > = {
/**
* Type of the fields.
*/
Expand Down Expand Up @@ -111,7 +111,9 @@ export type Field< Item > = {
/**
* Callback used to render an edit control for the field.
*/
Edit?: ComponentType< DataFormControlProps< Item > > | string;
Edit?:
| ComponentType< DataFormControlProps< Item, SupportsBulkEditing > >
| string;

/**
* Callback used to sort the field.
Expand Down Expand Up @@ -184,8 +186,11 @@ export type Fields< Item > = Field< Item >[];

export type Data< Item > = Item[];

export type DataFormControlProps< Item > = {
data: Item | Item[];
export type DataFormControlProps<
Item,
SupportsBulkEditing extends boolean = true,
> = {
data: SupportsBulkEditing extends true ? Item | Item[] : Item;
field: NormalizedField< Item >;
onChange: ( value: Record< string, any > ) => void;
hideLabelFromVision?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion packages/fields/src/fields/parent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { BasePost } from '../../types';
import { ParentEdit } from './parent-edit';
import { ParentView } from './parent-view';

const parentField: Field< BasePost > = {
const parentField: Field< BasePost, false > = {
id: 'parent',
type: 'text',
label: __( 'Parent' ),
Expand Down
3 changes: 1 addition & 2 deletions packages/fields/src/fields/parent/parent-edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,8 @@ export const ParentEdit = ( {
data,
field,
onChange,
}: DataFormControlProps< BasePost > ) => {
}: DataFormControlProps< BasePost, false > ) => {
const { id } = field;
data = Array.isArray( data ) ? data[ 0 ] : data;

const homeUrl = useSelect( ( select ) => {
// @ts-expect-error getEntityRecord is not typed with unstableBase as argument.
Expand Down
2 changes: 1 addition & 1 deletion packages/fields/src/fields/slug/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { BasePost } from '../../types';
import SlugEdit from './slug-edit';
import SlugView from './slug-view';

const slugField: Field< BasePost > = {
const slugField: Field< BasePost, false > = {
id: 'slug',
type: 'text',
label: __( 'Slug' ),
Expand Down
3 changes: 1 addition & 2 deletions packages/fields/src/fields/slug/slug-edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ const SlugEdit = ( {
field,
onChange,
data,
}: DataFormControlProps< BasePost > ) => {
}: DataFormControlProps< BasePost, false > ) => {
const { id } = field;
data = Array.isArray( data ) ? data[ 0 ] : data;

const slug = field.getValue( { item: data } ) || getSlug( data );
const permalinkTemplate = data.permalink_template || '';
Expand Down
2 changes: 1 addition & 1 deletion packages/fields/src/fields/template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { __ } from '@wordpress/i18n';
import type { BasePost } from '../../types';
import { TemplateEdit } from './template-edit';

const templateField: Field< BasePost > = {
const templateField: Field< BasePost, false > = {
id: 'template',
type: 'text',
label: __( 'Template' ),
Expand Down
3 changes: 1 addition & 2 deletions packages/fields/src/fields/template/template-edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ export const TemplateEdit = ( {
data,
field,
onChange,
}: DataFormControlProps< BasePost > ) => {
}: DataFormControlProps< BasePost, false > ) => {
const { id } = field;
data = Array.isArray( data ) ? data[ 0 ] : data;
const postType = data.type;
const postId =
typeof data.id === 'number' ? data.id : parseInt( data.id, 10 );
Expand Down

0 comments on commit 90dc201

Please sign in to comment.