Hone logo
Hone
Problems

Reactive Form Input Binding in Vue with TypeScript

This challenge focuses on implementing reactive form input binding in a Vue.js application using TypeScript. Form input binding is a fundamental concept in Vue, allowing you to synchronize data between your Vue component's data and the values entered by the user in form elements. Successfully completing this challenge demonstrates a solid understanding of Vue's reactivity system and TypeScript's type safety.

Problem Description

You are tasked with creating a Vue component that displays a simple form with a text input field and a button. The component should:

  1. Initialize a reactive data property: Create a data property named message of type string and initialize it with an empty string.
  2. Bind the input field: Bind the v-model directive of the text input field to the message data property. This will ensure that changes in the input field are reflected in the message property, and vice versa.
  3. Display the message: Display the current value of the message property in a paragraph element below the input field.
  4. Handle button click: When the button is clicked, clear the message property, setting it back to an empty string.
  5. TypeScript Typing: Ensure all code is properly typed using TypeScript.

Expected Behavior:

  • Initially, the input field should be empty, and the paragraph below should display an empty string.
  • As the user types in the input field, the paragraph should update in real-time to reflect the entered text.
  • When the button is clicked, the input field should clear, and the paragraph should display an empty string.

Examples

Example 1:

Input: User types "Hello Vue!" into the input field.
Output: The paragraph below the input field displays "Hello Vue!".
Explanation: The v-model binding updates the 'message' data property, which is then displayed in the paragraph.

Example 2:

Input: User types "Initial Message" into the input field, then clicks the button.
Output: The input field is cleared, and the paragraph below displays an empty string.
Explanation: The button click handler sets the 'message' data property back to an empty string, triggering a re-render and updating the paragraph.

Example 3: (Edge Case)

Input: User types a very long string (e.g., 1000 characters) into the input field.
Output: The paragraph displays the entire long string without any truncation or errors.
Explanation: Vue's reactivity system handles updating the DOM even with large data values.

Constraints

  • Framework: Vue.js (version 3 or higher)
  • Language: TypeScript
  • Component Structure: The solution should be a single Vue component.
  • Data Binding: Must use v-model for input binding.
  • Performance: The solution should be performant and avoid unnecessary re-renders. Simple reactivity is sufficient for this exercise.
  • Input Length: The input field should be able to handle strings of reasonable length (e.g., up to 2000 characters).

Notes

  • Consider using the ref attribute to access the input element directly if needed (though v-model is the preferred approach).
  • Focus on the core concepts of reactive data binding and event handling.
  • Pay close attention to TypeScript type annotations to ensure type safety.
  • You can use any Vue.js setup method (e.g., Options API or Composition API). The Composition API is generally recommended for new projects.
  • The goal is to demonstrate a clear and concise implementation of form input binding with TypeScript.
Loading editor...
typescript