Skip to content

Commit e9b83a0

Browse files
committed
feat: v-model targe value should support variable
1 parent 4c858a2 commit e9b83a0

5 files changed

Lines changed: 44 additions & 46 deletions

File tree

packages/babel-plugin-jsx/src/buildProps.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -242,22 +242,23 @@ const buildProps = (path: NodePath<t.JSXElement>, state: State) => {
242242
values.forEach((value, index) => {
243243
const propName = args[index];
244244
// v-model target with variable
245-
const isIdentifierProp = t.isIdentifier(propName);
245+
// const isIdentifierProp = t.isIdentifier(propName);
246+
const isDynamic = !t.isStringLiteral(propName) && !t.isNullLiteral(propName);
246247

247248
// must be v-model or v-models and is a component
248249
if (!directive) {
249250
properties.push(
250251
t.objectProperty(t.isNullLiteral(propName)
251-
? t.stringLiteral('modelValue') : propName, value as any, isIdentifierProp),
252+
? t.stringLiteral('modelValue') : propName, value as any, isDynamic),
252253
);
253-
if (!isIdentifierProp) {
254+
if (!isDynamic) {
254255
dynamicPropNames.add((propName as t.StringLiteral)?.value || 'modelValue');
255256
}
256257

257258
if (modifiers[index]?.size) {
258259
properties.push(
259260
t.objectProperty(
260-
isIdentifierProp
261+
isDynamic
261262
? t.binaryExpression('+', propName, t.stringLiteral('Modifiers'))
262263
: t.stringLiteral(`${(propName as t.StringLiteral)?.value || 'model'}Modifiers`),
263264
t.objectExpression(
@@ -266,13 +267,13 @@ const buildProps = (path: NodePath<t.JSXElement>, state: State) => {
266267
t.booleanLiteral(true),
267268
)),
268269
),
269-
isIdentifierProp,
270+
isDynamic,
270271
),
271272
);
272273
}
273274
}
274275

275-
const updateName = isIdentifierProp
276+
const updateName = isDynamic
276277
? t.binaryExpression('+', t.stringLiteral('onUpdate'), propName)
277278
: t.stringLiteral(`onUpdate:${(propName as t.StringLiteral)?.value || 'modelValue'}`);
278279

@@ -283,11 +284,11 @@ const buildProps = (path: NodePath<t.JSXElement>, state: State) => {
283284
[t.identifier('$event')],
284285
t.assignmentExpression('=', value as any, t.identifier('$event')),
285286
),
286-
isIdentifierProp,
287+
isDynamic,
287288
),
288289
);
289290

290-
if (!isIdentifierProp) {
291+
if (!isDynamic) {
291292
dynamicPropNames.add((updateName as t.StringLiteral).value);
292293
} else {
293294
hasDynamicKeys = true;

packages/babel-plugin-jsx/src/parseDirectives.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ const parseModifiers = (value: t.ArrayExpression): string[] => (
3434
const parseDirectives = (params: {
3535
name: string,
3636
path: NodePath<t.JSXAttribute>,
37-
value: t.StringLiteral | t.Expression | null,
37+
value: t.Expression | null,
3838
state: State,
3939
tag: Tag,
4040
isComponent: boolean
4141
}) => {
4242
const {
4343
name, path, value, state, tag, isComponent,
4444
} = params;
45-
const args: Array<t.StringLiteral | t.Identifier | t.NullLiteral> = [];
45+
const args: Array<t.Expression | t.NullLiteral> = [];
4646
const vals: t.Expression[] = [];
4747
const modifiersSet: Set<string>[] = [];
4848
const underscoreModifiers = name.split('_');
@@ -77,7 +77,7 @@ const parseDirectives = (params: {
7777
const [first, second, third] = elements;
7878
let modifiers = underscoreModifiers;
7979

80-
if (t.isStringLiteral(second) || t.isIdentifier(second)) {
80+
if (second && !t.isArrayExpression(second) && !t.isSpreadElement(second)) {
8181
args.push(second);
8282
modifiers = parseModifiers(third as t.ArrayExpression);
8383
} else if (t.isArrayExpression(second)) {

packages/babel-plugin-jsx/test/__snapshots__/snapshot.test.ts.snap

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,12 @@ exports[`v-model target value support variable: v-model target value support var
266266
"import { createVNode as _createVNode, resolveComponent as _resolveComponent, Fragment as _Fragment } from \\"vue\\";
267267
const foo = 'foo';
268268
269+
const a = () => 'a';
270+
271+
const b = {
272+
c: 'c'
273+
};
274+
269275
_createVNode(_Fragment, null, [_createVNode(_resolveComponent(\\"A\\"), {
270276
[foo]: xx,
271277
[\\"onUpdate\\" + foo]: $event => xx = $event
@@ -281,28 +287,24 @@ _createVNode(_Fragment, null, [_createVNode(_resolveComponent(\\"A\\"), {
281287
\\"a\\": true
282288
},
283289
[\\"onUpdate\\" + foo]: $event => xx = $event
284-
}, null, 16)]);"
285-
`;
286-
287-
exports[`v-models target value support variable: v-models target value support variable 1`] = `
288-
"import { createVNode as _createVNode, resolveComponent as _resolveComponent, Fragment as _Fragment } from \\"vue\\";
289-
const foo = 'foo';
290-
291-
_createVNode(_Fragment, null, [_createVNode(_resolveComponent(\\"A\\"), {
292-
[foo]: xx,
293-
[\\"onUpdate\\" + foo]: $event => xx = $event
294-
}, null, 16), _createVNode(_resolveComponent(\\"B\\"), {
295-
\\"modelValue\\": xx,
296-
\\"modelModifiers\\": {
290+
}, null, 16), _createVNode(_resolveComponent(\\"D\\"), {
291+
[foo === 'foo' ? 'a' : 'b']: xx,
292+
[(foo === 'foo' ? 'a' : 'b') + \\"Modifiers\\"]: {
297293
\\"a\\": true
298294
},
299-
\\"onUpdate:modelValue\\": $event => xx = $event
300-
}, null, 8, [\\"modelValue\\", \\"onUpdate:modelValue\\"]), _createVNode(_resolveComponent(\\"C\\"), {
301-
[foo]: xx,
302-
[foo + \\"Modifiers\\"]: {
295+
[\\"onUpdate\\" + (foo === 'foo' ? 'a' : 'b')]: $event => xx = $event
296+
}, null, 16), _createVNode(_resolveComponent(\\"E\\"), {
297+
[a()]: xx,
298+
[a() + \\"Modifiers\\"]: {
303299
\\"a\\": true
304300
},
305-
[\\"onUpdate\\" + foo]: $event => xx = $event
301+
[\\"onUpdate\\" + a()]: $event => xx = $event
302+
}, null, 16), _createVNode(_resolveComponent(\\"F\\"), {
303+
[b.c]: xx,
304+
[b.c + \\"Modifiers\\"]: {
305+
\\"a\\": true
306+
},
307+
[\\"onUpdate\\" + b.c]: $event => xx = $event
306308
}, null, 16)]);"
307309
`;
308310

packages/babel-plugin-jsx/test/snapshot.test.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -174,21 +174,17 @@ const tests: Test[] = [
174174
name: 'v-model target value support variable',
175175
from: `
176176
const foo = 'foo';
177+
178+
const a = () => 'a';
179+
180+
const b = { c: 'c' };
177181
<>
178182
<A v-model={[xx, foo]} />
179183
<B v-model={[xx, ['a']]} />
180184
<C v-model={[xx, foo, ['a']]} />
181-
</>
182-
`,
183-
},
184-
{
185-
name: 'v-models target value support variable',
186-
from: `
187-
const foo = 'foo';
188-
<>
189-
<A v-models={[[xx, foo]]} />
190-
<B v-models={[[xx, ['a']]]} />
191-
<C v-models={[[xx, foo, ['a']]]} />
185+
<D v-model={[xx, foo === 'foo' ? 'a' : 'b', ['a']]} />
186+
<E v-model={[xx, a(), ['a']]} />
187+
<F v-model={[xx, b.c, ['a']]} />
192188
</>
193189
`,
194190
},

packages/jsx-explorer/src/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ interface PersistedState {
2222
window.init = () => {
2323
const { monaco } = window;
2424

25-
const persistedState: PersistedState = JSON.parse(
26-
decodeURIComponent(window.location.hash.slice(1))
27-
|| localStorage.getItem('state')
28-
|| '{}',
29-
);
25+
const persistedState: PersistedState = JSON.parse(localStorage.getItem('state') || '{}');
3026

3127
Object.assign(compilerOptions, persistedState.options);
3228

@@ -66,7 +62,10 @@ window.init = () => {
6662

6763
const reCompile = () => {
6864
const src = editor.getValue();
69-
const state = JSON.stringify(compilerOptions);
65+
const state = JSON.stringify({
66+
src,
67+
options: compilerOptions,
68+
});
7069
localStorage.setItem('state', state);
7170
window.location.hash = encodeURIComponent(src);
7271
console.clear();

0 commit comments

Comments
 (0)