saasufy-components 3.4.1 → 3.6.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.
- package/collection-adder.js +48 -24
- package/model-input.js +19 -4
- package/package.json +1 -1
- package/styles.css +2 -2
package/collection-adder.js
CHANGED
|
@@ -63,31 +63,47 @@ class CollectionAdder extends SocketConsumer {
|
|
|
63
63
|
|
|
64
64
|
let trimSpaces = this.hasAttribute('trim-spaces');
|
|
65
65
|
|
|
66
|
-
let newModelData = {
|
|
67
|
-
...this.modelFieldValues,
|
|
68
|
-
...radioData,
|
|
69
|
-
...Object.fromEntries(
|
|
70
|
-
[...this.querySelectorAll('.collection-adder-input')]
|
|
71
|
-
.filter(input => input.value !== '')
|
|
72
|
-
.map((input) => {
|
|
73
|
-
let fieldType = this.fieldTypes[input.name];
|
|
74
|
-
let Type = getTypeCastFunction(fieldType);
|
|
75
|
-
let value;
|
|
76
|
-
if (fieldType === 'checkbox') {
|
|
77
|
-
value = input.checked;
|
|
78
|
-
} else {
|
|
79
|
-
value = input.value;
|
|
80
|
-
}
|
|
81
|
-
let sanitizedValue = Type(value);
|
|
82
|
-
if (trimSpaces && typeof sanitizedValue === 'string') {
|
|
83
|
-
sanitizedValue = sanitizedValue.trim();
|
|
84
|
-
}
|
|
85
|
-
return [ input.name, sanitizedValue ];
|
|
86
|
-
})
|
|
87
|
-
)
|
|
88
|
-
};
|
|
89
|
-
|
|
90
66
|
try {
|
|
67
|
+
let newModelData = {
|
|
68
|
+
...this.modelFieldValues,
|
|
69
|
+
...radioData,
|
|
70
|
+
...Object.fromEntries(
|
|
71
|
+
await Promise.all(
|
|
72
|
+
[...this.querySelectorAll('.collection-adder-input')].filter(
|
|
73
|
+
input => input.value !== ''
|
|
74
|
+
)
|
|
75
|
+
.map(
|
|
76
|
+
async (input) => {
|
|
77
|
+
let fieldType = this.fieldTypes[input.name];
|
|
78
|
+
let Type = getTypeCastFunction(fieldType);
|
|
79
|
+
let value;
|
|
80
|
+
if (input.type === 'file' && input.files && input.files.length) {
|
|
81
|
+
let reader = new FileReader();
|
|
82
|
+
let readerLoadPromise = new Promise((resolve, reject) => {
|
|
83
|
+
reader.addEventListener('load', () => {
|
|
84
|
+
resolve(reader.result);
|
|
85
|
+
});
|
|
86
|
+
reader.addEventListener('error', () => {
|
|
87
|
+
reject(new Error('Failed to read file'));
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
reader.readAsDataURL(input.files[0]);
|
|
91
|
+
value = await readerLoadPromise;
|
|
92
|
+
} else if (fieldType === 'checkbox') {
|
|
93
|
+
value = input.checked;
|
|
94
|
+
} else {
|
|
95
|
+
value = input.value;
|
|
96
|
+
}
|
|
97
|
+
let sanitizedValue = Type(value);
|
|
98
|
+
if (trimSpaces && typeof sanitizedValue === 'string') {
|
|
99
|
+
sanitizedValue = sanitizedValue.trim();
|
|
100
|
+
}
|
|
101
|
+
return [ input.name, sanitizedValue ];
|
|
102
|
+
}
|
|
103
|
+
)
|
|
104
|
+
)
|
|
105
|
+
)
|
|
106
|
+
};
|
|
91
107
|
await this.collection.create(newModelData);
|
|
92
108
|
messageContainer.classList.add('success');
|
|
93
109
|
messageContainer.classList.remove('error');
|
|
@@ -250,6 +266,14 @@ class CollectionAdder extends SocketConsumer {
|
|
|
250
266
|
inputLabel
|
|
251
267
|
}"${extraAttributesString}></textarea>`
|
|
252
268
|
);
|
|
269
|
+
} else if (inputType === 'file') {
|
|
270
|
+
items.push(
|
|
271
|
+
`<input class="collection-adder-input" type="${
|
|
272
|
+
inputType
|
|
273
|
+
}" name="${
|
|
274
|
+
field
|
|
275
|
+
}"${extraAttributesString} />`
|
|
276
|
+
);
|
|
253
277
|
} else {
|
|
254
278
|
items.push(
|
|
255
279
|
`<input class="collection-adder-input" type="${
|
package/model-input.js
CHANGED
|
@@ -167,10 +167,11 @@ class ModelInput extends SocketConsumer {
|
|
|
167
167
|
elementType = 'input';
|
|
168
168
|
}
|
|
169
169
|
this.inputElement = document.createElement(elementType);
|
|
170
|
+
let optionList = null;
|
|
170
171
|
if (type === 'select') {
|
|
171
172
|
if (options) {
|
|
172
|
-
|
|
173
|
-
|
|
173
|
+
optionList = options.split(',');
|
|
174
|
+
let optionElements = optionList
|
|
174
175
|
.map((option) => {
|
|
175
176
|
return `<option value="${option}">${option}</option>`;
|
|
176
177
|
})
|
|
@@ -208,7 +209,7 @@ class ModelInput extends SocketConsumer {
|
|
|
208
209
|
this.appendChild(errorMessageContainer);
|
|
209
210
|
}
|
|
210
211
|
this.appendChild(this.inputElement);
|
|
211
|
-
let destroyInputSync = this.syncInputElementWithModel(this.inputElement, model, modelField, errorMessageContainer);
|
|
212
|
+
let destroyInputSync = this.syncInputElementWithModel(this.inputElement, model, modelField, errorMessageContainer, optionList);
|
|
212
213
|
this.destroy = () => {
|
|
213
214
|
destroyInputSync();
|
|
214
215
|
this.isLoadedConsumer && this.isLoadedConsumer.kill();
|
|
@@ -233,9 +234,22 @@ class ModelInput extends SocketConsumer {
|
|
|
233
234
|
}
|
|
234
235
|
}
|
|
235
236
|
|
|
236
|
-
|
|
237
|
+
verifyValue(fieldValue, optionList, inputElement, messageContainerElement) {
|
|
238
|
+
if (optionList && !optionList.includes(fieldValue)) {
|
|
239
|
+
inputElement.classList.add('error');
|
|
240
|
+
inputElement.classList.remove('success');
|
|
241
|
+
if (messageContainerElement) {
|
|
242
|
+
messageContainerElement.textContent = 'Invalid selection';
|
|
243
|
+
messageContainerElement.classList.add('error');
|
|
244
|
+
messageContainerElement.classList.remove('hidden');
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
syncInputElementWithModel(inputElement, model, fieldName, messageContainerElement, optionList) {
|
|
237
250
|
if (model.isLoaded) {
|
|
238
251
|
let fieldValue = model.value[fieldName];
|
|
252
|
+
this.verifyValue(fieldValue, optionList, inputElement, messageContainerElement);
|
|
239
253
|
this.updateInputElement(inputElement, fieldValue);
|
|
240
254
|
}
|
|
241
255
|
let stopSaving = this.saveInputElementOnEdit(inputElement, model, fieldName, messageContainerElement);
|
|
@@ -245,6 +259,7 @@ class ModelInput extends SocketConsumer {
|
|
|
245
259
|
let hasFocus = document.activeElement === this.inputElement;
|
|
246
260
|
if (event.resourceField !== fieldName || (hasFocus && !event.isRemote)) continue;
|
|
247
261
|
let fieldValue = model.value[fieldName];
|
|
262
|
+
this.verifyValue(fieldValue, optionList, inputElement, messageContainerElement);
|
|
248
263
|
this.updateInputElement(inputElement, fieldValue);
|
|
249
264
|
}
|
|
250
265
|
})();
|
package/package.json
CHANGED
package/styles.css
CHANGED
|
@@ -26,12 +26,12 @@ input[type=text]:focus,input[type=password]:focus,input[type=number]:focus,selec
|
|
|
26
26
|
outline-color: #00a5e5;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
input[type=text].error
|
|
29
|
+
input[type=text].error,input[type=password].error,input[type=number].error,select.error,textarea.error {
|
|
30
30
|
border-color: #ff0000;
|
|
31
31
|
outline-color: #ff0000;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
input[type=text].warning
|
|
34
|
+
input[type=text].warning,input[type=password].warning,input[type=number].warning,select.warning,textarea.warning {
|
|
35
35
|
border-color: #ee9900;
|
|
36
36
|
outline-color: #ee9900;
|
|
37
37
|
}
|