Hone logo
Hone
Problems

Building a Simple Product Card Component in Vue with TypeScript

This challenge focuses on creating reusable and maintainable Vue components using functional components and TypeScript. Functional components are a lightweight way to create presentational components, and TypeScript adds type safety and improves code readability. You'll build a product card component that displays product information, demonstrating best practices for Vue component development.

Problem Description

You are tasked with creating a functional component in Vue that renders a product card. The component should accept product data as props and display the product's name, price, and a short description. The component should be purely presentational, meaning it doesn't manage any state and solely focuses on rendering the provided data. The component must be written in TypeScript to ensure type safety and improve code maintainability.

Key Requirements:

  • Functional Component: The component must be a functional component using the defineComponent utility.
  • Props: The component must accept the following props:
    • name: string - The name of the product.
    • price: number - The price of the product.
    • description: string - A short description of the product.
    • imageUrl: string - URL of the product image.
  • Rendering: The component must render the product name, price, description, and image within a visually appealing card structure. Use semantic HTML elements (e.g., article, h2, p, img).
  • Type Safety: Props must be properly typed using TypeScript.
  • Reusability: The component should be designed for reusability, accepting different product data without modification.

Expected Behavior:

Given a set of product data, the component should render a card displaying the product's information accurately. The component should not modify the input data. The component should handle cases where the imageUrl is an empty string gracefully (e.g., by displaying a placeholder image or no image at all).

Edge Cases to Consider:

  • Empty description: Handle the case where the product description is an empty string.
  • Missing imageUrl: Handle the case where the product image URL is missing or invalid.
  • Invalid price: While not strictly required, consider how you might handle a negative or non-numeric price (e.g., display a default value or an error message).

Examples

Example 1:

Input: { name: "Awesome T-Shirt", price: 25.99, description: "A comfortable and stylish t-shirt.", imageUrl: "https://example.com/tshirt.jpg" }
Output: A product card displaying "Awesome T-Shirt" as the title, $25.99 as the price, "A comfortable and stylish t-shirt." as the description, and the image from the provided URL.
Explanation: The component correctly renders all provided product details.

Example 2:

Input: { name: "Basic Mug", price: 9.99, description: "", imageUrl: "" }
Output: A product card displaying "Basic Mug" as the title, $9.99 as the price, and no description. No image is displayed.
Explanation: The component handles an empty description and missing image URL gracefully.

Example 3:

Input: { name: "Luxury Watch", price: 199.99, description: "A sophisticated timepiece.", imageUrl: null }
Output: A product card displaying "Luxury Watch" as the title, $199.99 as the price, and "A sophisticated timepiece." as the description. No image is displayed.
Explanation: The component handles a null image URL gracefully.

Constraints

  • Component Type: Must be a functional component using defineComponent.
  • TypeScript: All code must be written in TypeScript.
  • Prop Types: Prop types must be explicitly defined.
  • Rendering: The component must render valid HTML.
  • No State: The component must be purely presentational and should not manage any internal state.

Notes

  • Consider using CSS classes for styling the product card. You can use inline styles, but external CSS is preferred for maintainability.
  • Focus on creating a clean and well-structured component.
  • Think about how to handle potential errors or invalid data gracefully.
  • This challenge is designed to test your understanding of functional components, props, and TypeScript in Vue. Don't overcomplicate the styling; focus on the core component logic.
Loading editor...
typescript