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

buildInitialState doesn't handle Array Field Data correctly #6726

Open
tigersoft6084 opened this issue Jun 11, 2024 · 0 comments
Open

buildInitialState doesn't handle Array Field Data correctly #6726

tigersoft6084 opened this issue Jun 11, 2024 · 0 comments
Assignees
Labels

Comments

@tigersoft6084
Copy link

tigersoft6084 commented Jun 11, 2024

Link to reproduction

No response

Describe the Bug

I am implementing Settings page which handles configuring sub-domains for a tenant.
It uses payload/components/forms/Form to render domains array field and submit the updated data.
I used buildInitialState function to convert the initial data(which was fetched from db) to form state.
The form didn't show the initial rows when initialState was generated by the payload's buildInitialState, so I had to implement my own one to perform the conversion.

I noticed that Form component treats Array or Block field states in a different way from this code:
image

But buildInitialState was not treating those fields differently.

image

To Reproduce

I was implementing Settings page which handles configuring sub-domains for a tenant.
It uses payload/components/forms/Form to render domains array field and submit the updated data.
I used buildInitialState function to convert the initial data(which was from db) to form state.

import buildInitialState from 'payload/dist/admin/components/forms/Form/buildInitialState';

The field configuration and initial data looks like this:

const [fields] = useState<Field[]>([
    {
        name: 'domains',
        label: 'Domains',
        type: 'array',
        fields: [
            {
                type: 'text',
                name: 'domain',
                required: true,
            },
        ],
    },
]);

const initialData = {
    'domains': [
        {
            id: '66667b2731c48fcdba97a68f',
            domain: 'hello.com',
        },
    ],
};

const initialState = buildInitialState(initialData);

And the return value of the react component looks like this:

<DocumentInfoProvider collection={collectionConfig} id={id}>
    <Gutter>
        <h1>Domains</h1>
        <Form
            method="post"
            action={`${api}/settings/general`}
            fields={fields}
            initialState={initialState}
        >
            <RenderFields
                fieldSchema={fields}
                fieldTypes={fieldTypes}
                readOnly={false}
            />
            <FormSubmit>Submit</FormSubmit>
        </Form>
    </Gutter>
</DocumentInfoProvider>

The expected value of initialState is like this:

{
    'domains': {
        passesCondition: false,
        rows: [{ id: '66667b2731c48fcdba97a68f' }],
        value: 1,
        valid: true,
        initialValue: 1,
    },
    'domains.0.domain': {
        value: 'hello.com',
        initialValue: 'hello.com',
        valid: true,
    },
    'domains.0.id': {
        value: '66667b2731c48fcdba97a68f',
        initialValue: '66667b2731c48fcdba97a68f',
        valid: true,
    },
}

While initialState generated by buildInitialState is:

{
    'domains': {
        initialValue: [{
            id: '66667b2731c48fcdba97a68f',
            domain: 'hello.com',
        }],
        value: [{
            id: '66667b2731c48fcdba97a68f',
            domain: 'hello.com',
        }],
        valid: true
    }
}

The form didn't show the initial rows when initialState was generated by the payload's buildInitialState, so I implemented my own one.

Payload Version

2.19.3

Adapters and Plugins

No response

@tigersoft6084 tigersoft6084 added the status: needs-triage Possible bug which hasn't been reproduced yet label Jun 11, 2024
@github-actions github-actions bot removed the status: needs-triage Possible bug which hasn't been reproduced yet label Jun 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants