Field Comparisons
Enable allowFieldComparisons on Builder when a rule should be able to compare against another field instead of a literal value.
Enable field comparisons
import '@vojtechportes/react-query-builder/styles.css';import React, { useState } from 'react';import {Builder,type DenormalizedQuery,type IBuilderFieldProps,} from '@vojtechportes/react-query-builder';const fields: IBuilderFieldProps[] = [{field: 'ORDER_TOTAL',label: 'Order total',type: 'NUMBER',operators: ['LARGER_EQUAL'],fieldComparison: {type: 'number',comparableFields: ['ORDER_APPROVAL_LIMIT'],},},{field: 'ORDER_APPROVAL_LIMIT',label: 'Approval limit',type: 'NUMBER',operators: ['LARGER_EQUAL'],},{field: 'CUSTOMER_COUNTRY',label: 'Customer country',type: 'LIST',operators: ['EQUAL'],value: [{ value: 'CZ', label: 'Czech Republic' },{ value: 'SK', label: 'Slovakia' },],fieldComparison: {type: 'string',comparableFields: ['DELIVERY_COUNTRY_CODE'],},},{field: 'DELIVERY_COUNTRY_CODE',label: 'Delivery country code',type: 'TEXT',operators: ['EQUAL'],},];const initialData: DenormalizedQuery = [{type: 'GROUP',value: 'AND',isNegated: false,children: [{field: 'ORDER_TOTAL',operator: 'LARGER_EQUAL',valueSource: 'field',valueField: 'ORDER_APPROVAL_LIMIT',},{field: 'CUSTOMER_COUNTRY',operator: 'EQUAL',valueSource: 'field',valueField: 'DELIVERY_COUNTRY_CODE',},],},];export const FieldComparisonBuilder = () => {const [data, setData] = useState(initialData);return (<BuilderallowFieldComparisonsfields={fields}data={data}onChange={setData}/>);};
How It Works
valueSource: 'field'plusvalueFieldstores the selected comparison field in query data.fieldComparison.typelets semantically compatible fields compare even when their builder UI types differ, such asLISTtoTEXT.fieldComparison.comparableFieldsnarrows the right-hand-side selector to an explicit allowlist owned by the source field.
API reference
Parsing and formatting
Native field-to-field formatting and parsing examples are documented in Supported Formats.