@hitachivantara/uikit-react-core
Version:
UI Kit Core React components.
62 lines (61 loc) • 2.81 kB
JavaScript
import { Children } from "react";
//#region src/FormElement/utils.ts
/**
* Scans the element's children looking for the children IDs that match the different form element types.
* This function will produce an object that has a key for each provided name
* Inside each key there will be an array with each id of the found descriptor.
*
* @param {Array} children - The children inside the form element to scan.
* @param {Object} descriptors - Initial descriptors map (used for recursion).
*
*/
var findDescriptors = (children, descriptors = {
input: [],
label: [],
description: [],
controlled: [],
errormessage: [],
HvCalendarHeader: []
}) => {
Children.forEach(children, (child) => {
if (child?.type?.formElementType && child.props?.id) descriptors[child.type.formElementType]?.push({
id: child.props?.id,
htmlFor: child.props?.htmlFor
});
if (child?.type?.formElementType !== "formelement") findDescriptors(child?.props?.children, descriptors);
});
return descriptors;
};
var getIdReferenceListFor = (formElementType, descriptors, filterFor = null) => {
const referenceList = descriptors?.[formElementType]?.filter((d) => d.htmlFor !== filterFor)?.map((d) => d.id).join(" ").trim();
return referenceList !== "" ? referenceList : void 0;
};
var getIdReferenceFor = (formElementType, descriptors, filterFor = null) => {
const referenceList = descriptors?.[formElementType]?.filter((d) => d.htmlFor !== filterFor)?.map((d) => d.id)?.[0];
return referenceList !== "" ? referenceList : void 0;
};
var buildFormElementPropsFromContext = (name, disabled, readOnly, required, context) => {
return {
name: name || context?.name,
disabled: disabled ?? context?.disabled,
readOnly: readOnly ?? context?.readOnly,
required: required ?? context?.required,
status: context?.status
};
};
var buildAriaPropsFromContext = (props, context, isInvalid, inputId) => {
const arias = {
"aria-labelledby": props?.["aria-labelledby"] !== void 0 ? props?.["aria-labelledby"] : getIdReferenceListFor("label", context?.descriptors, inputId),
"aria-describedby": props?.["aria-describedby"] !== void 0 ? props?.["aria-describedby"] : getIdReferenceListFor("description", context?.descriptors),
"aria-controls": props?.["aria-controls"] !== void 0 ? props?.["aria-controls"] : getIdReferenceListFor("controlled", context?.descriptors)
};
if (isInvalid) {
arias["aria-invalid"] = isInvalid;
arias["aria-errormessage"] = props?.["aria-errormessage"] !== void 0 ? props?.["aria-errormessage"] : getIdReferenceFor("errormessage", context?.descriptors);
}
return arias;
};
var isValid = (state) => state === "valid";
var isInvalid = (state) => state === "invalid";
//#endregion
export { buildAriaPropsFromContext, buildFormElementPropsFromContext, findDescriptors, isInvalid, isValid };