native-document 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "native-document",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "main": "index.js",
5
5
  "type": "module",
6
6
  "scripts": {
package/readme.md CHANGED
@@ -32,6 +32,16 @@ curl -o native-document.min.js https://raw.githubusercontent.com/afrocodeur/nati
32
32
  <script src="https://cdn.jsdelivr.net/gh/afrocodeur/native-document@latest/dist/native-document.min.js"></script>
33
33
  ```
34
34
 
35
+ ```bash
36
+ #Use degit
37
+
38
+ npx degit afrocodeur/native-document-vite my-project
39
+ cd my-project
40
+ npm install
41
+ npm start
42
+ ```
43
+
44
+
35
45
 
36
46
  ### Basic Example
37
47
 
@@ -2,7 +2,7 @@ import HtmlElementWrapper from "../wrappers/HtmlElementWrapper";
2
2
 
3
3
  export const Div = HtmlElementWrapper('div');
4
4
  export const Span = HtmlElementWrapper('span');
5
- export const Label = HtmlElementWrapper('span');
5
+ export const Label = HtmlElementWrapper('label');
6
6
  export const P = HtmlElementWrapper('p');
7
7
  export const Paragraph = P;
8
8
  export const Strong = HtmlElementWrapper('strong');
@@ -11,7 +11,7 @@ import DebugManager from "../../utils/debug-manager.js";
11
11
  * @param {string|null} comment
12
12
  * @returns {DocumentFragment}
13
13
  */
14
- export const ShowIf = function(condition, child, comment) {
14
+ export const ShowIf = function(condition, child, comment = null) {
15
15
  if(!(Validator.isObservable(condition))) {
16
16
  return DebugManager.warn('ShowIf', "ShowIf : condition must be an Observable / "+comment, condition);
17
17
  }
@@ -1,6 +1,6 @@
1
1
  import Validator from "../utils/validator";
2
2
  import NativeDocumentError from "../errors/NativeDocumentError";
3
-
3
+ import {BOOLEAN_ATTRIBUTES} from "./constants.js";
4
4
 
5
5
  /**
6
6
  *
@@ -52,6 +52,43 @@ function bindStyleAttribute(element, data) {
52
52
  }
53
53
  }
54
54
 
55
+ /**
56
+ *
57
+ * @param {HTMLElement} element
58
+ * @param {string} attributeName
59
+ * @param {boolean|number|Observable} value
60
+ */
61
+ function bindBooleanAttribute(element, attributeName, value) {
62
+ element[attributeName] = Boolean(Validator.isObservable(value) ? value.val() : value);
63
+ if(Validator.isObservable(value)) {
64
+ if(['checked'].includes(attributeName)) {
65
+ element.addEventListener('input', () => value.set(element[attributeName]));
66
+ }
67
+ value.subscribe(newValue => {
68
+ element[attributeName] = Boolean(newValue);
69
+ });
70
+ }
71
+ }
72
+
73
+
74
+ /**
75
+ *
76
+ * @param {HTMLElement} element
77
+ * @param {string} attributeName
78
+ * @param {Observable} value
79
+ */
80
+ function bindAttributeWithObservable(element, attributeName, value) {
81
+ value.subscribe(newValue => element.setAttribute(attributeName, newValue));
82
+ element.setAttribute(attributeName, value.val());
83
+ if(attributeName === 'value') {
84
+ if(['checkbox', 'radio'].includes(element.type)) {
85
+ element.addEventListener('input', () => value.set(element.checked));
86
+ } else {
87
+ element.addEventListener('input', () => value.set(element.value));
88
+ }
89
+ }
90
+ }
91
+
55
92
  /**
56
93
  *
57
94
  * @param {HTMLElement} element
@@ -65,18 +102,15 @@ export default function AttributesWrapper(element, attributes) {
65
102
  throw new NativeDocumentError('Attributes must be an object');
66
103
  }
67
104
 
68
- for(let attributeName in attributes) {
105
+ for(let key in attributes) {
106
+ const attributeName = key.toLowerCase();
69
107
  const value = attributes[attributeName];
108
+ if(BOOLEAN_ATTRIBUTES.includes(attributeName)) {
109
+ bindBooleanAttribute(element, attributeName, value);
110
+ continue;
111
+ }
70
112
  if(Validator.isObservable(value)) {
71
- value.subscribe(newValue => element.setAttribute(attributeName, newValue));
72
- element.setAttribute(attributeName, value.val());
73
- if(attributeName === 'value') {
74
- if(['checkbox', 'radio'].includes(element.type)) {
75
- element.addEventListener('input', () => value.set(element.checked));
76
- } else {
77
- element.addEventListener('input', () => value.set(element.value));
78
- }
79
- }
113
+ bindAttributeWithObservable(element, attributeName, value);
80
114
  continue;
81
115
  }
82
116
  if(attributeName === 'class' && Validator.isJson(value)) {
@@ -0,0 +1,2 @@
1
+
2
+ export const BOOLEAN_ATTRIBUTES = ['checked', 'selected', 'disabled', 'readonly', 'required', 'autofocus', 'multiple', 'autocomplete', 'hidden', 'contenteditable', 'spellcheck', 'translate', 'draggable', 'async', 'defer', 'autoplay', 'controls', 'loop', 'muted', 'download', 'reversed', 'open', 'default', 'formnovalidate', 'novalidate', 'scoped', 'itemscope', 'allowfullscreen', 'allowpaymentrequest', 'playsinline'];