react-awesome-query-builder icon indicating copy to clipboard operation
react-awesome-query-builder copied to clipboard

Polymorphic fields

Open gempain opened this issue 4 years ago • 1 comments

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

Say I have the following object:

{
  values: [
    {type: '1', props: {.a: 1 }},
    {type: '2', props: {.a: 2 }}
  ]
}

Right now, the only way is to define the following fields:

{
  values: {
    type: '!group',
    subfields: {
      type: {
        type: 'select',
        valueSources: ['value'],
        fieldSettings: {
          listValues: [
            { value: '1' },
            { value: '2' },
          ],
        },
      },
      value: {
        type: '!group',
        subfields: {
          a: { type: 'text', valueSources: ['value'] },
          b: { type: 'text', valueSources: ['value'] },
        },
      },
    },
  },
}

Which may lead to invalid rules. If someone selects field 1 and value.b in the criteria, nothing will ever match, and it's misleading for users.

Describe the solution you'd like I would create a new property allowing a dynamic schema to be selected:

const val: Fields = {
  values: {
    type: '!group',
    subfields: {
      type: { /*...*/ },
      value: {
        type: '!group',
        subfields: parent => {
          switch (parent.field) {
            case '1': 
              return {
                a: { type: 'text', valueSources: ['value'] },
              };
            case '2':
              return {
                b: { type: 'text', valueSources: ['value'] },
              };
          }
        },
      },
    },
  },
};

Describe alternatives you've considered I guess this is related to #43. The only alternative at the moment is to extract value and apply the json logic on every item in it. That removes the need for a polymorphic definition, but it's not always handy when you have multiple levels of polymorphic objects.

Additional context N/A

gempain avatar Nov 11 '21 12:11 gempain

Plus it would be great if we had some way of changing the second element options based on the first one, for example, having a list of products for each store

image

PauloStanize-NHT avatar Nov 24 '21 18:11 PauloStanize-NHT