@wordpress/components
Version:
UI components for WordPress.
56 lines (55 loc) • 1.85 kB
JavaScript
// packages/components/src/validated-form-controls/components/form-token-field.tsx
import { forwardRef, useRef } from "@wordpress/element";
import { ControlWithError } from "../control-with-error";
import { FormTokenField } from "../../form-token-field";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
var UnforwardedValidatedFormTokenField = ({
required,
onValidate,
customValidity,
onChange,
markWhenOptional,
...restProps
}, forwardedRef) => {
const validityTargetRef = useRef(null);
const valueRef = useRef(restProps.value);
return /* @__PURE__ */ _jsxs("div", {
className: "components-validated-control__wrapper-with-error-delegate",
ref: forwardedRef,
children: [/* @__PURE__ */ _jsx(ControlWithError, {
required,
markWhenOptional,
onValidate: () => {
return onValidate?.(valueRef.current);
},
customValidity,
getValidityTarget: () => validityTargetRef.current,
children: /* @__PURE__ */ _jsx(FormTokenField, {
__next40pxDefaultSize: true,
__nextHasNoMarginBottom: true,
...restProps,
onChange: (value, ...args) => {
valueRef.current = value;
onChange?.(value, ...args);
}
})
}), /* @__PURE__ */ _jsx("input", {
className: "components-validated-control__error-delegate",
type: "text",
ref: validityTargetRef,
required,
value: valueRef.current && valueRef.current.length > 0 ? "hasvalue" : "",
tabIndex: -1,
onChange: () => {
},
onFocus: (e) => {
e.target.previousElementSibling?.querySelector('input[type="text"]')?.focus();
}
})]
});
};
var ValidatedFormTokenField = forwardRef(UnforwardedValidatedFormTokenField);
export {
ValidatedFormTokenField
};
//# sourceMappingURL=form-token-field.js.map