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

Ability to get the input type on generated rules from JSON. #226

Closed
balboFK opened this issue Oct 4, 2021 · 3 comments
Closed

Ability to get the input type on generated rules from JSON. #226

balboFK opened this issue Oct 4, 2021 · 3 comments

Comments

@balboFK
Copy link

balboFK commented Oct 4, 2021

Hello!
Thanks for the awesome library!

I was wondering if it's somehow possible to get the "inputType" on the final generated rule object when exporting to JSON.

const fields = [
     { name: 'description', label: 'Description', inputType: 'string' },
     { name: 'price', label: 'Price', inputType: 'number' },
]

{
    "rules": [
        {
            "field": "description",
            "value": "M4",
            "operator": "startswith",
            "type": "string" // this being the prop that was given to "inputType"
        },
        {
            "field": "price",
            "value": "579,99",
            "operator": "greaterthan",
            "type": "number" // this also being the prop that was given to "inputType"
        },
    ],
    "combinator": "and",
    "not": false
}

Also, is it possible to remove the "not" property from the object when using 'formatQuery'?
Thanks!

@jakeboone02
Copy link
Member

I don't want to add inputType to the default export since it's tied to the Field object and not the Rule, and would therefore be redundant, but you can add inputType to the query yourself with the following code. (I haven't actually tested it but it should be pretty close.) It's TypeScript but if you need a plain JavaScript version I can strip out the type-related stuff for you.

import { RuleGroupType, RuleType } from 'react-querybuilder';

const fields = [
  { name: 'description', label: 'Description', inputType: 'string' },
  { name: 'price', label: 'Price', inputType: 'number' }
];

const processRule = (r: RuleType): RuleType & { inputType?: string } => ({
  ...r,
  inputType: fields.find((f) => f.name === r.field)?.inputType
});

const processGroup = (rg: RuleGroupType): RuleGroupType => ({
  ...rg,
  rules: rg.rules.map((r: RuleType | RuleGroupType) => {
    if (r['field' as 'id']) {
      return processRule(r as RuleType);
    }
    return processGroup(r as RuleGroupType);
  })
});

const result = processGroup(query);

To remove the not property, just do this:

JSON.stringify(query, ['rules', 'field', 'value', 'operator', 'combinator']);

That will stringify your query but only export the properties listed in the second argument array. This is exactly how the "json_without_ids" format works, but it includes the not property.

@balboFK
Copy link
Author

balboFK commented Oct 4, 2021

Hey @jakeboone02 , thanks for the fast reply and help! Works like a charm.
The guys from the backend here are giving you a shoutout, since they will not need to change anything on their side to achieve what we we're hoping for!

Thanks for the help and for the amazing library, you rock.

@jakeboone02
Copy link
Member

Aw, thanks! I really appreciate that. Glad to help.

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

2 participants