UNPKG

@wordpress/block-library

Version:
62 lines (61 loc) 2.3 kB
import { jsx, jsxs } from "react/jsx-runtime"; import clsx from "clsx"; import removeAccents from "remove-accents"; import { RichText, useBlockProps, __experimentalGetBorderClassesAndStyles as getBorderClassesAndStyles, __experimentalGetColorClassesAndStyles as getColorClassesAndStyles } from "@wordpress/block-editor"; import { __unstableStripHTML as stripHTML } from "@wordpress/dom"; const getNameFromLabel = (content) => { return removeAccents(stripHTML(content)).replace(/[^\p{L}\p{N}]+/gu, "-").toLowerCase().replace(/(^-+)|(-+$)/g, ""); }; function save({ attributes }) { const { type, name, label, inlineLabel, required, placeholder, value } = attributes; const borderProps = getBorderClassesAndStyles(attributes); const colorProps = getColorClassesAndStyles(attributes); const inputStyle = { ...borderProps.style, ...colorProps.style }; const inputClasses = clsx( "wp-block-form-input__input", colorProps.className, borderProps.className ); const TagName = type === "textarea" ? "textarea" : "input"; const blockProps = useBlockProps.save(); const isCheckboxOrRadio = type === "checkbox" || type === "radio"; if ("hidden" === type) { return /* @__PURE__ */ jsx("input", { type, name, value }); } return /* @__PURE__ */ jsx("div", { ...blockProps, children: /* @__PURE__ */ jsxs( "label", { className: clsx("wp-block-form-input__label", { "is-label-inline": inlineLabel }), children: [ !isCheckboxOrRadio && /* @__PURE__ */ jsx("span", { className: "wp-block-form-input__label-content", children: /* @__PURE__ */ jsx(RichText.Content, { value: label }) }), /* @__PURE__ */ jsx( TagName, { className: inputClasses, type: "textarea" === type ? void 0 : type, name: name || getNameFromLabel(label), required, "aria-required": required, placeholder: placeholder || void 0, style: inputStyle } ), isCheckboxOrRadio && /* @__PURE__ */ jsx("span", { className: "wp-block-form-input__label-content", children: /* @__PURE__ */ jsx(RichText.Content, { value: label }) }) ] } ) }); } export { save as default }; //# sourceMappingURL=save.js.map