mirror of
https://github.com/Kingsrook/qqq-frontend-material-dashboard.git
synced 2025-07-18 13:20:43 +00:00
CE-1955 - Add onChangeCallback to form fields; add ability to get a DynamicSelect out of DynamicFormField;
This commit is contained in:
@ -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" : "";
|
||||||
|
@ -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,42 +120,76 @@ 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) =>
|
||||||
{
|
{
|
||||||
const beforeStart = e.target.selectionStart;
|
if(isToUpperCase || isToLowerCase)
|
||||||
const beforeEnd = e.target.selectionEnd;
|
|
||||||
|
|
||||||
flushSync(() =>
|
|
||||||
{
|
{
|
||||||
let newValue = e.currentTarget.value;
|
const beforeStart = e.target.selectionStart;
|
||||||
if (isToUpperCase)
|
const beforeEnd = e.target.selectionEnd;
|
||||||
{
|
|
||||||
newValue = newValue.toUpperCase();
|
|
||||||
}
|
|
||||||
if (isToLowerCase)
|
|
||||||
{
|
|
||||||
newValue = newValue.toLowerCase();
|
|
||||||
}
|
|
||||||
setFieldValue(name, newValue);
|
|
||||||
});
|
|
||||||
|
|
||||||
const input = document.getElementById(name) as HTMLInputElement;
|
flushSync(() =>
|
||||||
if (input)
|
{
|
||||||
|
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 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%"
|
||||||
|
Reference in New Issue
Block a user