@kwiz/fluentui
Version:
KWIZ common controls for FluentUI
33 lines • 1.67 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import { Field } from "@fluentui/react-components";
import { isNotEmptyString, isNullOrEmptyArray, isNullOrEmptyString, isNullOrUndefined, stringEqualsOrEmpty } from "@kwiz/common";
import { useCallback, useEffect, useState } from "react";
import { useFormValidationContext } from "./form-context";
export function FormFieldEX(props) {
const { validation, formKey, required, value } = props;
const validate_internal = useCallback(() => {
//calc if value is valid
let error = validation
? validation()
: "";
if (isNullOrEmptyString(error) && required) {
if ((Array.isArray(value) ? isNullOrEmptyArray(value) : isNullOrEmptyString(value)))
error = "Missing required value";
}
return error;
}, [validation, required, value]);
const [error, setError] = useState("");
useEffect(() => {
const newError = validate_internal();
if (!stringEqualsOrEmpty(newError, error))
setError(newError);
}, [validate_internal, value, error]);
const formContext = useFormValidationContext();
useEffect(() => {
if (!isNullOrUndefined(formContext === null || formContext === void 0 ? void 0 : formContext.setError)) {
formContext.setError(error, formKey);
}
}, [formContext, error, formKey]);
return _jsx(Field, Object.assign({}, props, { validationMessage: formContext.showErrors ? error : undefined, validationState: formContext.showErrors && isNotEmptyString(error) ? "error" : "none", children: props.children }));
}
//# sourceMappingURL=field.js.map