CE-1955 - Add onChangeCallback to form fields; add ability to get a DynamicSelect out of DynamicFormField;

This commit is contained in:
2024-11-25 10:11:27 -06:00
parent cfca47054e
commit b07d65aaca
2 changed files with 74 additions and 23 deletions

View File

@ -80,11 +80,12 @@ interface Props
label: string; label: string;
value: boolean; value: boolean;
isDisabled: 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(); const {setFieldValue} = useFormikContext();
@ -93,6 +94,10 @@ function BooleanFieldSwitch({name, label, value, isDisabled}: Props) : JSX.Eleme
if(!isDisabled) if(!isDisabled)
{ {
setFieldValue(name, newValue); setFieldValue(name, newValue);
if(onChangeCallback)
{
onChangeCallback(newValue);
}
event.stopPropagation(); event.stopPropagation();
} }
} }
@ -100,6 +105,10 @@ function BooleanFieldSwitch({name, label, value, isDisabled}: Props) : JSX.Eleme
const toggleSwitch = () => const toggleSwitch = () =>
{ {
setFieldValue(name, !value); setFieldValue(name, !value);
if(onChangeCallback)
{
onChangeCallback(!value);
}
} }
const classNullSwitch = (value === null || value == undefined || `${value}` == "") ? "nullSwitch" : ""; const classNullSwitch = (value === null || value == undefined || `${value}` == "") ? "nullSwitch" : "";

View File

@ -19,10 +19,12 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import {QPossibleValue} from "@kingsrook/qqq-frontend-core/lib/model/QPossibleValue";
import {Box, InputAdornment, InputLabel} from "@mui/material"; import {Box, InputAdornment, InputLabel} from "@mui/material";
import Switch from "@mui/material/Switch"; import Switch from "@mui/material/Switch";
import {ErrorMessage, Field, useFormikContext} from "formik"; import {ErrorMessage, Field, useFormikContext} from "formik";
import DynamicFormUtils from "qqq/components/forms/DynamicFormUtils"; import DynamicFormUtils from "qqq/components/forms/DynamicFormUtils";
import DynamicSelect from "qqq/components/forms/DynamicSelect";
import React, {useMemo, useState} from "react"; import React, {useMemo, useState} from "react";
import AceEditor from "react-ace"; import AceEditor from "react-ace";
import colors from "qqq/assets/theme/base/colors"; import colors from "qqq/assets/theme/base/colors";
@ -43,6 +45,8 @@ interface Props
placeholder?: string; placeholder?: string;
backgroundColor?: string; backgroundColor?: string;
onChangeCallback?: (newValue: any) => void;
[key: string]: any; [key: string]: any;
bulkEditMode?: boolean; bulkEditMode?: boolean;
@ -51,7 +55,7 @@ interface Props
} }
function QDynamicFormField({ 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 }: Props): JSX.Element
{ {
const [switchChecked, setSwitchChecked] = useState(false); const [switchChecked, setSwitchChecked] = useState(false);
@ -116,9 +120,11 @@ function QDynamicFormField({
// put the onChange in an object and assign it with a spread // // put the onChange in an object and assign it with a spread //
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
let onChange: any = {}; let onChange: any = {};
if (isToUpperCase || isToLowerCase) if (isToUpperCase || isToLowerCase || onChangeCallback)
{ {
onChange.onChange = (e: any) => onChange.onChange = (e: any) =>
{
if(isToUpperCase || isToLowerCase)
{ {
const beforeStart = e.target.selectionStart; const beforeStart = e.target.selectionStart;
const beforeEnd = e.target.selectionEnd; const beforeEnd = e.target.selectionEnd;
@ -135,6 +141,7 @@ function QDynamicFormField({
newValue = newValue.toLowerCase(); newValue = newValue.toLowerCase();
} }
setFieldValue(name, newValue); setFieldValue(name, newValue);
onChangeCallback(newValue);
}); });
const input = document.getElementById(name) as HTMLInputElement; const input = document.getElementById(name) as HTMLInputElement;
@ -142,16 +149,47 @@ function QDynamicFormField({
{ {
input.setSelectionRange(beforeStart, beforeEnd); input.setSelectionRange(beforeStart, beforeEnd);
} }
}
else if(onChangeCallback)
{
onChangeCallback(e.currentTarget.value);
}
}; };
} }
/***************************************************************************
**
***************************************************************************/
function dynamicSelectOnChange(newValue?: QPossibleValue)
{
if(onChangeCallback)
{
onChangeCallback(newValue == null ? null : newValue.id)
}
}
let field; let field;
let getsBulkEditHtmlLabel = true; let getsBulkEditHtmlLabel = true;
if (type === "checkbox") if(formFieldObject.possibleValueProps)
{
field = (<DynamicSelect
name={name}
fieldPossibleValueProps={formFieldObject.possibleValueProps}
isEditable={!isDisabled}
fieldLabel={label}
initialValue={value}
bulkEditMode={bulkEditMode}
bulkEditSwitchChangeHandler={bulkEditSwitchChangeHandler}
onChange={dynamicSelectOnChange}
// otherValues={otherValuesMap}
useCase="form"
/>)
}
else if (type === "checkbox")
{ {
getsBulkEditHtmlLabel = false; getsBulkEditHtmlLabel = false;
field = (<> field = (<>
<BooleanFieldSwitch name={name} label={label} value={value} isDisabled={isDisabled} /> <BooleanFieldSwitch name={name} label={label} value={value} isDisabled={isDisabled} onChangeCallback={onChangeCallback} />
<Box mt={0.75}> <Box mt={0.75}>
<MDTypography component="div" variant="caption" color="error" fontWeight="regular"> <MDTypography component="div" variant="caption" color="error" fontWeight="regular">
{!isDisabled && <div className="fieldErrorMessage"><ErrorMessage name={name} render={msg => <span data-field-error="true">{msg}</span>} /></div>} {!isDisabled && <div className="fieldErrorMessage"><ErrorMessage name={name} render={msg => <span data-field-error="true">{msg}</span>} /></div>}
@ -179,6 +217,10 @@ function QDynamicFormField({
onChange={(value: string, event: any) => onChange={(value: string, event: any) =>
{ {
setFieldValue(name, value, false); setFieldValue(name, value, false);
if(onChangeCallback)
{
onChangeCallback(value);
}
}} }}
setOptions={{useWorker: false}} setOptions={{useWorker: false}}
width="100%" width="100%"