react-admin icon indicating copy to clipboard operation
react-admin copied to clipboard

DependsOn not working

Open zavadpe opened this issue 8 years ago • 9 comments

What you were expecting: <DependsOn> is working as described in documentation.

What happened instead: <DependsOn> is causing error and blank page.

Related code:

<DependsOn source="name" value="abstract">
  <TextField source="name">
</DependsOn>

applied in next branch demo project.

Environment

  • react-admin: 2.0.0-beta4
  • React: 16.2.0 dependson_error

zavadpe avatar Apr 12 '18 18:04 zavadpe

Are you using it in a form or in a list/show view? Can you provide more context?

fzaninotto avatar Apr 13 '18 12:04 fzaninotto

@fzaninotto: Sorry, here's more detailed example from demo src/categories/index.js:

export const CategoryList = withStyles(listStyles)(({ classes, ...props }) => (
    <List {...props} sort={{ field: 'name', order: 'ASC' }}>
        <Datagrid>
            <TextField source="name" className={classes.name} />
            <LinkToRelatedProducts />
            <DependsOn source="name" value="abstract">
              <TextField source="name" />
            </DependsOn>
            <EditButton />
        </Datagrid>
    </List>
));

Note that also using resolve function causes the same.

zavadpe avatar Apr 13 '18 12:04 zavadpe

The current DependsOn implementation requires a redux-form to be declared higher in the component hierarchy, so it doesn't work in List and Show views.

But I'm wondering if we need to provide a special component for conditional field, as it's already possible using a simple:

const ConditionalTextField = ({ record, ...props }) => record.name == 'abstract' ? <TextField source="name" record={record} {...props} /> : null;
ConditionalTestField.defaultProps: {
  addLabel: true,
};

@djhi, any opinion?

fzaninotto avatar Apr 15 '18 18:04 fzaninotto

Agreed

djhi avatar Apr 16 '18 07:04 djhi

Not that simple: the addLabel should be conditional, too...

It seems that the DependentField in ra-dependent-input didn't work either, since it used <FormField> in a formless environment...

fzaninotto avatar Apr 16 '18 15:04 fzaninotto

Fixed by #1748

fzaninotto avatar Apr 17 '18 10:04 fzaninotto

@fzaninotto there is an example for how to do this on and . Are you able to give an example for how to do this on ?

I can't seem to figure it out. Thanks.

trsteel88 avatar Dec 05 '18 23:12 trsteel88

Fyi, I tried the following:

const ConditionalOrganisation = ({ record, ...rest }) => {
    if (record && 'CUSTOM' === record.type) {
        return (
            <ReferenceField label="Organisation" source="organisation" reference="organisations" record={record} {...rest}>
                <TextField source="name" />
            </ReferenceField>
        );
    }

    return null;
}

It seems like it may just be an issue with the ReferenceField and this method?

trsteel88 avatar Dec 05 '18 23:12 trsteel88

Please stop asking this kind of questions on issues, especially closed ones. Use Stack overflow for that.

djhi avatar Dec 06 '18 07:12 djhi