|
8 | 8 | */
|
9 | 9 |
|
10 | 10 | import { Controller } from '@hotwired/stimulus';
|
11 |
| -import { type App, createApp } from 'vue'; |
| 11 | +import { |
| 12 | + type App, |
| 13 | + type Component, |
| 14 | + createApp, |
| 15 | + defineComponent, |
| 16 | + h, |
| 17 | + type ShallowReactive, |
| 18 | + shallowReactive, |
| 19 | + toRaw, |
| 20 | + toRefs, |
| 21 | + unref, |
| 22 | + watch, |
| 23 | +} from 'vue'; |
12 | 24 |
|
13 | 25 | export default class extends Controller<Element & { __vue_app__?: App<Element> }> {
|
14 |
| - private props: Record<string, unknown> | null; |
| 26 | + private props: ShallowReactive<Record<string, unknown>>; |
15 | 27 | private app: App<Element>;
|
16 | 28 | declare readonly componentValue: string;
|
17 |
| - declare readonly propsValue: Record<string, unknown> | null | undefined; |
| 29 | + declare readonly hasPropsValue: boolean; |
| 30 | + declare propsValue: Record<string, unknown> | null | undefined; |
18 | 31 |
|
19 | 32 | static values = {
|
20 | 33 | component: String,
|
21 | 34 | props: Object,
|
22 | 35 | };
|
23 | 36 |
|
24 |
| - connect() { |
25 |
| - this.props = this.propsValue ?? null; |
| 37 | + propsValueChanged(newProps: typeof this.propsValue, oldProps: typeof this.propsValue) { |
| 38 | + if (oldProps) { |
| 39 | + let removedPropNames = Object.keys(oldProps); |
| 40 | + |
| 41 | + if (newProps) { |
| 42 | + removedPropNames = removedPropNames.filter( |
| 43 | + (propName) => !Object.prototype.hasOwnProperty.call(newProps, propName) |
| 44 | + ); |
| 45 | + } |
26 | 46 |
|
| 47 | + removedPropNames.forEach((propName) => { |
| 48 | + delete this.props[propName]; |
| 49 | + }); |
| 50 | + } |
| 51 | + if (newProps) { |
| 52 | + Object.entries(newProps).forEach(([propName, propValue]) => { |
| 53 | + this.props[propName] = propValue; |
| 54 | + }); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + initialize() { |
| 59 | + const props = this.hasPropsValue && this.propsValue ? this.propsValue : {}; |
| 60 | + this.props = shallowReactive({ ...props }); |
| 61 | + watch( |
| 62 | + this.props, |
| 63 | + (props) => { |
| 64 | + this.propsValue = toRaw(props); |
| 65 | + }, |
| 66 | + { flush: 'post' } |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + connect() { |
27 | 71 | this.dispatchEvent('connect', { componentName: this.componentValue, props: this.props });
|
28 | 72 |
|
29 | 73 | const component = window.resolveVueComponent(this.componentValue);
|
| 74 | + const wrappedComponent = this.wrapComponent(component); |
30 | 75 |
|
31 |
| - this.app = createApp(component, this.props); |
| 76 | + this.app = createApp(wrappedComponent); |
32 | 77 |
|
33 | 78 | if (this.element.__vue_app__ !== undefined) {
|
34 | 79 | this.element.__vue_app__.unmount();
|
@@ -62,4 +107,27 @@ export default class extends Controller<Element & { __vue_app__?: App<Element> }
|
62 | 107 | private dispatchEvent(name: string, payload: any) {
|
63 | 108 | this.dispatch(name, { detail: payload, prefix: 'vue' });
|
64 | 109 | }
|
| 110 | + |
| 111 | + private wrapComponent(component: Component): Component { |
| 112 | + return defineComponent({ |
| 113 | + setup: () => { |
| 114 | + const propsRefs = toRefs(this.props); |
| 115 | + |
| 116 | + return () => |
| 117 | + h(component, { |
| 118 | + ...Object.fromEntries( |
| 119 | + Object.entries(propsRefs).map(([propName, propRef]) => [propName, unref(propRef)]) |
| 120 | + ), |
| 121 | + ...Object.fromEntries( |
| 122 | + Object.keys(propsRefs).map((propName) => [ |
| 123 | + `onUpdate:${propName}`, |
| 124 | + (value: unknown) => { |
| 125 | + propsRefs[propName].value = value; |
| 126 | + }, |
| 127 | + ]) |
| 128 | + ), |
| 129 | + }); |
| 130 | + }, |
| 131 | + }); |
| 132 | + } |
65 | 133 | }
|
0 commit comments