nbhi 1.0.1 → 1.0.2

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.
@@ -79,18 +79,34 @@ function define({ name, template, script, styleSheets, observer }) {
79
79
  }
80
80
 
81
81
  attributeChangedCallback(attribute, oldValue, newValue) {
82
- if (oldValue !== newValue) {
82
+ if (oldValue === newValue) {
83
+ return;
84
+ }
85
+ const state = states.get(this);
86
+ if (hasDefault(state, attribute)) {
87
+ // Like the attribute of a native field, it sets the default value: the
88
+ // field shows it until the value is changed, and resetting the form goes
89
+ // back to it. The browser doesn't validate a step or length on it either
90
+ if (attribute === 'value') {
91
+ state.field.defaultValue = newValue ?? '';
92
+ } else {
93
+ state.field.defaultChecked = newValue !== null;
94
+ }
95
+ sync(this);
96
+ } else {
83
97
  this[toProperty(attribute)] = newValue;
84
98
  }
85
99
  }
86
100
 
87
101
  formResetCallback() {
88
102
  const { field, initial } = states.get(this);
89
- if (initial) {
103
+ if (field.localName === 'select' && initial) {
90
104
  field.value = initial.value;
91
- field.checked = initial.checked;
92
- sync(this);
105
+ } else if (field.localName !== 'select') {
106
+ field.value = field.defaultValue;
107
+ field.checked = field.defaultChecked;
93
108
  }
109
+ sync(this);
94
110
  }
95
111
 
96
112
  formDisabledCallback(disabled) {
@@ -157,12 +173,21 @@ function write(element, property, value) {
157
173
  select.value = previous;
158
174
  } else {
159
175
  state.bindings
160
- .filter(other => other.element === select && other.kind === 'attribute')
176
+ .filter(other => other.element === select && other.kind === 'attribute' && state.values.has(other.name))
161
177
  .forEach(other => apply(other, state.values.get(other.name)));
162
178
  }
163
179
  }
164
180
  }
165
181
 
182
+ // The value and checked attributes of a component set the default of its field
183
+ // when no binding sends them to another element
184
+ function hasDefault({ field, bindings }, attribute) {
185
+ return Boolean(field)
186
+ && field.localName !== 'select'
187
+ && fieldProperties.includes(attribute)
188
+ && bindings.every(binding => binding.name !== attribute || binding.element === field);
189
+ }
190
+
166
191
  function sync(element) {
167
192
  const { field, internals } = states.get(element);
168
193
  if (field.type === 'checkbox' || field.type === 'radio') {
@@ -175,7 +200,9 @@ function sync(element) {
175
200
  internals.setFormValue(field.value);
176
201
  }
177
202
 
178
- if (field.validity.valid) {
203
+ // A readonly or disabled field keeps its flags (a pattern the value breaks)
204
+ // but the browser doesn't validate it and gives no message
205
+ if (field.validity.valid || !field.willValidate) {
179
206
  internals.setValidity({});
180
207
  } else {
181
208
  const flags = Object.fromEntries(validityFlags.map(flag => [flag, field.validity[flag]]));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nbhi",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Web components from plain HTML templates, without build tools. Bindings in a template become properties, rows() renders lists, and a component with a form field is a real form control.",
5
5
  "keywords": [
6
6
  "web-components",