Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate 'value' attribute for <input type="radio"> when using it with v-model #1775

Closed
2-5 opened this issue Aug 28, 2022 · 5 comments
Closed
Labels
bug Something isn't working

Comments

@2-5
Copy link
Contributor

2-5 commented Aug 28, 2022

The following TypeScript error is thrown:

error TS17001: JSX elements cannot have multiple attributes with the same name.
                 value="One"
                 ~~~~~

for the following template (inspired from https://vuejs.org/guide/essentials/forms.html#radio):

<template>
  <input v-model="picked" type="radio" value="One" />
  <input v-model="checked" type="checkbox" value="Two" />
</template>

<script setup lang="ts">
import { ref } from "vue"

const picked = ref([])
const checked = ref([])
</script>

Volar special-cases the <input type="checkbox"> case which works, but not the <input type="radio">:

https://github.com/johnsoncodehk/volar/blob/master/packages/vue-language-core/src/generators/template.ts#L2000

function getModelValuePropName(node: CompilerDOM.ElementNode, vueVersion: number) {
  const tag = node.tag;
  const typeAttr = node.props.find(prop => prop.type === CompilerDOM.NodeTypes.ATTRIBUTE && prop.name === 'type');
  const type = typeAttr?.value?.content;

// >>>
  if (tag === 'input' && type === 'checkbox')
    return 'checked';
// >>>>>

  if (
    tag === 'input' ||
    tag === 'textarea' ||
    tag === 'select' ||
    vueVersion < 3
  ) return 'value';

  return 'modelValue';
}
@melan0802
Copy link

Got same error, when using v-model and value at same time like this:

<n-checkbox v-model="auth" label="root" value="root" />

vue: 2.7
volar: 0.40.2/0.40.3/0.40.4

tsconfig.json:

{
  "compilerOptions": {
    // ... other options omitted
    "allowSyntheticDefaultImports": true,
    "lib": [
      "dom",
      "esnext"
    ],
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "allowJs": true,
    "sourceMap": true,
    "noImplicitReturns": true,
    "target": "es5",
    "experimentalDecorators": true,
    "baseUrl": ".",
    "paths": {
      "@/*": [ "src/*" ]
    },
    "jsx": "preserve"
  },
  "vueCompilerOptions": {
    "target": 2.7
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.vue"
  ]
}

Temporary solution: Downgrade to 0.40.1.

@mklein994
Copy link

@tekkimariani
Copy link

Got this error when using the checkbox like this:

<input 
    type="checkbox"
    class="checkbox"
    v-model="form.show_password"
    :checked="form.show_password"
    name="show_password"
>

The ':checked' is marked as the problem.

@jarrettmeyer
Copy link

Hack solution, use @change instead of v-model.

@LinusBorg
Copy link
Member

LinusBorg commented Aug 30, 2022

Got this error when using the checkbox like this:

<input 
    type="checkbox"
    class="checkbox"
    v-model="form.show_password"
    :checked="form.show_password"
    name="show_password"
>

The ':checked' is marked as the problem.

Well, this is just invalid. v-model already handles checked, so you should not add it yourself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants