saasufy-components 4.2.0 → 4.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.
package/README.md CHANGED
@@ -580,6 +580,7 @@ Used to displaying a field of a model instance in real time.
580
580
  - `model-id` (required): The id of the model instance/record to bind to.
581
581
  - `model-field` (required): The field of the model instance/record to bind to for reading.
582
582
  - `slice-to`: Optional attribute which can be set to a number to trim strings down to a maximum number of characters when reading. This is useful when dealing with potentially very long field values. Note that it may affect caching if the same field is being referenced in multiple parts of the application at the same time within the same `socket-provider`.
583
+ - `default-value`: A default value to show the user if the underlying model field's value is null or undefined.
583
584
  - `hide-error-logs`: By default, this component will log errors to the console. If set, this attribute will suppress such errors from showing up on the console.
584
585
 
585
586
  ### model-viewer
package/input-provider.js CHANGED
@@ -54,11 +54,24 @@ class InputProvider extends HTMLElement {
54
54
  this.inputElement && this.inputElement.dispatchEvent(new Event('change'));
55
55
  }
56
56
 
57
+ updateInputClassList(newValue) {
58
+ if (this.inputElement.type === 'checkbox') {
59
+ this.inputElement.classList.remove('empty-input');
60
+ return;
61
+ }
62
+ if (newValue === '' || newValue == null) {
63
+ this.inputElement.classList.add('empty-input');
64
+ } else {
65
+ this.inputElement.classList.remove('empty-input');
66
+ }
67
+ }
68
+
57
69
  set value(newValue) {
58
70
  if (this.inputElement) {
59
71
  if (newValue && this.hasAttribute('computable-value')) {
60
72
  newValue = renderTemplate(newValue);
61
73
  }
74
+ this.updateInputClassList(newValue);
62
75
  let oldValue;
63
76
  if (this.inputElement.type === 'checkbox') {
64
77
  oldValue = this.inputElement.checked;
@@ -117,6 +130,9 @@ class InputProvider extends HTMLElement {
117
130
  let optionElements = options
118
131
  .split(',')
119
132
  .map((option) => {
133
+ if (option === placeholder) {
134
+ return `<option value="" selected class="select-default-option">${option}</option>`;
135
+ }
120
136
  return `<option value="${option}">${option}</option>`;
121
137
  })
122
138
  .join('');
@@ -153,6 +169,7 @@ class InputProvider extends HTMLElement {
153
169
  }
154
170
  }
155
171
 
172
+ this.updateInputClassList(value);
156
173
  this.appendChild(this.inputElement);
157
174
 
158
175
  let destroyHandlers = this.updateConsumerElementsOnEdit();
@@ -160,6 +177,7 @@ class InputProvider extends HTMLElement {
160
177
  if (value) {
161
178
  this.value = value;
162
179
  }
180
+
163
181
  this.destroy = () => {
164
182
  destroyHandlers();
165
183
  };
@@ -176,6 +194,7 @@ class InputProvider extends HTMLElement {
176
194
  let debounce = debouncer();
177
195
 
178
196
  let onInputChange = (event) => {
197
+ this.updateInputClassList(event.target.value);
179
198
  debounce(async () => {
180
199
  let elementName = this.getAttribute('name');
181
200
  let providerTemplate = this.getAttribute('provider-template');
@@ -183,6 +202,7 @@ class InputProvider extends HTMLElement {
183
202
  if (inputElement.type === 'checkbox') {
184
203
  let checked = !!inputElement.checked;
185
204
  this.lastValue = checked;
205
+
186
206
  updateConsumerElements(consumers, checked, providerTemplate, elementName);
187
207
  return;
188
208
  }
@@ -201,6 +221,7 @@ class InputProvider extends HTMLElement {
201
221
  let onInputKeyUp;
202
222
  if (inputElement.type !== 'checkbox') {
203
223
  onInputKeyUp = async (event) => {
224
+ this.updateInputClassList(event.target.value);
204
225
  debounce(async () => {
205
226
  let elementName = this.getAttribute('name');
206
227
  let providerTemplate = this.getAttribute('provider-template');
package/model-text.js CHANGED
@@ -25,7 +25,8 @@ class ModelText extends SocketConsumer {
25
25
  'model-id',
26
26
  'model-field',
27
27
  'slice-to',
28
- 'hide-error-logs'
28
+ 'hide-error-logs',
29
+ 'default-value'
29
30
  ];
30
31
  }
31
32
 
@@ -35,6 +36,14 @@ class ModelText extends SocketConsumer {
35
36
  this.render();
36
37
  }
37
38
 
39
+ computeDisplayValue(value) {
40
+ let safeValue = toSafeHTML(value);
41
+ if (safeValue != null) {
42
+ return safeValue;
43
+ }
44
+ return this.getAttribute('default-value') || '';
45
+ }
46
+
38
47
  render() {
39
48
  this.innerHTML = '';
40
49
  let socketInstanceProperty = this.getAttribute('socket-instance-property');
@@ -107,12 +116,11 @@ class ModelText extends SocketConsumer {
107
116
  (async () => {
108
117
  for await (let event of changeConsumer) {
109
118
  if (event.resourceField !== modelField) continue;
110
- let fieldValue = toSafeHTML(model.value[modelField]);
111
- this.innerHTML = fieldValue;
119
+ this.innerHTML = this.computeDisplayValue(model.value[modelField]);
112
120
  }
113
121
  })();
114
122
  if (model.isLoaded) {
115
- this.innerHTML = toSafeHTML(model.value[modelField]);
123
+ this.innerHTML = this.computeDisplayValue(model.value[modelField]);
116
124
  }
117
125
  this.destroy = () => {
118
126
  changeConsumer.kill();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "saasufy-components",
3
- "version": "4.2.0",
3
+ "version": "4.4.0",
4
4
  "description": "Components for Saasufy.",
5
5
  "main": "index.js",
6
6
  "scripts": {