Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

I have a CustomOperatorSelector that I need to use in order to select operators dynamically (I cannot use getOperators) because they depend on a custom value selected by the user. Right now is implemented like this:

export function CustomOperatorSelector(props: OperatorSelectorProps) {
  const { rule, options, value, schema, path } = props;

  // Determine required operator based on rule value's aggregation type
  let requiredOperator: string | null = null;
  if (rule.value && typeof rule.value === 'object' && 'mode' in rule.value) {
    const numValue = rule.value as NumericRuleValue;
    if (numValue.mode === 'agg') {
      if (numValue.agg === 'min') {
        requiredOperator = 'num.gt'; // min → only allow > (price greater than minimum)
      } else if (numValue.agg === 'max') {
        requiredOperator = 'num.lt'; // max → only allow < (price less than maximum)
      }
    }
  }

  // Filter options if there's a required operator
  const filteredOptions: FullOptionList<FullOperator> = requiredOperator
    ? options.filter((op): op is FullOperator => !('options' in op) && op.name === requiredOperator)
    : options;

  // Auto-correct operator if current value doesn't match required
  // HOW DO I DO THIS?
  onPropChange('operator', requiredOperator, path);

  }, [requiredOperator, value, schema.actions, path]);

  return (
    <MaterialValueSelector {...props} options={filteredOptions} variant="outlined" size="small" />
  );
}

The problem is that after selecting the operator, the query might be in a bad "state" because the "old" operator might not be compatible with the new value. I want to change the operator in the current query.

You must be logged in to vote

Can you go through this page and see if it helps?

https://react-querybuilder.js.org/docs/tips/arbitrary-updates

If not, or you need further assistance, let me know.

Replies: 1 comment

Comment options

Can you go through this page and see if it helps?

https://react-querybuilder.js.org/docs/tips/arbitrary-updates

If not, or you need further assistance, let me know.

You must be logged in to vote
0 replies
Answer selected by fediazgon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.