UNPKG

@wordpress/components

Version:
50 lines (49 loc) • 2.17 kB
import clsx from "clsx"; import { forwardRef, useRef } from "@wordpress/element"; import { useMergeRefs } from "@wordpress/compose"; import { ControlWithError } from "@wordpress/ui"; import ContentEditableControl from "../../content-editable-control/index.mjs"; import { jsx, jsxs } from "react/jsx-runtime"; // packages/components/src/validated-form-controls/components/content-editable-control.tsx /** * A `contentEditable` field does not participate in the Constraint Validation * API, so — like `ValidatedToggleGroupControl` — the validity state lives on a * hidden delegate input that mirrors whether the field has content. */ const UnforwardedValidatedContentEditableControl = ({ required, customValidity, markWhenOptional, value, className, ...restProps }, forwardedRef) => { const validityTargetRef = useRef(null); const editableRef = useRef(null); const mergedRef = useMergeRefs([editableRef, forwardedRef]); const getInteractiveTarget = () => editableRef.current; return /* @__PURE__ */ jsxs("div", { className: "components-validated-control__wrapper-with-error-delegate", children: [/* @__PURE__ */ jsx(ControlWithError, { className: "components-validated-control", required, markWhenOptional, customValidity, getValidityTarget: () => validityTargetRef.current, getInteractiveTarget, children: /* @__PURE__ */ jsx(ContentEditableControl, { ref: mergedRef, className: clsx("components-validated-control__content-editable", className), "aria-invalid": customValidity?.type === "invalid" || void 0, ...restProps }) }), /* @__PURE__ */ jsx("input", { className: "components-validated-control__error-delegate", type: "text", ref: validityTargetRef, required, value: value?.trim() ?? "", tabIndex: -1, "aria-hidden": "true", onChange: () => {}, onFocus: () => getInteractiveTarget()?.focus() })] }); }; const ValidatedContentEditableControl = forwardRef(UnforwardedValidatedContentEditableControl); ValidatedContentEditableControl.displayName = "ValidatedContentEditableControl"; export { ValidatedContentEditableControl }; //# sourceMappingURL=content-editable-control.mjs.map