@@ -13,7 +13,7 @@ import { useTranslation } from '../../providers/Translation/index.js'
1313import { reduceFieldsToOptions } from '../../utilities/reduceFieldsToOptions.js'
1414import { Button } from '../Button/index.js'
1515import { Condition } from './Condition/index.js'
16- import fieldTypes from './field-types.js'
16+ import { fieldTypeConditions , getValidFieldOperators } from './field-types.js'
1717import './index.scss'
1818
1919const baseClass = 'where-builder'
@@ -69,7 +69,7 @@ export const WhereBuilder: React.FC<WhereBuilderProps> = (props) => {
6969 async ( { andIndex, field, orIndex, relation } ) => {
7070 const newConditions = [ ...conditions ]
7171
72- const defaultOperator = fieldTypes [ field . field . type ] . operators [ 0 ] . value
72+ const defaultOperator = fieldTypeConditions [ field . field . type ] . operators [ 0 ] . value
7373
7474 if ( relation === 'and' ) {
7575 newConditions [ orIndex ] . and . splice ( andIndex , 0 , {
@@ -95,30 +95,21 @@ export const WhereBuilder: React.FC<WhereBuilderProps> = (props) => {
9595 )
9696
9797 const updateCondition : UpdateCondition = React . useCallback (
98- async ( { andIndex, field, operator : incomingOperator , orIndex, value : valueArg } ) => {
98+ async ( { andIndex, field, operator : incomingOperator , orIndex, value } ) => {
9999 const existingCondition = conditions [ orIndex ] . and [ andIndex ]
100100
101- const defaults = fieldTypes [ field . field . type ]
102- const operator = incomingOperator || defaults . operators [ 0 ] . value
103-
104101 if ( typeof existingCondition === 'object' && field . value ) {
105- const value = valueArg ?? existingCondition ?. [ operator ]
106-
107- const valueChanged = value !== existingCondition ?. [ String ( field . value ) ] ?. [ String ( operator ) ]
108-
109- const operatorChanged =
110- operator !== Object . keys ( existingCondition ?. [ String ( field . value ) ] || { } ) ?. [ 0 ]
111-
112- if ( valueChanged || operatorChanged ) {
113- const newRowCondition = {
114- [ String ( field . value ) ] : { [ operator ] : value } ,
115- }
116-
117- const newConditions = [ ...conditions ]
118- newConditions [ orIndex ] . and [ andIndex ] = newRowCondition
119-
120- await handleWhereChange ( { or : newConditions } )
102+ const { validOperator } = getValidFieldOperators ( {
103+ field : field . field ,
104+ operator : incomingOperator ,
105+ } )
106+ const newRowCondition = {
107+ [ String ( field . value ) ] : { [ validOperator ] : value } ,
121108 }
109+
110+ const newConditions = [ ...conditions ]
111+ newConditions [ orIndex ] . and [ andIndex ] = newRowCondition
112+ await handleWhereChange ( { or : newConditions } )
122113 }
123114 } ,
124115 [ conditions , handleWhereChange ] ,
0 commit comments