landaxs 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.
package/README.md CHANGED
@@ -0,0 +1,367 @@
1
+ Landaxs is library for dynamically managing input forms, supporting two-way binding and event handling with clean syntax.
2
+
3
+ <img src="logo.png" width="200" />
4
+ ## ✨ Features
5
+ - Two-way data binding
6
+ - Event handling
7
+ - TypeScript support
8
+ - Lightweight & dependency-free
9
+
10
+ CDN
11
+
12
+ ```html
13
+ <script src="https://cdn.jsdelivr.net/npm/landaxs@1.0.1/dist/index.min.js"></script>
14
+ ```
15
+
16
+ ### Define input name
17
+ define the input tag input name into the input property
18
+
19
+ >example
20
+ ```html
21
+ <!-- in html -->
22
+ <input type="text" x_input="username">
23
+ <input type="email" x_input="email">
24
+
25
+ <input type="radio" x_input="gender" value="male"/>
26
+ <input type="radio" x_input="gender" value="female"/>
27
+
28
+ <input type="checkbox" x_input="role" value="admin">
29
+ <input type="checkbox" x_input="role" value="sales">
30
+ <input type="checkbox" x_input="role" value="employee">
31
+
32
+
33
+ <!-- in js -->
34
+ <script>
35
+ // to define input name
36
+     let form_login = new Landaxs()
37
+     .defineInput({
38
+         username : "",
39
+         email: "",
40
+         gender : "male",
41
+         role : []
42
+     })
43
+ // to access input
44
+ form_login.input.username
45
+ form_login.input.email
46
+ form_login.input.gender
47
+ form_login.input.role
48
+ </script>
49
+ ```
50
+
51
+ ### Triggering Input
52
+ to trigger input from html
53
+ **Parameters:**
54
+
55
+ - `name_input` - Nama input (string) atau array nama input
56
+ - `callback` - Function yang dijalankan saat input berubah (menerima `data_input` sebagai parameter)
57
+
58
+ >example
59
+ ```html
60
+ <script>
61
+ //to define input name
62
+     let form_login = new Landaxs()
63
+     .defineInput({
64
+         username : "",
65
+         email: "",
66
+     }).triggerInput("username",(data)=>{
67
+     console.log(data)
68
+     })
69
+
70
+ // for single trigger input
71
+ .triggerInput("username",(data)=>{
72
+     console.log(data) // print input property
73
+ })
74
+ // for mutiple trigger input
75
+ .triggerInput(["username","email"],(data)=>{
76
+     console.log(data) //print input property
77
+ })
78
+ </script>
79
+ ```
80
+
81
+ ### Reference DOM elements
82
+
83
+ >this is the same as document.getElementById("id_")
84
+
85
+ >example
86
+ ```html
87
+ <!-- in html -->
88
+ <div x_ref="msg"></div>
89
+ <script>
90
+ // to define input name
91
+     let form_login = new Landaxs()
92
+     .defineInput({
93
+         username : "",
94
+         email: "",
95
+     }).triggerInput("username",(data)=>{
96
+     console.log(data)
97
+     }).setRef(["msg"])
98
+    
99
+ // for single ref
100
+     .setRef("msg")
101
+ // for mutiple ref
102
+     .setRef(["msg","msg2"])
103
+    
104
+ // to access ref
105
+ form_login.ref.msg.textContent = "hello world"
106
+ </script>
107
+ ```
108
+ ### Method
109
+ register a method to the methods property
110
+ >
111
+ ```html
112
+ <!-- in html -->
113
+ <button type="button" onclick="form_login.method.my_method">Save</button>
114
+
115
+ <script>
116
+ //to define input name
117
+     let form_login = new Landaxs()
118
+    
119
+     .defineInput({
120
+         username : "",
121
+         email: "",
122
+     })
123
+    
124
+     .triggerInput("username",(data)=>{
125
+     console.log(data)
126
+     })
127
+    
128
+     .setRef(["msg"])
129
+    
130
+     .methods({
131
+     my_method(){
132
+     form_login.ref.msg.textContent = "hello world"
133
+     },
134
+     my_method2(){
135
+ form_login.ref.msg.textContent = "hello 22"
136
+     }
137
+     })
138
+    
139
+     // access method
140
+     form_input.method.my_methods()
141
+    
142
+     //format object method
143
+    
144
+     .method({
145
+     method_name : function(){
146
+ // code
147
+     }
148
+     })
149
+    
150
+     .method({
151
+     method_name : ()=>{
152
+ // code
153
+     }
154
+     })
155
+    
156
+ .method({
157
+     method_name(){
158
+ // code
159
+     }
160
+     })
161
+    
162
+ </script>
163
+ ```
164
+ ### properties and methods of the Landaxs class
165
+ ### 📦 Properties (Internal State)
166
+
167
+ | Property | Type | Function |
168
+ | --------------- | -------------------------------------- | --------------------------------------------------- |
169
+ | `_data` | `Record<string, any>` | Stores the main internal reactive state |
170
+ | `input` | `Record<string, any>` | Proxy wrapper of `_data` to intercept value changes |
171
+ | `input_details` | `Array<Record<string, any>>` | Stores metadata of all registered inputs |
172
+ | `ref` | `Record<string, any>` | Stores DOM references based on `x_ref` attribute |
173
+ | method | Record<string, (...args:any[]) => any> | Stores externally registered functions |
174
+ | | | |
175
+ ### ⚙ Methods
176
+
177
+ | Method | Parameters | Function |
178
+ | -------------- | -------------------------------- | ------------------------------------------------------------ |
179
+ | `defineInput` | `data: Record<string, any>` | Initializes data binding between state and DOM via `x_input` |
180
+ | `setRef` | `string \| string[]` | Registers DOM references using `x_ref` |
181
+ | `triggerInput` | `string \| string[]`, `callback` | Executes callback when input value changes |
182
+ | methods | Record<string, Function> | Registers external custom methods into the instance |
183
+ >example use
184
+ ```html
185
+ <!DOCTYPE html>
186
+
187
+ <html lang="en">
188
+ <head>
189
+     <meta charset="UTF-8">
190
+     <meta name="viewport" content="width=device-width, initial-scale=1.0">
191
+     <title>Document</title>
192
+     <script src="https://cdn.jsdelivr.net/npm/landaxs@1.0.1/dist/index.min.js"></script>
193
+ <style>
194
+     .form-control {
195
+         display: flex;
196
+         flex-direction: column;
197
+         width: min-content;
198
+     }
199
+ </style>
200
+ </head>
201
+ <body>
202
+ <div x_ref="msg"></div>
203
+ <form>
204
+ <div class="form-control">
205
+     <label>Username</label>
206
+     <input x_ref="input_username" type="text" x_input="username">
207
+ </div>
208
+ <div class="form-control">
209
+     <label>Email</label>
210
+     <input type="email" x_input="email">
211
+ </div>
212
+ <div>
213
+     <label>Gender</label>
214
+     <label>
215
+         <input type="radio" x_input="gender" value="male"/>
216
+         Male
217
+     </label>
218
+     <label>
219
+         <input type="radio" x_input="gender" value="female"/>
220
+         Female
221
+     </label>
222
+ </div>
223
+ <div>
224
+   <label>Role</label>
225
+   <label>
226
+     <input type="checkbox" x_input="role" value="admin">
227
+     Admin
228
+   </label>
229
+   <label>
230
+     <input type="checkbox" x_input="role" value="sales">
231
+     Sales
232
+   </label>
233
+   <label>
234
+     <input type="checkbox" x_input="role" value="employee">
235
+     Employee
236
+   </label>
237
+ </div>
238
+ <button type="button" onclick="form_login.method.send(this)">Save</button>
239
+ <button type="button" onclick="form_login.method.clear()">clear</button>
240
+ </form>
241
+ <div x_ref="data"></div>
242
+ <div x_ref="send_msg"></div>
243
+ <script>
244
+
245
+     let form_login = new Landaxs()
246
+     .defineInput({
247
+         username : "",
248
+         email: "",
249
+         gender : "male",
250
+         role : ""
251
+     })
252
+    
253
+     .triggerInput(["username","email","gender","role"],(data)=>{
254
+         form_login.ref.data.textContent = JSON.stringify(data)
255
+         form_login.method.validate()
256
+     })
257
+
258
+     .setRef(["data","msg","send_msg","input_username"])
259
+
260
+     .methods({
261
+    
262
+         send : (e)=>{
263
+             if(form_login.method.validate()){
264
+                 form_login.ref.send_msg.innerHTML = `<h4>Result</h4> ${JSON.stringify(form_login.input)}`
265
+                 form_login.method.clear()
266
+             }
267
+         },
268
+        
269
+         validate : ()=>{
270
+             if(form_login.input.username.length < 4){
271
+                 form_login.ref.msg.textContent = "Length Username Must > 3"
272
+                 form_login.ref.msg.style.color = "red"
273
+             }else if(!form_login.input.email.includes("@")){
274
+                 form_login.ref.msg.textContent = "Input Must an Email"
275
+                 form_login.ref.msg.style.color = "red"
276
+             }else{
277
+                 form_login.ref.msg.textContent = "nice"
278
+                 form_login.ref.msg.style.color = "green"
279
+                 return true
280
+             }
281
+         },
282
+
283
+         clear : ()=>{
284
+             form_login.input.username = ""
285
+             form_login.input.email = ""
286
+             form_login.input.gender = ""
287
+             form_login.input.role = []
288
+             form_login.ref.input_username.focus()
289
+         }
290
+
291
+     })
292
+
293
+     form_login.ref.data.textContent = JSON.stringify(form_login.input)
294
+
295
+
296
+
297
+ </script>
298
+ </body>
299
+ </html>
300
+ ```
301
+ ---
302
+ ### 💡 Tips & Tricks
303
+ #### Tip 1: Two-Way Binding
304
+
305
+ ```typescript
306
+ // Update data → DOM automatically updates
307
+ form.input.nama = "Baru"
308
+ // Update DOM (user types) → data automatically updates
309
+ // <input x_input="nama" /> ← When the user changes a value, form.input.nama updates.
310
+ ```
311
+ #### Tip 2: Akses Raw Data
312
+
313
+ ```typescript
314
+ // Via Proxy (two-way)
315
+ console.log(form.input)
316
+ // Via Private Data
317
+ console.log(form._data)
318
+ ```
319
+ #### Tip 3: Get Form Metadata
320
+
321
+ ```typescript
322
+
323
+ form.input_details
324
+
325
+ // Output:
326
+ // [
327
+ //   { name_input: "nama", type_input: "text", type_variable: "string" },
328
+ //   { name_input: "email", type_input: "email", type_variable: "string" },
329
+ //   { name_input: "gender", type_input: "radio", type_variable: "string" },
330
+ //   { name_input: "hobi", type_input: "checkbox", type_variable: "object" }
331
+ // ]
332
+
333
+ ```
334
+ #### Tip 4: Convert Checkbox Array to String
335
+
336
+ ```typescript
337
+
338
+ form.methods({
339
+     getHobiString: () => {
340
+         return form.input.hobi.join(", ")
341
+         // "membaca, gaming, olahraga"
342
+     }
343
+ })
344
+
345
+ ```
346
+ #### Tip 5: Method Chaining
347
+
348
+ ```typescript
349
+
350
+ form
351
+     .defineInput({/* ... */})
352
+     .setRef([/* ... */])
353
+     .triggerInput([/* ... */], (data) => {/* ... */})
354
+     .methods({/* ... */})
355
+ // Can chain multiple methods!
356
+ ```
357
+
358
+
359
+ ---
360
+ ### 🚀 Browser Support
361
+
362
+ ✅ Chrome 60+  
363
+ ✅ Firefox 55+  
364
+ ✅ Safari 11+  
365
+ ✅ Edge 79+  
366
+
367
+ ---
package/dist/index.d.ts CHANGED
@@ -5,7 +5,7 @@ declare class Landaxs {
5
5
  ref: Record<string, any>;
6
6
  method: Record<string, (...args: any[]) => any>;
7
7
  defineInput(data: Record<string, any>): this;
8
- setRef(data: Array<string>): this;
8
+ setRef(data: string | Array<string>): this;
9
9
  triggerInput(name_input: string | Array<string>, callback: Function): this;
10
10
  methods(function_parameter: Record<string, (...args: any[]) => any>): this;
11
11
  }
package/dist/index.js CHANGED
@@ -13,8 +13,30 @@ class Landaxs {
13
13
  set: (target, key, value) => {
14
14
  target[key] = value;
15
15
  document.querySelectorAll(`[x_input='${key}']`).forEach(ctx => {
16
- ctx.value = value;
17
- this._data[key] = value;
16
+ if (ctx.type == "checkbox") {
17
+ if (value.length === 0) {
18
+ ctx.checked = false;
19
+ }
20
+ else {
21
+ ctx.checked = false;
22
+ value.forEach((x) => {
23
+ if (ctx.value === x) {
24
+ ctx.checked = true;
25
+ }
26
+ });
27
+ }
28
+ }
29
+ else if (ctx.type === "radio") {
30
+ if (ctx.value === this._data[key]) {
31
+ ctx.checked = true;
32
+ }
33
+ else {
34
+ ctx.checked = false;
35
+ }
36
+ }
37
+ else {
38
+ ctx.value = value;
39
+ }
18
40
  });
19
41
  return true;
20
42
  }
@@ -40,6 +62,9 @@ class Landaxs {
40
62
  }
41
63
  else if (ctx.type === "checkbox") {
42
64
  ctx.setAttribute("name", `${key}[]`);
65
+ if (typeof this._data[key] === "string") {
66
+ this._data[key] = [];
67
+ }
43
68
  this._data[key].forEach((ctx2) => {
44
69
  if (ctx.value === ctx2) {
45
70
  ctx.checked = true;
@@ -80,12 +105,13 @@ class Landaxs {
80
105
  type_input: "select",
81
106
  type_variable: typeof this._data[key],
82
107
  });
83
- let cx = [];
108
+ if (typeof this._data[key] === "string") {
109
+ this._data[key] = [];
110
+ }
84
111
  this._data[key].forEach((ctx2) => {
85
112
  for (let i = 0; i < ctx.options.length; i++) {
86
113
  if (ctx.options[i].value === ctx2) {
87
114
  ctx.options[i].selected = true;
88
- cx.push(ctx2);
89
115
  }
90
116
  }
91
117
  });
@@ -108,9 +134,14 @@ class Landaxs {
108
134
  return this;
109
135
  }
110
136
  setRef(data) {
111
- data.forEach(ctx => {
112
- this.ref[ctx] = document.querySelectorAll(`[x_ref='${ctx}']`)[0];
113
- });
137
+ if (typeof data === "string") {
138
+ this.ref[data] = document.querySelectorAll(`[x_ref='${data}']`)[0];
139
+ }
140
+ else {
141
+ data.forEach(ctx => {
142
+ this.ref[ctx] = document.querySelectorAll(`[x_ref='${ctx}']`)[0];
143
+ });
144
+ }
114
145
  return this;
115
146
  }
116
147
  triggerInput(name_input, callback) {
@@ -147,7 +178,7 @@ class Landaxs {
147
178
  return this;
148
179
  }
149
180
  methods(function_parameter) {
150
- this.method = function_parameter;
181
+ this.method = { ...this.method, ...function_parameter };
151
182
  return this;
152
183
  }
153
184
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "landaxs",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Landaxs is a lightweight library for dynamic input binding with two-way data flow.",
5
5
  "keywords": ["forms", "binding", "javascript", "typescript"],
6
6
  "homepage": "https://github.com/Rakhmadi/Landaxs#readme",
package/dist/get.js DELETED
@@ -1 +0,0 @@
1
- "use strict";