Skip to content

Commit

Permalink
fix(v-model): allow v-model usage on declared custom elements
Browse files Browse the repository at this point in the history
fix #1699
  • Loading branch information
yyx990803 committed Jul 27, 2020
1 parent 2a633c8 commit 71c3c6e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
6 changes: 5 additions & 1 deletion packages/compiler-core/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface ParserOptions {
/**
* Separate option for end users to extend the native elements list
*/
isCustomElement?: (tag: string) => boolean
isCustomElement?: (tag: string) => boolean | void
/**
* Get tag namespace
*/
Expand Down Expand Up @@ -83,6 +83,10 @@ export interface TransformOptions {
* for them.
*/
isBuiltInComponent?: (tag: string) => symbol | void
/**
* Used by some transforms that expects only native elements
*/
isCustomElement?: (tag: string) => boolean | void
/**
* Transform expressions like {{ foo }} to `_ctx.foo`.
* If this option is false, the generated code will be wrapped in a
Expand Down
2 changes: 2 additions & 0 deletions packages/compiler-core/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export function createTransformContext(
directiveTransforms = {},
transformHoist = null,
isBuiltInComponent = NOOP,
isCustomElement = NOOP,
expressionPlugins = [],
scopeId = null,
ssr = false,
Expand All @@ -134,6 +135,7 @@ export function createTransformContext(
directiveTransforms,
transformHoist,
isBuiltInComponent,
isCustomElement,
expressionPlugins,
scopeId,
ssr,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`compiler: transform v-model errors should allow custom element 1`] = `
"const _Vue = Vue
return function render(_ctx, _cache) {
with (_ctx) {
const { vModelText: _vModelText, createVNode: _createVNode, withDirectives: _withDirectives, openBlock: _openBlock, createBlock: _createBlock } = _Vue
return _withDirectives((_openBlock(), _createBlock(\\"my-input\\", {
\\"onUpdate:modelValue\\": $event => (model = $event)
}, null, 8 /* PROPS */, [\\"onUpdate:modelValue\\"])), [
[_vModelText, model]
])
}
}"
`;

exports[`compiler: transform v-model input w/ dynamic v-bind 1`] = `
"const _Vue = Vue
Expand Down
11 changes: 11 additions & 0 deletions packages/compiler-dom/__tests__/transforms/vModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ describe('compiler: transform v-model', () => {
)
})

test('should allow usage on custom element', () => {
const onError = jest.fn()
const root = transformWithModel('<my-input v-model="model" />', {
onError,
isCustomElement: tag => tag.startsWith('my-')
})
expect(root.helpers).toContain(V_MODEL_TEXT)
expect(onError).not.toHaveBeenCalled()
expect(generate(root).code).toMatchSnapshot()
})

test('should raise error if used file input element', () => {
const onError = jest.fn()
transformWithModel(`<input type="file" v-model="test"/>`, {
Expand Down
11 changes: 9 additions & 2 deletions packages/compiler-dom/src/transforms/vModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
}

const { tag } = node
if (tag === 'input' || tag === 'textarea' || tag === 'select') {
if (
tag === 'input' ||
tag === 'textarea' ||
tag === 'select' ||
context.isCustomElement(tag)
) {
let directiveToUse = V_MODEL_TEXT
let isInvalidType = false
if (tag === 'input') {
Expand Down Expand Up @@ -86,7 +91,9 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
}
} else if (tag === 'select') {
directiveToUse = V_MODEL_SELECT
} else if (tag === 'textarea') {
}
{

This comment has been minimized.

Copy link
@ibrahimBeladi

ibrahimBeladi Jul 28, 2020

Is the removal of the else-if by mistake?

// textarea or custom elements
__DEV__ && checkDuplicatedValue()
}
// inject runtime directive
Expand Down

0 comments on commit 71c3c6e

Please sign in to comment.