Skip to content

Commit e221148

Browse files
committed
fix: ensure modified properties are always reactive
before unwrapping the props for mapping `styles` to `style` had a side effect which caused that the reactivity git lost. In fact watchers never fired (as reported in #349). closes #349
1 parent 41648e3 commit e221148

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/composables/__tests__/usePropsAsObjectProperties.spec.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe("usePropsAsObjectProperties", () => {
4242

4343
it("should be possible to watch resulting properties and register changes on the input props", async () => {
4444
const WrapperComponent = defineComponent({
45-
props: ["foo", "style"],
45+
props: ["foo"],
4646
setup(props) {
4747
const watchFired = ref(false);
4848
const properties = usePropsAsObjectProperties(props);
@@ -63,4 +63,28 @@ describe("usePropsAsObjectProperties", () => {
6363

6464
expect(wrapper.vm.watchFired).toEqual(true);
6565
});
66+
67+
it("should watch modified styles property and register changes on the input props", async () => {
68+
const WrapperComponent = defineComponent({
69+
props: ["styles"],
70+
setup(props) {
71+
const watchFired = ref(false);
72+
const properties = usePropsAsObjectProperties(props);
73+
74+
watch(properties, () => {
75+
watchFired.value = true;
76+
});
77+
return {
78+
watchFired,
79+
};
80+
},
81+
});
82+
83+
const wrapper = shallowMount(WrapperComponent, {
84+
propsData: { styles: "initial" },
85+
});
86+
await wrapper.setProps({ styles: "updated" });
87+
88+
expect(wrapper.vm.watchFired).toEqual(true);
89+
});
6690
});

src/composables/usePropsAsObjectProperties.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { inject, reactive, type UnwrapNestedRefs } from "vue";
1+
import { inject, reactive, type UnwrapNestedRefs, toRefs } from "vue";
22

33
type OlClassOptions<T> = T extends { styles: infer S }
44
? { style: S } & Omit<T, "styles">
@@ -12,7 +12,7 @@ function checkAndUpdateStylePropDef<T extends Record<string, unknown>>(
1212
options: T,
1313
) {
1414
if ("styles" in options) {
15-
const { styles, ...rest } = options;
15+
const { styles, ...rest } = toRefs(options);
1616
return { style: styles, ...rest } as OlClassOptions<T>;
1717
} else {
1818
return options as OlClassOptions<T>;

0 commit comments

Comments
 (0)