1
0
Fork 0
VaultUI/src/elements/CopyableInputBox.js

48 lines
1.2 KiB
JavaScript
Raw Normal View History

2021-05-03 09:25:42 +01:00
import { MarginInline } from "./MarginInline.js";
import { addClipboardNotifications } from "../pageUtils.js";
import { makeElement } from "../htmlUtils";
2021-05-03 09:25:42 +01:00
import ClipboardJS from "clipboard";
import i18next from "i18next";
2021-04-15 13:01:58 +01:00
export function CopyableInputBox(text, copyable = true) {
let inputBoxDiv = makeElement({ tag: "div" });
let inputBoxCopyButton = null;
if (copyable) {
inputBoxCopyButton = makeElement({
tag: "a",
class: "uk-form-icon",
attributes: {
"uk-icon": "icon: copy",
"role": "img",
"aria-label": i18next.t("copy_input_box_copy_icon_text")
2021-05-07 12:07:51 +01:00
},
thenRun: (e) => {
let clipboard = new ClipboardJS(e);
addClipboardNotifications(clipboard, 600);
}
});
2021-04-15 13:01:58 +01:00
}
let inputBoxInput = makeElement({
tag: "input",
class: ["uk-input"],
attributes: { "readonly": true, "type": "text" },
});
let inputBoxInner = MarginInline([
inputBoxCopyButton,
inputBoxInput
]);
inputBoxDiv.appendChild(inputBoxInner);
inputBoxDiv.setText = function (text) {
inputBoxInput.value = `${text}`;
if (copyable) {
inputBoxCopyButton.dataset.clipboardText = `${text}`;
}
};
inputBoxDiv.setText(text);
return inputBoxDiv;
}