@wordpress/block-library
Version:
Block library for the WordPress editor.
184 lines (183 loc) • 5.74 kB
JavaScript
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import clsx from "clsx";
import { __ } from "@wordpress/i18n";
import {
InspectorControls,
RichText,
useBlockProps,
__experimentalUseBorderProps as useBorderProps,
__experimentalUseColorProps as useColorProps
} from "@wordpress/block-editor";
import {
TextControl,
CheckboxControl,
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem
} from "@wordpress/components";
import { useRef } from "@wordpress/element";
import { useToolsPanelDropdownMenuProps } from "../utils/hooks";
function InputFieldBlock({ attributes, setAttributes, className }) {
const { type, name, label, inlineLabel, required, placeholder, value } = attributes;
const blockProps = useBlockProps();
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
const ref = useRef();
const TagName = type === "textarea" ? "textarea" : "input";
const borderProps = useBorderProps(attributes);
const colorProps = useColorProps(attributes);
if (ref.current) {
ref.current.focus();
}
const isCheckboxOrRadio = type === "checkbox" || type === "radio";
const controls = /* @__PURE__ */ jsxs(Fragment, { children: [
"hidden" !== type && /* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsxs(
ToolsPanel,
{
label: __("Settings"),
resetAll: () => {
setAttributes({
inlineLabel: false,
required: false
});
},
dropdownMenuProps,
children: [
"checkbox" !== type && /* @__PURE__ */ jsx(
ToolsPanelItem,
{
label: __("Inline label"),
hasValue: () => !!inlineLabel,
onDeselect: () => setAttributes({ inlineLabel: false }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(
CheckboxControl,
{
__nextHasNoMarginBottom: true,
label: __("Inline label"),
checked: inlineLabel,
onChange: (newVal) => {
setAttributes({
inlineLabel: newVal
});
}
}
)
}
),
/* @__PURE__ */ jsx(
ToolsPanelItem,
{
label: __("Required"),
hasValue: () => !!required,
onDeselect: () => setAttributes({ required: false }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(
CheckboxControl,
{
__nextHasNoMarginBottom: true,
label: __("Required"),
checked: required,
onChange: (newVal) => {
setAttributes({
required: newVal
});
}
}
)
}
)
]
}
) }),
/* @__PURE__ */ jsx(InspectorControls, { group: "advanced", children: /* @__PURE__ */ jsx(
TextControl,
{
__next40pxDefaultSize: true,
__nextHasNoMarginBottom: true,
autoComplete: "off",
label: __("Name"),
value: name,
onChange: (newVal) => {
setAttributes({
name: newVal
});
},
help: __(
'Affects the "name" attribute of the input element, and is used as a name for the form submission results.'
)
}
) })
] });
const content = /* @__PURE__ */ jsx(
RichText,
{
tagName: "span",
className: "wp-block-form-input__label-content",
value: label,
onChange: (newLabel) => setAttributes({ label: newLabel }),
"aria-label": label ? __("Label") : __("Empty label"),
"data-empty": !label,
placeholder: __("Type the label for this input")
}
);
if ("hidden" === type) {
return /* @__PURE__ */ jsxs(Fragment, { children: [
controls,
/* @__PURE__ */ jsx(
"input",
{
type: "hidden",
className: clsx(
className,
"wp-block-form-input__input",
colorProps.className,
borderProps.className
),
"aria-label": __("Value"),
value,
onChange: (event) => setAttributes({ value: event.target.value })
}
)
] });
}
return /* @__PURE__ */ jsxs("div", { ...blockProps, children: [
controls,
/* @__PURE__ */ jsxs(
"span",
{
className: clsx("wp-block-form-input__label", {
"is-label-inline": inlineLabel || "checkbox" === type
}),
children: [
!isCheckboxOrRadio && content,
/* @__PURE__ */ jsx(
TagName,
{
type: "textarea" === type ? void 0 : type,
className: clsx(
className,
"wp-block-form-input__input",
colorProps.className,
borderProps.className
),
"aria-label": __("Optional placeholder text"),
placeholder: placeholder ? void 0 : __("Optional placeholder\u2026"),
value: placeholder,
onChange: (event) => setAttributes({ placeholder: event.target.value }),
"aria-required": required,
style: {
...borderProps.style,
...colorProps.style
}
}
),
isCheckboxOrRadio && content
]
}
)
] });
}
var edit_default = InputFieldBlock;
export {
edit_default as default
};
//# sourceMappingURL=edit.js.map