Skip to content

Input fields inside loop and v-model update value of all fields #8255

@bjarnef

Description

@bjarnef

Describe the bug

It seems something is wrong when using InputText and DatePicker with v-model inside a loop.

I have something like this:

type Address = {
  date: Date | null,
  from: string,
  to: string,
  distance?: number | null,
};

const items = reactive<Address[]>([
  { date: null, from: "Hello", to: "World" },
  { date: null, from: "", to: "" },
]);
<div class="form-group flex gap-3"
  v-for="(item, index) in items"
  :key="index">
  <PrimeFloatLabel>
    <PrimeDatePicker
      v-model="items[index].date"
      :field="props.field.id + '_date_' + index"
      show-icon
      icon-display="input"
      class="date" />
    <label :for="props.field.id + '_date_' + index">
      Date
    </label>
  </PrimeFloatLabel>
  <PrimeFloatLabel>
    <PrimeInputText
      v-model="items[index].from"
      :field="props.field.id + '_from_' + index"
      :invalid="props.invalid" />
    <label :for="props.field.id + '_from_' + index">
      From address
    </label>
  </PrimeFloatLabel>
  <PrimeFloatLabel>
    <PrimeInputText
      v-model="items[index].to"
      :field="props.field.id + '_to_' + index"
      :invalid="props.invalid" />
    <label :for="props.field.id + '_to_' + index">
      To address
    </label>
  </PrimeFloatLabel>
</div>

When a update any input (date or text) it correctly update the model property in the specific item, but it updates value of all fields. With initial model values it looks correct:

Image

When I start typing in a field, the model value it as expected, but for some reason it update value of all input fields.

Image

If I use native input field instead it works as expected.

<div class="form-group flex gap-3"
  v-for="(item, index) in items"
  :key="index">
  <input
      type="date"
      v-model="items[index].date" />
  <input
      type="text"
      v-model="items[index].from" />
  <input
      type="text"
      v-model="items[index].to" />
</div>
Image

Pull Request Link

No response

Reason for not contributing a PR

  • Lack of time
  • Unsure how to implement the fix/feature
  • Difficulty understanding the codebase
  • Other

Other Reason

No response

Reproducer

Environment

Window 10 Pro

Vue version

3.3.8

PrimeVue version

4.3.5

Node version

No response

Browser(s)

Chrome Version 141.0.7390.12

Steps to reproduce the behavior

Expected behavior

Only update value for the specific input.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: Needs TriageIssue will be reviewed by Core Team and a relevant label will be added as soon as possible

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions