Skip to content

Commit 5741311

Browse files
feature (form): file input in form framework
1 parent c810912 commit 5741311

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

public/assets/components/form.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createElement } from "../lib/skeleton/index.js";
2+
import { qs } from "../lib/dom.js";
23
import { gid } from "../lib/random.js";
34
import { ApplicationError } from "../lib/error.js";
45

@@ -267,14 +268,35 @@ export function $renderInput(options = {}) {
267268
$img.setAttribute("src", value);
268269
return $img;
269270
}
270-
case "file": { // TODO
271-
return createElement(`
272-
<input
273-
type="password"
274-
class="component_input"
275-
readonly
276-
/>
271+
case "file": {
272+
const $file = createElement(`
273+
<div className="fileupload-image">
274+
<input
275+
type="file"
276+
class="component_input"
277+
/>
278+
<div class="preview" style="margin-top:-15px;"></div>
279+
</div>
277280
`);
281+
const $preview = qs($file, ".preview");
282+
const draw = (val) => {
283+
$preview.innerHTML = "";
284+
if ((val || "").substring(0, 10) === "data:image") $preview.appendChild(createElement(`
285+
<img class="full-width" src="${val}" />
286+
`));
287+
else if ((val || "").substring(0, 20) === "data:application/pdf") $preview.appendChild(createElement(`
288+
<object class="full-width" type="application/pdf" data="${val}" style="height:250px;" />
289+
`));
290+
console.log("DRAW", $preview, (val || "").substring(0, 10))
291+
};
292+
qs($file, "input").onchange = (e) => {
293+
if (e.target.files.length === 0) return;
294+
const reader = new window.FileReader();
295+
reader.readAsDataURL(e.target.files[0]);
296+
reader.onload = () => draw(reader.result);
297+
};
298+
draw(value);
299+
return $file;
278300
}
279301
default: {
280302
const $input = createElement(`

public/assets/css/designsystem_input.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@ input.component_input[disabled], textarea.component_textarea[disabled] {
2626
opacity: 0.8;
2727
}
2828

29+
.component_input[type="file"] { border: none; margin-bottom: 0; }
30+
.component_input[type="file"]::file-selector-button {
31+
border-radius: 3px;
32+
background-color: transparent;
33+
border: 2px solid var(--border);
34+
display: block;
35+
transition: 0.1s all;
36+
color: inherit;
37+
cursor: pointer;
38+
}
39+
.component_input[type="file"]::file-selector-button:hover {
40+
background-color: var(--border);
41+
border-color: var(--border);
42+
}
43+
2944
.component_input:-webkit-autofill,
3045
.component_input:-webkit-autofill:focus {
3146
transition: background-color 600000s 0s, color 600000s 0s;

0 commit comments

Comments
 (0)