Search code examples
vue.jsvuejs3

How can I get modified value of input in Vue?


I have a dates object that is passed to a component, and when a new date is clicked the list changes so I think I need to use a computed property to set the date. I tried using data but then the value wouldn't change when a new date was clicked.

When I type in the input box, how can I get the value that is currently in it? (in shift1_start)?

<form method="post" @submit.prevent="updateShifts">
                <div class="text-center bg-slate-800 mt-2 rounded-lg">
                    <span>Shift 1</span>

                    <div class="flex items-center justify-between pb-2 px-4">
                        <input name="shift1_start"
                               class="text-gray-900 w-[54px] px-1 text-center rounded border-2 border-gray-500 bg-white inline-flex items-center justify-center invalid:border-red-500 disabled:bg-slate-400 disabled:text-slate-800"
                               pattern="^(2[0-3]|[01]?[0-9]):([0-5]?[0-9])$"
                               maxlength="5"
                               :value="firstSelectedDate.shift1_start"
                        >
                    </div>
                </div>

                <div class="text-center">
                    <button type="submit" class="bg-orange-500 px-2 py-1 mt-2 rounded-lg text-white">Save</button>
                </div>
            </form>
        </div>
    `,

    props: {
        dates: [Object, null]
    },

    methods: {
        updateShifts() {
            console.log(this.firstSelectedDate);
        }
    },

    computed: {
        firstSelectedDate() {
            return {
                nwd: this.dates[0].base_shift_nwd,
                shift1_start: this.dates[0].base_shift1_start,
            };
        }
    }
}

Solution

  • You can can directly bind your date to the input via v-model, there's no need in a computed

    <input ... v-model="dates[0].base_shift1_start"/>