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

52 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-05-03 09:25:42 +01:00
import { MarginInline } from "./MarginInline.js";
2021-05-07 23:21:38 +01:00
import { addClipboardNotifications } from "../pageUtils";
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
2021-05-08 00:28:31 +01:00
interface CopyableInputBoxType extends HTMLElement {
setText(text: string): void;
}
export function CopyableInputBox(text: string, copyable = true): CopyableInputBoxType {
const inputBoxDiv = (makeElement({ tag: "div" }) as CopyableInputBoxType);
2021-04-15 13:01:58 +01:00
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) => {
2021-05-08 00:28:31 +01:00
const clipboard = new ClipboardJS(e);
2021-05-07 12:07:51 +01:00
addClipboardNotifications(clipboard, 600);
}
});
2021-04-15 13:01:58 +01:00
}
2021-05-08 00:28:31 +01:00
const inputBoxInput = makeElement({
2021-04-15 13:01:58 +01:00
tag: "input",
class: ["uk-input"],
attributes: { "readonly": true, "type": "text" },
});
2021-05-08 00:28:31 +01:00
const inputBoxInner = MarginInline([
2021-04-15 13:01:58 +01:00
inputBoxCopyButton,
inputBoxInput
]);
inputBoxDiv.appendChild(inputBoxInner);
inputBoxDiv.setText = function (text) {
2021-05-08 00:28:31 +01:00
(inputBoxInput as HTMLInputElement).value = `${text}`;
2021-04-15 13:01:58 +01:00
if (copyable) {
inputBoxCopyButton.dataset.clipboardText = `${text}`;
}
};
inputBoxDiv.setText(text);
return inputBoxDiv;
}