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

MuiAutocomplete - convert value to Array so it can be rendered when j… #991

Closed
wants to merge 1 commit into from

Conversation

ivanbarlog
Copy link

…son logic is imported back to the component

@ukrbublik
Copy link
Owner

@ivanbarlog Please explain what issue this PR is trying to fix.
For multiple autocomplete value should be already Array. For single autocomplete value should not be an Array

@ivanbarlog
Copy link
Author

Hi @ukrbublik,

I am using following function spec:

const listValues = [
    { title: 'A', value: 1 },
    { title: 'B', value: 2 },
    { title: 'C', value: 3 }
];

const config = {
    ...MuiConfig,
    funcs: {
        percentOfMultiselect: {
        label: '% of',
        jsonLogic: ({ value, percent }) => {
          return { percentOfMultiselect: [value, percent] };
        },
        jsonLogicImport: ({ percentOfMultiselect }) => {
          return percentOfMultiselect;
        },
        returnType: 'number',
        allowSelfNesting: false,
        args: {
          value: {
            label: 'Value',
            type: 'multiselect',
            valueSources: ['value'],
            fieldSettings: {
              listValues,
              showSearch: true
            }
          },
          percent: {
            label: 'Percent',
            type: 'number',
            valueSources: ['value'],
            fieldSettings: { min: 0, step: 0.01 }
          }
        }
      }
   }
}

When I render it and select A and C for instance it looks like following figure.

image

But then when I persist it to the database and try to hydrate it back the fields are selected in the dropdown list but are not rendered in MuiAutocomplete.

image image

When I apply hotfix it looks like it should as depicted in following figure.

image

I am then parsing the JSON from database like this (simplified version without the checks):

const parsed = JSON.parse(stringifiedValueFromDb);
const logic = QbUtils.loadFromJsonLogic(parsed, config);

// I am using this in my React state
const tree = QbUtils.checkTree(logic, config)

Here is the value of stringifiedValueFromDb (formatted):

{
  "and": [
    {
      "==": [
        {
          "percentOfMultiselect": [
            [
              1,
              3
            ],
            10
          ]
        },
        30
      ]
    }
  ]
}

Also here is stringified value of logic value:

{
  "type": "group",
  "id": "a99aa988-4567-489a-bcde-f18b753b0909",
  "children1": {
    "b89a9bb8-89ab-4cde-b012-318b753b0909": {
      "type": "rule",
      "id": "b89a9bb8-89ab-4cde-b012-318b753b0909",
      "properties": {
        "field": {
          "func": "percentOfMultiselect",
          "args": {
            "value": {
              "valueSrc": "value",
              "value": [
                1,
                3
              ],
              "valueType": "multiselect"
            },
            "percent": {
              "valueSrc": "value",
              "value": 10,
              "valueType": "number"
            }
          }
        },
        "fieldSrc": "func",
        "operator": "equal",
        "value": [
          30
        ],
        "valueSrc": [
          "value"
        ],
        "valueType": [
          "number"
        ]
      }
    }
  },
  "properties": {
    "conjunction": "AND",
    "not": false
  }
}

Am I doing something wrong?

When I add console.log(selectedValue, typeof selectedValue); to the patch this is what I get:

image

So it seems that MuiAutocomplete does not provide values as array but rather an object. That's why I've done Array.from(selectedValue).

@ukrbublik
Copy link
Owner

@ivanbarlog
Thanks for the detailed explanation!
Now I get it, the issue is using multiselect as a function argument.
I need to fix this issue at the import level (object you see is an Immutable object, but it should be Array).

Repository owner deleted a comment from codecov bot Oct 28, 2023
Repository owner deleted a comment from codesandbox-ci bot Oct 28, 2023
Repository owner deleted a comment from vercel bot Oct 28, 2023
Repository owner deleted a comment from codesandbox bot Oct 28, 2023
@ukrbublik
Copy link
Owner

Closing in favour of #997 with more comprehensive fix
Thanks for issue report and PR

@ukrbublik ukrbublik closed this Oct 28, 2023
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

Successfully merging this pull request may close these issues.

2 participants