nbhi 1.0.1 → 1.0.3
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 +4 -2
- package/javascript/component.js +33 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,7 +19,7 @@ No build step is needed. Map the package in an import map and import it in a mod
|
|
|
19
19
|
|
|
20
20
|
```html
|
|
21
21
|
<script type="importmap">
|
|
22
|
-
{ "imports": { "nbhi": "https://cdn.jsdelivr.net/npm/nbhi@1
|
|
22
|
+
{ "imports": { "nbhi": "https://cdn.jsdelivr.net/npm/nbhi@1/javascript/index.js" } }
|
|
23
23
|
</script>
|
|
24
24
|
|
|
25
25
|
<script type="module">
|
|
@@ -28,6 +28,8 @@ No build step is needed. Map the package in an import map and import it in a mod
|
|
|
28
28
|
</script>
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
+
`nbhi@1` loads the latest 1.x release. To stay on one version, pin it, for example `nbhi@1.0.2`.
|
|
32
|
+
|
|
31
33
|
Or install it with `npm install nbhi`.
|
|
32
34
|
|
|
33
35
|
## Defining components
|
|
@@ -89,7 +91,7 @@ The script's default export runs with the element. Its imports go through the pa
|
|
|
89
91
|
<script type="importmap">
|
|
90
92
|
{
|
|
91
93
|
"imports": {
|
|
92
|
-
"nbhi": "https://cdn.jsdelivr.net/npm/nbhi@1
|
|
94
|
+
"nbhi": "https://cdn.jsdelivr.net/npm/nbhi@1/javascript/index.js",
|
|
93
95
|
"date-fns": "https://esm.sh/date-fns@4.1.0",
|
|
94
96
|
"articles": "/javascript/articles.js"
|
|
95
97
|
}
|
package/javascript/component.js
CHANGED
|
@@ -79,18 +79,34 @@ function define({ name, template, script, styleSheets, observer }) {
|
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
attributeChangedCallback(attribute, oldValue, newValue) {
|
|
82
|
-
if (oldValue
|
|
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
|
-
|
|
92
|
-
|
|
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
|
-
|
|
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.
|
|
3
|
+
"version": "1.0.3",
|
|
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",
|