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

Add optional target property in ReferenceArrayField component #3725

Closed
nicgirault opened this issue Sep 22, 2019 · 4 comments
Closed

Add optional target property in ReferenceArrayField component #3725

nicgirault opened this issue Sep 22, 2019 · 4 comments

Comments

@nicgirault
Copy link
Contributor

Is your feature request related to a problem? Please describe.

I need to link 2 resources but the link does not pass through the id of a resource.

Describe the solution you'd like

Let's consider 2 resources:

Tag(id: number, name: string)
Post(id: number, tagNames: string[]).

Note that the Post resource does not link to Tag via the id of the tag but the name of the tag.

I would like to be able to specify the target property when using ReferenceArrayField:

<ReferenceArrayField
          label="tags"
          reference="tag"
          source="tagNames"
          target="name"
>
@Kmaschta
Copy link
Contributor

Kmaschta commented Sep 23, 2019

Indeed, the reference input and fields only use id as a way to select references, and do not provide any way to select a secondary key.

This is a strong opinionated choice from the core team as it makes a lot of things far easier (cache, optimistic, filters, etc)
It's very unlikely that the core team implements this feature request as we don't have the need.

Any PR are welcome, but I guess this will not be an easy one.
I'll let the core team decide to open this one as an enhancement or close it.

📣 If someone does have a workaround for this use case, feel free to share!

@Kmaschta
Copy link
Contributor

Kmaschta commented Oct 2, 2019

I'm closing since the core team won't implement that suggestion, but comments are still open.

@Kmaschta Kmaschta closed this as completed Oct 2, 2019
@nicgirault
Copy link
Contributor Author

nicgirault commented Nov 7, 2019

Here is my workaround:

import keyBy from 'lodash/keyBy'
import { Loading, Datagrid, Query, TextField, Pagination } from 'react-admin'

const Tags = ({ tagNames }) => {
  const [page, setPage] = useState(1)
  const perPage = 50

  return (
    <Query
      type="GET_LIST"
      resource="tags"
      payload={{
        pagination: { page, perPage },
        sort: { field: 'id', order: 'ASC' },
        filter: { name: tagNames },
      }}
    >
      {({ data, total, loading, error }) => {
        if (loading) {
          return <Loading />
        }
        if (error) {
          return <p>ERROR: {error}</p>
        }
        return (
          <>
            <Datagrid
              data={keyBy(data, 'id')}
              ids={data.map(({ id }) => id)}
              currentSort={{ field: 'id', order: 'ASC' }}
            >
              <TextField source="id" />
              <TextField source="name" />
            </Datagrid>
            <Pagination
              page={page}
              perPage={perPage}
              setPage={setPage}
              total={total}
            />
          </>
        )
      }}
    </Query>
  )
}

I use it in the edit form:

const PostEdit = (props) => (
  <Edit {...props}>
    <SimpleForm>
      <Tags tagNames={props.record.tagNames} />
    </SimpleForm>
  </Edit>
)

Note: it's quite hard to find how to use Datagrid and Pagination components outside of a List component. I had to go in the source code to understand which props are expected. Is there a plan to provide a systematic API doc for all the available components?

@djhi
Copy link
Collaborator

djhi commented Nov 7, 2019

Note: it's quite hard to find how to use Datagrid and Pagination components outside of a List component. I had to go in the source code to understand which props are expected. Is there a plan to provide a systematic API doc for all the available components?

We would love to. Any chance you leverage what you learned and start documenting it ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants