Search code examples
reactjsreact-admin

How to get the API provided record values within <Edit>?


I have an ArrayInput within an Edit view.

    <Edit undoable={false} {...props} title={<AdTitle />} >
        <TabbedForm>
            <FormTab label="summary">
                <ArrayInput source="siteads" addLabel={false}>
                    <SimpleFormIterator disableRemove disableAdd>
 { record.status === 0 && <TextInput label="Hivatkozas" source="href" props={{ disabled: true }}/> }
                    </SimpleFormIterator>
                </ArrayInput>
            </FormTab>
        </TabbedForm>
    </Edit>

How can I get the record's fields/values within the Edit view?


Solution

  • Here will also work FormDataConsumer, an example from the documentation:

    "You're using a FormDataConsumer inside an ArrayInput and you did not called the getSource function supplied by the FormDataConsumer component. This is required for your inputs to get the proper source"

    <ArrayInput source="users">
      <SimpleFormIterator>
        <TextInput source="name" />
        <FormDataConsumer>
            {({
                formData, // The whole form data
                scopedFormData, // The data for this item of the ArrayInput
                getSource, // A function to get the valid source inside an ArrayInput
                ...rest,
            }) =>
                scopedFormData.name ? (
                    <SelectInput
                        source={getSource('role')} // Will translate to "users[0].role"
                        choices={['admin', 'user']}
                        {...rest}
                    />
                ) : null
            }
        </FormDataConsumer>
      </SimpleFormIterator>
    </ArrayInput>