Skip to content

Commit 0604214

Browse files
committed
fix: Add events at the begining of argument list
1 parent ee8f6df commit 0604214

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed

Diff for: packages/babel-sugar-v-model/src/index.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ const isComponent = (t, path) => {
112112
if (t.isJSXMemberExpression(name)) {
113113
return true
114114
}
115-
115+
116116
const tag = name.get('name').node
117117

118118
return !htmlTags.includes(tag) && !svgTags.includes(tag)
@@ -155,7 +155,7 @@ const getType = (t, path) => {
155155
* @param body Array<Statement>
156156
*/
157157
const addHandler = (t, path, event, body) => {
158-
addProp(t, path, `on-${event}`, t.arrowFunctionExpression([t.identifier('$event')], t.blockStatement(body)))
158+
addProp(t, path, `on-${event}`, t.arrowFunctionExpression([t.identifier('$event')], t.blockStatement(body)), true)
159159
}
160160

161161
/**
@@ -166,8 +166,10 @@ const addHandler = (t, path, event, body) => {
166166
* @param propName string
167167
* @param expression Expression
168168
*/
169-
const addProp = (t, path, propName, expression) => {
170-
path.node.attributes.push(t.jSXAttribute(t.jSXIdentifier(propName), t.jSXExpressionContainer(expression)))
169+
const addProp = (t, path, propName, expression, unshift = false) => {
170+
path.node.attributes[unshift ? 'unshift' : 'push'](
171+
t.jSXAttribute(t.jSXIdentifier(propName), t.jSXExpressionContainer(expression)),
172+
)
171173
}
172174

173175
/**

Diff for: packages/babel-sugar-v-model/test/snapshot.js

+34-20
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ const tests = [
127127
{
128128
name: 'Generic input[type="checkbox"] vModel',
129129
from: `const A = <input type="checkbox" vModel={a.b} />`,
130-
to: `const A = <input type="checkbox" domProps-checked={Array.isArray(a.b) ? this._i(a.b, null) > -1 : a.b} on-change={$event => {
130+
to: `const A = <input on-change={$event => {
131131
const $$a = a.b,
132132
$$el = $event.target,
133133
$$c = $$el.checked ? true : false;
@@ -144,7 +144,7 @@ const tests = [
144144
} else {
145145
a.b = $$c;
146146
}
147-
}} {...{
147+
}} type="checkbox" domProps-checked={Array.isArray(a.b) ? this._i(a.b, null) > -1 : a.b} {...{
148148
directives: [{
149149
name: "model",
150150
value: a.b,
@@ -155,7 +155,7 @@ const tests = [
155155
{
156156
name: 'input[type="checkbox"] vModel_number',
157157
from: `const A = <input type="checkbox" vModel_number={a.b} />`,
158-
to: `const A = <input type="checkbox" domProps-checked={Array.isArray(a.b) ? this._i(a.b, null) > -1 : a.b} on-change={$event => {
158+
to: `const A = <input on-change={$event => {
159159
const $$a = a.b,
160160
$$el = $event.target,
161161
$$c = $$el.checked ? true : false;
@@ -172,7 +172,7 @@ const tests = [
172172
} else {
173173
a.b = $$c;
174174
}
175-
}} {...{
175+
}} type="checkbox" domProps-checked={Array.isArray(a.b) ? this._i(a.b, null) > -1 : a.b} {...{
176176
directives: [{
177177
name: "model",
178178
value: a.b,
@@ -185,7 +185,7 @@ const tests = [
185185
{
186186
name: 'input[type="checkbox"] vModel with value, trueValue and falseValue',
187187
from: `const A = <input type="checkbox" vModel={a.b} value="abc" trueValue={trueVal} falseValue={falseVal} />`,
188-
to: `const A = <input type="checkbox" domProps-checked={Array.isArray(a.b) ? this._i(a.b, "abc") > -1 : this._q(a.b, trueVal)} on-change={$event => {
188+
to: `const A = <input on-change={$event => {
189189
const $$a = a.b,
190190
$$el = $event.target,
191191
$$c = $$el.checked ? trueVal : falseVal;
@@ -202,7 +202,7 @@ const tests = [
202202
} else {
203203
a.b = $$c;
204204
}
205-
}} {...{
205+
}} type="checkbox" domProps-checked={Array.isArray(a.b) ? this._i(a.b, "abc") > -1 : this._q(a.b, trueVal)} {...{
206206
directives: [{
207207
name: "model",
208208
value: a.b,
@@ -213,9 +213,9 @@ const tests = [
213213
{
214214
name: 'Generic input[type="radio"] vModel',
215215
from: `const A = <input type="radio" vModel={a.b} />`,
216-
to: `const A = <input type="radio" domProps-checked={this._q(a.b, null)} on-change={$event => {
216+
to: `const A = <input on-change={$event => {
217217
a.b = null;
218-
}} {...{
218+
}} type="radio" domProps-checked={this._q(a.b, null)} {...{
219219
directives: [{
220220
name: "model",
221221
value: a.b,
@@ -226,9 +226,9 @@ const tests = [
226226
{
227227
name: 'input[type="radio"] vModel_number',
228228
from: `const A = <input type="radio" vModel_number={a.b} value="10" />`,
229-
to: `const A = <input type="radio" domProps-checked={this._q(a.b, this._n("10"))} on-change={$event => {
229+
to: `const A = <input on-change={$event => {
230230
a.b = this._n("10");
231-
}} {...{
231+
}} type="radio" domProps-checked={this._q(a.b, this._n("10"))} {...{
232232
directives: [{
233233
name: "model",
234234
value: a.b,
@@ -241,10 +241,10 @@ const tests = [
241241
{
242242
name: 'Generic input[type="text"] vModel',
243243
from: `const A = <input type="text" vModel={a.b} />`,
244-
to: `const A = <input type="text" domProps-value={a.b} on-input={$event => {
244+
to: `const A = <input on-input={$event => {
245245
if ($event.target.composing) return;
246246
a.b = $event.target.value;
247-
}} {...{
247+
}} type="text" domProps-value={a.b} {...{
248248
directives: [{
249249
name: "model",
250250
value: a.b,
@@ -255,10 +255,10 @@ const tests = [
255255
{
256256
name: 'Generic textarea vModel',
257257
from: `const A = <textarea vModel={a.b} />`,
258-
to: `const A = <textarea domProps-value={a.b} on-input={$event => {
258+
to: `const A = <textarea on-input={$event => {
259259
if ($event.target.composing) return;
260260
a.b = $event.target.value;
261-
}} {...{
261+
}} domProps-value={a.b} {...{
262262
directives: [{
263263
name: "model",
264264
value: a.b,
@@ -269,11 +269,11 @@ const tests = [
269269
{
270270
name: 'input[type="text"] vModel_lazy_trim_number',
271271
from: `const A = <input type="text" vModel_lazy_trim_number={a.b} />`,
272-
to: `const A = <input type="text" domProps-value={a.b} on-change={$event => {
273-
a.b = this._n($event.target.value.trim());
274-
}} on-blur={$event => {
272+
to: `const A = <input on-blur={$event => {
275273
this.$forceUpdate();
276-
}} {...{
274+
}} on-change={$event => {
275+
a.b = this._n($event.target.value.trim());
276+
}} type="text" domProps-value={a.b} {...{
277277
directives: [{
278278
name: "model",
279279
value: a.b,
@@ -288,14 +288,28 @@ const tests = [
288288
{
289289
name: 'input[type="range"] vModel',
290290
from: `const A = <input type="range" vModel={a.b} />`,
291-
to: `const A = <input type="range" domProps-value={a.b} on-__r={$event => {
291+
to: `const A = <input on-__r={$event => {
292292
a.b = $event.target.value;
293-
}} {...{
293+
}} type="range" domProps-value={a.b} {...{
294294
directives: [{
295295
name: "model",
296296
value: a.b,
297297
modifiers: {}
298298
}]
299+
}} />;`,
300+
},
301+
{
302+
name: 'event listener added before other listeners',
303+
from: `const A = <input onInput={this.someMethod} vModel={this.model} />`,
304+
to: `const A = <input on-input={$event => {
305+
if ($event.target.composing) return;
306+
this.model = $event.target.value;
307+
}} onInput={this.someMethod} domProps-value={this.model} {...{
308+
directives: [{
309+
name: "model",
310+
value: this.model,
311+
modifiers: {}
312+
}]
299313
}} />;`,
300314
},
301315
]

0 commit comments

Comments
 (0)