From 42222cd2f68bc9f2dbb5a19e33d0a8d2e186d70c Mon Sep 17 00:00:00 2001 From: Patrik Date: Thu, 23 May 2024 16:01:13 -0400 Subject: [PATCH] fix(ui): where builder issues (#6478) Co-authored-by: Jarrod Flesch --- .../RenderCustomClientComponent/index.tsx | 21 +++ .../elements/RenderCustomComponent/index.tsx | 5 + .../elements/WhereBuilder/Condition/index.tsx | 8 +- .../ui/src/elements/WhereBuilder/index.tsx | 79 +++++----- test/fields/collections/Text/e2e.spec.ts | 136 ++++++++++++++++++ 5 files changed, 207 insertions(+), 42 deletions(-) create mode 100644 packages/ui/src/elements/RenderCustomClientComponent/index.tsx diff --git a/packages/ui/src/elements/RenderCustomClientComponent/index.tsx b/packages/ui/src/elements/RenderCustomClientComponent/index.tsx new file mode 100644 index 000000000..3f2ffd325 --- /dev/null +++ b/packages/ui/src/elements/RenderCustomClientComponent/index.tsx @@ -0,0 +1,21 @@ +import React from 'react' + +export type RenderCustomComponentProps = { + CustomComponent?: React.ComponentType + DefaultComponent: React.ComponentType + componentProps?: Record +} + +export const RenderCustomClientComponent: React.FC = (props) => { + const { CustomComponent, DefaultComponent, componentProps = {} } = props + + if (CustomComponent) { + return + } + + if (DefaultComponent) { + return + } + + return null +} diff --git a/packages/ui/src/elements/RenderCustomComponent/index.tsx b/packages/ui/src/elements/RenderCustomComponent/index.tsx index 8deb06547..87c07c62c 100644 --- a/packages/ui/src/elements/RenderCustomComponent/index.tsx +++ b/packages/ui/src/elements/RenderCustomComponent/index.tsx @@ -14,6 +14,11 @@ export type RenderCustomComponentProps = { serverOnlyProps?: ServerProps } +/** + * If you are passing dynamic props or function props to this component, + * you should instead use the + */ + export const RenderCustomComponent: React.FC = (props) => { const { CustomComponent, DefaultComponent, componentProps, serverOnlyProps } = props diff --git a/packages/ui/src/elements/WhereBuilder/Condition/index.tsx b/packages/ui/src/elements/WhereBuilder/Condition/index.tsx index 9bdb3d65a..25eb55bcc 100644 --- a/packages/ui/src/elements/WhereBuilder/Condition/index.tsx +++ b/packages/ui/src/elements/WhereBuilder/Condition/index.tsx @@ -36,7 +36,7 @@ export type Props = { }) => void } -import { RenderCustomComponent } from '../../../elements/RenderCustomComponent/index.js' +import { RenderCustomClientComponent } from '../../../elements/RenderCustomClientComponent/index.js' import { useDebounce } from '../../../hooks/useDebounce.js' import { Button } from '../../Button/index.js' import { ReactSelect } from '../../ReactSelect/index.js' @@ -148,7 +148,7 @@ export const Condition: React.FC = (props) => { />
- = (props) => { iconStyle="with-border" onClick={() => addCondition({ - andIndex, - fieldName, + andIndex: andIndex + 1, + fieldName: fields[0].value, orIndex, relation: 'and', }) diff --git a/packages/ui/src/elements/WhereBuilder/index.tsx b/packages/ui/src/elements/WhereBuilder/index.tsx index e3d1d9f9e..d8226cb41 100644 --- a/packages/ui/src/elements/WhereBuilder/index.tsx +++ b/packages/ui/src/elements/WhereBuilder/index.tsx @@ -131,7 +131,6 @@ export const WhereBuilder: React.FC = (props) => { setConditions((prevConditions) => { const newConditions = [...prevConditions] newConditions[orIndex].and.splice(andIndex, 1) - if (newConditions[orIndex].and.length === 0) { newConditions.splice(orIndex, 1) } @@ -156,44 +155,48 @@ export const WhereBuilder: React.FC = (props) => { {t('general:filterWhere', { label: getTranslation(collectionPluralLabel, i18n) })}
    - {conditions.map((or, orIndex) => ( -
  • - {orIndex !== 0 &&
    {t('general:or')}
    } -
      - {Array.isArray(or?.and) && - or.and.map((_, andIndex) => { - const initialFieldName = Object.keys(conditions[orIndex].and[andIndex])[0] - const initialOperator = - Object.keys( - conditions[orIndex].and[andIndex]?.[initialFieldName] || {}, - )?.[0] || undefined - const initialValue = - conditions[orIndex].and[andIndex]?.[initialFieldName]?.[initialOperator] || - '' + {conditions.map((or, orIndex) => { + const compoundOrKey = `${orIndex}_${Array.isArray(or?.and) ? or.and.length : ''}` - return ( -
    • - {andIndex !== 0 && ( -
      {t('general:and')}
      - )} - -
    • - ) - })} -
    -
  • - ))} + return ( +
  • + {orIndex !== 0 &&
    {t('general:or')}
    } +
      + {Array.isArray(or?.and) && + or.and.map((_, andIndex) => { + const initialFieldName = Object.keys(conditions[orIndex].and[andIndex])[0] + const initialOperator = + Object.keys( + conditions[orIndex].and[andIndex]?.[initialFieldName] || {}, + )?.[0] || undefined + const initialValue = + conditions[orIndex].and[andIndex]?.[initialFieldName]?.[ + initialOperator + ] || '' + + return ( +
    • + {andIndex !== 0 && ( +
      {t('general:and')}
      + )} + +
    • + ) + })} +
    +
  • + ) + })}