React-Admin: How to make <Filter> mandatory with "alwaysOn" using "list"

Viewed 246

I couldn't find a way to ONLY FETCH or LIST resources,
when a user fills in the <SelectInput> (as rendered by <ReferenceInput>).

Due to a domain restriction,
I need to ask the user to choose some Customer in order to query the Orders.
Is that possible? How can I pull it off?

To be clear, in their Demo project, the "Orders" can be listed using "Customer" as a <Filter>. (see image below)

enter image description here

A workaround could be also valid. Thanks.

1 Answers

Ok, this is an interesting scenario.

Let's closely look at the resource, and how it routes to various components.
In this case, our interest is within the <list> and what exactly it renders:

// Set up the filter
const CustomerFilter = (props) => (
  <Filter {...props}>
    // setup your `ReferenceInput` and `SelectInput`
  </Filter>
);

// Within your rendered component (OrderList)
<List {...props} filters={<CustomerFilter />}>
 <Datagrid>
   {console.log(props)} // Look out for the `match.params` prop
   // ...
   // Check for "customer_name" param within your props (verbose)
   {match.params.customer &&
     <TextField label="Customer" source="customer_name" /> 
   } 
 </Datagrid>
</List>

Please note: don't just copy/paste this code. It's a verbose version.
The implication is that you can render customer name (or whatever) within your list depending on a passed parameter.

Related