From b07d65aacacf5fa62f69e23cc4e25a7a14090c9b Mon Sep 17 00:00:00 2001 From: Darin Kelkhoff Date: Mon, 25 Nov 2024 10:11:27 -0600 Subject: [PATCH] CE-1955 - Add onChangeCallback to form fields; add ability to get a DynamicSelect out of DynamicFormField; --- .../components/forms/BooleanFieldSwitch.tsx | 11 ++- src/qqq/components/forms/DynamicFormField.tsx | 86 ++++++++++++++----- 2 files changed, 74 insertions(+), 23 deletions(-) diff --git a/src/qqq/components/forms/BooleanFieldSwitch.tsx b/src/qqq/components/forms/BooleanFieldSwitch.tsx index 530dfeb..ef0cc2e 100644 --- a/src/qqq/components/forms/BooleanFieldSwitch.tsx +++ b/src/qqq/components/forms/BooleanFieldSwitch.tsx @@ -80,11 +80,12 @@ interface Props label: string; value: boolean; isDisabled: boolean; + onChangeCallback?: (newValue: any) => void; } -function BooleanFieldSwitch({name, label, value, isDisabled}: Props) : JSX.Element +function BooleanFieldSwitch({name, label, value, isDisabled, onChangeCallback}: Props) : JSX.Element { const {setFieldValue} = useFormikContext(); @@ -93,6 +94,10 @@ function BooleanFieldSwitch({name, label, value, isDisabled}: Props) : JSX.Eleme if(!isDisabled) { setFieldValue(name, newValue); + if(onChangeCallback) + { + onChangeCallback(newValue); + } event.stopPropagation(); } } @@ -100,6 +105,10 @@ function BooleanFieldSwitch({name, label, value, isDisabled}: Props) : JSX.Eleme const toggleSwitch = () => { setFieldValue(name, !value); + if(onChangeCallback) + { + onChangeCallback(!value); + } } const classNullSwitch = (value === null || value == undefined || `${value}` == "") ? "nullSwitch" : ""; diff --git a/src/qqq/components/forms/DynamicFormField.tsx b/src/qqq/components/forms/DynamicFormField.tsx index d9991d6..f2183bc 100644 --- a/src/qqq/components/forms/DynamicFormField.tsx +++ b/src/qqq/components/forms/DynamicFormField.tsx @@ -19,10 +19,12 @@ * along with this program. If not, see . */ +import {QPossibleValue} from "@kingsrook/qqq-frontend-core/lib/model/QPossibleValue"; import {Box, InputAdornment, InputLabel} from "@mui/material"; import Switch from "@mui/material/Switch"; import {ErrorMessage, Field, useFormikContext} from "formik"; import DynamicFormUtils from "qqq/components/forms/DynamicFormUtils"; +import DynamicSelect from "qqq/components/forms/DynamicSelect"; import React, {useMemo, useState} from "react"; import AceEditor from "react-ace"; import colors from "qqq/assets/theme/base/colors"; @@ -43,6 +45,8 @@ interface Props placeholder?: string; backgroundColor?: string; + onChangeCallback?: (newValue: any) => void; + [key: string]: any; bulkEditMode?: boolean; @@ -51,7 +55,7 @@ interface Props } function QDynamicFormField({ - label, name, displayFormat, value, bulkEditMode, bulkEditSwitchChangeHandler, type, isEditable, placeholder, backgroundColor, formFieldObject, ...rest + label, name, displayFormat, value, bulkEditMode, bulkEditSwitchChangeHandler, type, isEditable, placeholder, backgroundColor, formFieldObject, onChangeCallback, ...rest }: Props): JSX.Element { const [switchChecked, setSwitchChecked] = useState(false); @@ -116,42 +120,76 @@ function QDynamicFormField({ // put the onChange in an object and assign it with a spread // //////////////////////////////////////////////////////////////////////// let onChange: any = {}; - if (isToUpperCase || isToLowerCase) + if (isToUpperCase || isToLowerCase || onChangeCallback) { onChange.onChange = (e: any) => { - const beforeStart = e.target.selectionStart; - const beforeEnd = e.target.selectionEnd; - - flushSync(() => + if(isToUpperCase || isToLowerCase) { - let newValue = e.currentTarget.value; - if (isToUpperCase) - { - newValue = newValue.toUpperCase(); - } - if (isToLowerCase) - { - newValue = newValue.toLowerCase(); - } - setFieldValue(name, newValue); - }); + const beforeStart = e.target.selectionStart; + const beforeEnd = e.target.selectionEnd; - const input = document.getElementById(name) as HTMLInputElement; - if (input) + flushSync(() => + { + let newValue = e.currentTarget.value; + if (isToUpperCase) + { + newValue = newValue.toUpperCase(); + } + if (isToLowerCase) + { + newValue = newValue.toLowerCase(); + } + setFieldValue(name, newValue); + onChangeCallback(newValue); + }); + + const input = document.getElementById(name) as HTMLInputElement; + if (input) + { + input.setSelectionRange(beforeStart, beforeEnd); + } + } + else if(onChangeCallback) { - input.setSelectionRange(beforeStart, beforeEnd); + onChangeCallback(e.currentTarget.value); } }; } + /*************************************************************************** + ** + ***************************************************************************/ + function dynamicSelectOnChange(newValue?: QPossibleValue) + { + if(onChangeCallback) + { + onChangeCallback(newValue == null ? null : newValue.id) + } + } + let field; let getsBulkEditHtmlLabel = true; - if (type === "checkbox") + if(formFieldObject.possibleValueProps) + { + field = () + } + else if (type === "checkbox") { getsBulkEditHtmlLabel = false; field = (<> - + {!isDisabled &&
{msg}} />
} @@ -179,6 +217,10 @@ function QDynamicFormField({ onChange={(value: string, event: any) => { setFieldValue(name, value, false); + if(onChangeCallback) + { + onChangeCallback(value); + } }} setOptions={{useWorker: false}} width="100%"