nsw-design-system 3.2.5 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,56 @@
1
+ .nsw-file-upload {
2
+ margin-top: rem(8px);
3
+ display: block;
4
+
5
+ &__label {
6
+ margin: 0;
7
+ }
8
+
9
+ &__input {
10
+ position: absolute;
11
+ opacity: 0;
12
+
13
+ &:disabled {
14
+ + .nsw-file-upload__label {
15
+ opacity: 0.4;
16
+ cursor: not-allowed;
17
+ }
18
+ }
19
+
20
+ &:focus + .nsw-file-upload__label {
21
+ @include nsw-focus;
22
+ }
23
+ }
24
+
25
+ &__list {
26
+ display: none;
27
+ margin: 0;
28
+ padding: 0;
29
+ border: 0;
30
+
31
+ &.active {
32
+ display: block;
33
+ }
34
+ }
35
+
36
+ &__item {
37
+ display: flex;
38
+ max-width: rem(500px);
39
+ align-items: center;
40
+ justify-content: space-between;
41
+ background-color: var(--nsw-off-white);
42
+ border-radius: var(--nsw-border-radius);
43
+ padding: rem(8px);
44
+
45
+ &:not(:last-child) {
46
+ margin-bottom: rem(8px);
47
+ }
48
+ }
49
+
50
+ &__item-filename {
51
+ margin-left: rem(8px);
52
+ overflow: hidden;
53
+ text-overflow: ellipsis;
54
+ white-space: nowrap;
55
+ }
56
+ }
@@ -0,0 +1,87 @@
1
+ /* eslint-disable max-len */
2
+ class FileUpload {
3
+ constructor(element) {
4
+ this.element = element
5
+ this.input = this.element.querySelector('.nsw-file-upload__input')
6
+ this.label = this.element.querySelector('.nsw-file-upload__label')
7
+ this.multipleUpload = this.input.hasAttribute('multiple')
8
+ this.replaceFiles = this.element.hasAttribute('data-replace-files')
9
+ this.filesList = false
10
+ }
11
+
12
+ init() {
13
+ if (!this.input) return
14
+
15
+ this.input.addEventListener('change', () => {
16
+ if (this.input.value === '') return
17
+ this.updateFileList()
18
+ })
19
+ }
20
+
21
+ createFileList() {
22
+ const ul = document.createElement('ul')
23
+ ul.classList.add('nsw-file-upload__list')
24
+ this.label.insertAdjacentElement('afterend', ul)
25
+ this.filesList = this.element.querySelector('.nsw-file-upload__list')
26
+ }
27
+
28
+ createFileItem(file) {
29
+ const li = document.createElement('li')
30
+ li.classList.add('nsw-file-upload__item')
31
+ const html = `
32
+ <span class="nsw-file-upload__item-filename"></span>
33
+ <button type="button" class="nsw-icon-button">
34
+ <span class="sr-only">Remove file</span>
35
+ <span class="material-icons nsw-material-icons" focusable="false" aria-hidden="true">cancel</span>
36
+ </button>`
37
+
38
+ li.insertAdjacentHTML('afterbegin', html)
39
+ li.querySelector('.nsw-file-upload__item-filename').textContent = this.constructor.truncateString(file.name, 50)
40
+ return li.outerHTML
41
+ }
42
+
43
+ updateFileList() {
44
+ if (!this.filesList) {
45
+ this.createFileList()
46
+ }
47
+
48
+ this.filesList.classList.add('active')
49
+
50
+ let string = ''
51
+ for (let i = 0; i < this.input.files.length; i += 1) {
52
+ const file = this.input.files[i]
53
+ string = this.createFileItem(file) + string
54
+ }
55
+
56
+ if (this.replaceFiles) {
57
+ this.filesList.innerHTML = string
58
+ } else {
59
+ this.filesList.insertAdjacentHTML('beforeend', string)
60
+ }
61
+
62
+ this.removeFile()
63
+ }
64
+
65
+ removeFile() {
66
+ this.filesList.addEventListener('click', (event) => {
67
+ if (!event.target.closest('.nsw-icon-button')) return
68
+ event.preventDefault()
69
+ const item = event.target.closest('.nsw-file-upload__item')
70
+
71
+ item.remove()
72
+
73
+ if (event.currentTarget.children.length === 0) {
74
+ this.filesList.classList.remove('active')
75
+ }
76
+ })
77
+ }
78
+
79
+ static truncateString(str, num) {
80
+ if (str.length <= num) {
81
+ return str
82
+ }
83
+ return `${str.slice(0, num)}...`
84
+ }
85
+ }
86
+
87
+ export default FileUpload