Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | 1x 1x 1x 1x 1x 1x | import Field from "../Field";
import { CustomElementPropertyMetadata } from "../../../custom-element/interfaces";
import defineCustomElement from "../../../custom-element/helpers/defineCustomElement";
import { NodePatchingData } from "../../../renderer/NodePatcher";
import html from "../../../renderer/html";
function formatSize(fileSize) {
if (fileSize < 1024) {
return fileSize + 'bytes';
}
else if (fileSize >= 1024 && fileSize < 1048576) {
return (fileSize / 1024).toFixed(1) + 'KB';
}
else if (fileSize >= 1048576) {
return (fileSize / 1048576).toFixed(1) + 'MB';
}
}
export default class FileField extends Field {
static get properties(): Record<string, CustomElementPropertyMetadata> {
return {
accept: {
type: String
},
capture: {
type: Boolean,
value: true
},
multiple: {
type: Boolean
},
/** Whether to preview the files (that can be previewed) */
preview: {
type: Boolean
}
};
}
render(): NodePatchingData {
const {
name,
//value,
accept,
capture,
multiple,
//required,
disabled,
//preview
} = this;
// Note: opacity is used to hide the file input instead of visibility: hidden or display: none, because assistive technology interprets the latter two styles to mean the file input isn't interactive.
return html`
<input
style="opacity: 0; position: absolute;"
type="file"
name=${name}
id=${name}
accept=${accept}
capture=${capture}
multiple=${multiple}
disabled=${disabled}
onInput=${event => this.handleInput(event)}
onChange=${event => this.handleChange(event)}
onBlur=${event => this.handleBlur(event)}
/>
${this.renderFileList()}
<gcl-button kind="secondary" variant="contained" click=${() => this.openFileDialog()}>
<gcl-icon name="upload"></gcl-icon>
<gcl-localized-text>Click here to upload files</gcl-localized-text>
</gcl-button>`;
}
openFileDialog(): void {
const {
name
} = this;
this.document.getElementById(name).click();
}
renderFileList(): NodePatchingData[] {
const {
preview,
value,
} = this;
if (preview === false) {
return null;
}
if (value === undefined) {
return null;
}
const data = Array.isArray(value) ? value : [value]; // Ensure it is an array
return data.map(record => {
const {
name,
content,
size
} = record;
// The content can be either read from the server or selected from a File object
const src = content.indexOf('blob:') === -1 ?
`data:image/jpeg;base64,${content}` :
content;
return html`
<gcl-row value={name}>
<img style="width: 48px; height: 48px;" src=${src} />
<span>${name}</span>
<span>${formatSize(size)}</span>
</gcl-row>`;
});
}
}
defineCustomElement('gcl-file-field', FileField); |