ripple 0.2.112 → 0.2.113

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
@@ -3,7 +3,7 @@
3
3
  "description": "Ripple is an elegant TypeScript UI framework",
4
4
  "license": "MIT",
5
5
  "author": "Dominic Gannaway",
6
- "version": "0.2.112",
6
+ "version": "0.2.113",
7
7
  "type": "module",
8
8
  "module": "src/runtime/index-client.js",
9
9
  "main": "src/runtime/index-client.js",
@@ -38,6 +38,11 @@
38
38
  "require": "./src/compiler/index.js",
39
39
  "default": "./src/compiler/index.js"
40
40
  },
41
+ "./bindings": {
42
+ "types": "./src/bindings/index.d.ts",
43
+ "require": "./src/bindings/index.js",
44
+ "default": "./src/bindings/index.js"
45
+ },
41
46
  "./validator": {
42
47
  "types": "./types/index.d.ts",
43
48
  "require": "./validator/index.js",
@@ -0,0 +1,13 @@
1
+ import type { Tracked } from "ripple";
2
+
3
+ /**
4
+ * @param {Tracked<V>} tracked
5
+ * @returns {(node: HTMLInputElement) => void}
6
+ */
7
+ export declare function value<V>(tracked: Tracked<V>): (node: HTMLInputElement) => void;
8
+
9
+ /**
10
+ * @param {Tracked<V>} tracked
11
+ * @returns {(node: HTMLInputElement) => void}
12
+ */
13
+ export declare function checked<V>(tracked: Tracked<V>): (node: HTMLInputElement) => void;
@@ -0,0 +1,79 @@
1
+ /** @import {Block, Tracked} from '#client' */
2
+
3
+ import { active_block, get, set, tick } from '../runtime/internal/client';
4
+ import { on } from '../runtime/internal/client/events';
5
+ import { is_tracked_object } from '../runtime/internal/client/utils';
6
+
7
+ /**
8
+ * @param {string} value
9
+ */
10
+ function to_number(value) {
11
+ return value === '' ? null : +value;
12
+ }
13
+
14
+ /**
15
+ * @param {HTMLInputElement} input
16
+ */
17
+ function is_numberlike_input(input) {
18
+ var type = input.type;
19
+ return type === 'number' || type === 'range';
20
+ }
21
+
22
+ /**
23
+ * @param {unknown} maybe_tracked
24
+ * @returns {(node: HTMLInputElement) => void}
25
+ */
26
+ export function value(maybe_tracked) {
27
+ if (!is_tracked_object(maybe_tracked)) {
28
+ throw new TypeError('value() argument is not a tracked object');
29
+ }
30
+
31
+ const block = /** @type {Block} */ (active_block);
32
+ const tracked = /** @type {Tracked} */ (maybe_tracked);
33
+
34
+ return (input) => {
35
+ const clear_event = on(input, 'input', async () => {
36
+ /** @type {any} */
37
+ var value = input.value;
38
+ value = is_numberlike_input(input) ? to_number(value) : value;
39
+ set(tracked, value, block);
40
+
41
+ await tick();
42
+
43
+ if (value !== (value = get(tracked))) {
44
+ var start = input.selectionStart;
45
+ var end = input.selectionEnd;
46
+ input.value = value ?? '';
47
+
48
+ // Restore selection
49
+ if (end !== null) {
50
+ input.selectionStart = start;
51
+ input.selectionEnd = Math.min(end, input.value.length);
52
+ }
53
+ }
54
+ });
55
+
56
+ return clear_event;
57
+ };
58
+ }
59
+
60
+ /**
61
+ * @param {unknown} maybe_tracked
62
+ * @returns {(node: HTMLInputElement) => void}
63
+ */
64
+ export function checked(maybe_tracked) {
65
+ if (!is_tracked_object(maybe_tracked)) {
66
+ throw new TypeError('checked() argument is not a tracked object');
67
+ }
68
+
69
+ const block = /** @type {any} */ (active_block);
70
+ const tracked = /** @type {Tracked<any>} */ (maybe_tracked);
71
+
72
+ return (input) => {
73
+ const clear_event = on(input, 'change', () => {
74
+ set(tracked, input.checked, block);
75
+ });
76
+
77
+ return clear_event;
78
+ };
79
+ }
@@ -17,12 +17,12 @@ export type Dependency = {
17
17
  n: null | Dependency;
18
18
  };
19
19
 
20
- export type Tracked = {
20
+ export type Tracked<V = any> = {
21
21
  a: { get?: Function, set?: Function };
22
22
  b: Block;
23
23
  c: number;
24
24
  f: number;
25
- v: any;
25
+ v: V;
26
26
  };
27
27
 
28
28
  export type Derived = {
@@ -0,0 +1,63 @@
1
+ import { describe, it, expect, beforeEach, afterEach } from 'vitest';
2
+ import { mount, flushSync, track, effect } from 'ripple';
3
+ import { value, checked } from 'ripple/bindings';
4
+
5
+ describe('use value()', () => {
6
+ let container;
7
+
8
+ function render(component) {
9
+ mount(component, {
10
+ target: container,
11
+ });
12
+ }
13
+
14
+ beforeEach(() => {
15
+ container = document.createElement('div');
16
+ document.body.appendChild(container);
17
+ });
18
+
19
+ afterEach(() => {
20
+ document.body.removeChild(container);
21
+ container = null;
22
+ });
23
+
24
+ it('should update value on input', () => {
25
+ component App() {
26
+ const text = track('');
27
+
28
+ effect(() => {
29
+ console.log('text changed', @text);
30
+ });
31
+
32
+ <input type="text" {ref value(text)} />
33
+ }
34
+ render(App);
35
+ flushSync();
36
+
37
+ const input = container.querySelector('input');
38
+ input.value = 'Hello';
39
+ input.dispatchEvent(new Event('input'));
40
+ flushSync();
41
+ expect(input.value).toBe('Hello');
42
+ });
43
+
44
+ it('should update checked on input', () => {
45
+ component App() {
46
+ const value = track(false);
47
+
48
+ effect(() => {
49
+ console.log('checked changed', @value);
50
+ });
51
+
52
+ <input type="checkbox" {ref checked(value)} />
53
+ }
54
+ render(App);
55
+ flushSync();
56
+
57
+ const input = container.querySelector('input');
58
+ input.checked = true;
59
+ input.dispatchEvent(new Event('input'));
60
+ flushSync();
61
+ expect(input.checked).toBe(true);
62
+ });
63
+ });