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

Renders DataForm component only when data has been fetched #67694

Merged
merged 4 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 5 additions & 1 deletion packages/dataviews/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,10 @@ const fields = [
- `type`: either `regular` or `panel`.
- `fields`: a list of fields ids that should be rendered.

#### `isLoading`: `boolean`

Whether the data is loading. `false` by default.

#### `onChange`: `function`

Callback function that receives an object with the edits done by the user.
Expand Down Expand Up @@ -520,7 +524,7 @@ The unique identifier of the action.
- Required
- Example: `move-to-trash`

### `label`
### `label`

The user facing description of the action.

Expand Down
3 changes: 2 additions & 1 deletion packages/dataviews/src/components/dataform/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ export default function DataForm< Item >( {
form,
fields,
onChange,
isLoading = false,
}: DataFormProps< Item > ) {
const normalizedFields = useMemo(
() => normalizeFields( fields ),
[ fields ]
);

if ( ! form.fields ) {
if ( ! form.fields || isLoading ) {
return null;
}

Expand Down
1 change: 1 addition & 0 deletions packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ export interface DataFormProps< Item > {
fields: Field< Item >[];
form: Form;
onChange: ( value: Record< string, any > ) => void;
isLoading?: boolean;
}

export interface FieldLayoutProps< Item > {
Expand Down
22 changes: 14 additions & 8 deletions packages/edit-site/src/components/post-edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,22 @@ const fieldsWithBulkEditSupport = [

function PostEditForm( { postType, postId } ) {
const ids = useMemo( () => postId.split( ',' ), [ postId ] );
const { record } = useSelect(
const { record, hasFinishedResolution } = useSelect(
( select ) => {
const args = [ 'postType', postType, ids[ 0 ] ];

const {
getEditedEntityRecord,
hasFinishedResolution: hasFinished,
} = select( coreDataStore );

return {
record:
ids.length === 1
? select( coreDataStore ).getEditedEntityRecord(
'postType',
postType,
ids[ 0 ]
)
: null,
ids.length === 1 ? getEditedEntityRecord( ...args ) : null,
hasFinishedResolution: hasFinished(
'getEditedEntityRecord',
args
),
};
},
[ postType, ids ]
Expand Down Expand Up @@ -162,6 +167,7 @@ function PostEditForm( { postType, postId } ) {
<PostEditHeader postType={ postType } postId={ postId } />
<DataForm
data={ ids.length === 1 ? record : multiEdits }
isLoading={ ! hasFinishedResolution }
fields={ fieldsWithDependency }
form={ form }
onChange={ onChange }
Expand Down
Loading