@veracity/vui
Version:
Veracity UI is a React component library crafted for use within Veracity applications and pages. Based on Styled Components and @xstyled.
56 lines (54 loc) • 2.3 kB
JavaScript
import { isString } from "../utils/assertion.js";
import { helpTextMargin } from "./consts.js";
import { Label } from "../label/label.js";
import { cloneElement, isValidElement } from "react";
import { jsx } from "react/jsx-runtime";
//#region src/textField/helpers.tsx
/** Returns length of the initial field's value. */
function getInitialCount(props) {
const { defaultValue, value } = props;
return defaultValue?.toString().length ?? value?.toString().length ?? 0;
}
/** Reserves horizontal space for the counter so help/error text doesn't run underneath it. */
function getHelpTextMargin(showCount) {
return showCount ? helpTextMargin : void 0;
}
/** Truncates a value to `maxLength`, if set. */
function clampValue(value, maxLength) {
return maxLength ? value.slice(0, maxLength) : value;
}
/** Builds a minimal synthetic change event carrying just the given value. */
function createSyntheticChangeEvent(value, type) {
return {
target: { value },
type
};
}
/** Predicate for filtering autocomplete options against the field's current value. */
function filterAutoCompleteOption(currentValue, option) {
return currentValue === option ? false : !currentValue || option.toLowerCase().includes(currentValue.toLowerCase());
}
/** Matches `Label` elements, including thin wrappers (styled/memo) that preserve the `Label` display name. */
function isLabelElement(label) {
if (!isValidElement(label)) return false;
const type = label.type;
return label.type === Label || type?.displayName === Label.displayName;
}
/** Resolves the `label` prop: wraps strings in `Label`, auto-wires `htmlFor` onto `Label` elements. */
function resolveLabel(label, id, disabled, labelMarkOptional, labelTooltipText, labelStyles) {
if (isString(label)) return /* @__PURE__ */ jsx(Label, {
"aria-disabled": disabled,
className: "vui-textFieldLabel",
htmlFor: id,
markAsOptional: labelMarkOptional,
text: label,
tooltipText: labelTooltipText,
...labelStyles
});
if (isLabelElement(label)) return cloneElement(label, { htmlFor: label.props.htmlFor ?? id });
return label;
}
//#endregion
export { clampValue, createSyntheticChangeEvent, filterAutoCompleteOption, getHelpTextMargin, getInitialCount, resolveLabel };
globalThis.__vuiVersion__ = "5.4.0-alpha"
//# sourceMappingURL=helpers.js.map