Skip to content

Commit

Permalink
fix(plugin-jsx): v-model argument and modifier co-usage (#668)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiyuanzmj authored Jan 12, 2024
1 parent 55bab7a commit d352b58
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/babel-plugin-jsx/README-zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,14 @@ const App = {
```jsx
<input v-model={[val, ['modifier']]} />
// 或者
<input v-model_modifier={val} />
```
```jsx
<A v-model={[val, 'argument', ['modifier']]} />
// 或者
<input v-model:argument_modifier={val} />
```
会编译成:
Expand Down
4 changes: 4 additions & 0 deletions packages/babel-plugin-jsx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,14 @@ const App = {
```jsx
<input v-model={[val, ['modifier']]} />
// Or
<input v-model_modifier={val} />
```
```jsx
<A v-model={[val, 'argument', ['modifier']]} />
// Or
<input v-model:argument_modifier={val} />
```
Will compile to:
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-plugin-jsx/src/parseDirectives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const parseDirectives = (params: {
.replace(/^\S/, (s: string) => s.toLowerCase());

if (directiveArgument) {
args.push(t.stringLiteral(directiveArgument));
args.push(t.stringLiteral(directiveArgument.split('_')[0]));
}

const isVModels = directiveName === 'models';
Expand Down
45 changes: 45 additions & 0 deletions packages/babel-plugin-jsx/test/v-model.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,48 @@ test('Named model', async () => {
await wrapper.trigger('click');
expect(wrapper.html()).toBe('<div>2</div>');
});

test('named model and underscore modifier should work in custom component', async () => {
const Child = defineComponent({
emits: ['update:value'],
props: {
value: {
type: Number,
default: 0,
},
valueModifiers: {
default: () => ({ double: false }),
},
},
setup(props, { emit }) {
const handleClick = () => {
emit('update:value', 3);
};
return () => (
<div onClick={handleClick}>
{props.valueModifiers.double ? props.value * 2 : props.value}
</div>
);
},
});

const wrapper = mount(
defineComponent({
data() {
return {
foo: 1,
};
},
render() {
return <Child v-model:value_double={this.foo} />;
},
})
);

expect(wrapper.html()).toBe('<div>2</div>');
wrapper.vm.$data.foo += 1;
await wrapper.vm.$nextTick();
expect(wrapper.html()).toBe('<div>4</div>');
await wrapper.trigger('click');
expect(wrapper.html()).toBe('<div>6</div>');
});

0 comments on commit d352b58

Please sign in to comment.