UNPKG

@wordpress/editor

Version:
85 lines (84 loc) 2.89 kB
// packages/editor/src/components/collab-sidebar/note-form.js import TextareaAutosize from "react-autosize-textarea"; import { useState } from "@wordpress/element"; import { __experimentalTruncate as Truncate, Button } from "@wordpress/components"; import { Stack, VisuallyHidden } from "@wordpress/ui"; import { __ } from "@wordpress/i18n"; import { useInstanceId } from "@wordpress/compose"; import { isKeyboardEvent } from "@wordpress/keycodes"; import { sanitizeNoteContent } from "./utils.mjs"; import { jsx, jsxs } from "react/jsx-runtime"; function NoteForm({ onSubmit, onCancel, note, labels }) { const [inputComment, setInputComment] = useState( note?.content?.raw ?? "" ); const inputId = useInstanceId(NoteForm, "comment-input"); const isDisabled = inputComment === note?.content?.raw || !sanitizeNoteContent(inputComment).length; return /* @__PURE__ */ jsxs( Stack, { className: "editor-collab-sidebar-panel__note-form", direction: "column", gap: "lg", render: /* @__PURE__ */ jsx("form", {}), onSubmit: (event) => { event.preventDefault(); onSubmit(inputComment); setInputComment(""); }, children: [ /* @__PURE__ */ jsx(VisuallyHidden, { render: /* @__PURE__ */ jsx("label", { htmlFor: inputId }), children: labels?.input ?? __("Note") }), /* @__PURE__ */ jsx( TextareaAutosize, { id: inputId, value: inputComment ?? "", onChange: (comment) => setInputComment(comment.target.value), rows: 1, maxRows: 20, onKeyDown: (event) => { if (isKeyboardEvent.primary(event, "Enter") && !isDisabled) { event.target.parentNode.requestSubmit(); } if (event.key === "Escape") { event.preventDefault(); onCancel(event); } } } ), /* @__PURE__ */ jsxs( Stack, { direction: "row", align: "center", justify: "flex-end", gap: "sm", wrap: "wrap", children: [ /* @__PURE__ */ jsx(Button, { size: "compact", variant: "tertiary", onClick: onCancel, children: /* @__PURE__ */ jsx(Truncate, { children: __("Cancel") }) }), /* @__PURE__ */ jsx( Button, { size: "compact", accessibleWhenDisabled: true, variant: "primary", type: "submit", disabled: isDisabled, children: /* @__PURE__ */ jsx(Truncate, { children: labels?.submit ?? __("Add note") }) } ) ] } ) ] } ); } export { NoteForm }; //# sourceMappingURL=note-form.mjs.map