|
|
@@ -0,0 +1,51 @@
|
|
|
+import React, { useState } from 'react';
|
|
|
+import { Product } from '../types';
|
|
|
+
|
|
|
+interface AddProductFormProps {
|
|
|
+ onAddProduct: (product: Product) => void;
|
|
|
+}
|
|
|
+
|
|
|
+const AddProductForm: React.FC<AddProductFormProps> = ({ onAddProduct }) => {
|
|
|
+ const [name, setName] = useState('');
|
|
|
+ const [price, setPrice] = useState('');
|
|
|
+
|
|
|
+ const handleSubmit = (e: React.FormEvent) => {
|
|
|
+ e.preventDefault();
|
|
|
+ if (name && price) {
|
|
|
+ onAddProduct({ name, price: parseFloat(price), id: 0 });
|
|
|
+ setName('');
|
|
|
+ setPrice('');
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ return (
|
|
|
+ <form onSubmit={handleSubmit}>
|
|
|
+ <h2>Add New Product</h2>
|
|
|
+ <div>
|
|
|
+ <label htmlFor="name">Name:</label>
|
|
|
+ <input
|
|
|
+ type="text"
|
|
|
+ id="name"
|
|
|
+ value={name}
|
|
|
+ onChange={(e) => setName(e.target.value)}
|
|
|
+ required
|
|
|
+ />
|
|
|
+ </div>
|
|
|
+ <div>
|
|
|
+ <label htmlFor="price">Price:</label>
|
|
|
+ <input
|
|
|
+ type="number"
|
|
|
+ id="price"
|
|
|
+ value={price}
|
|
|
+ onChange={(e) => setPrice(e.target.value)}
|
|
|
+ min="0"
|
|
|
+ step="0.01"
|
|
|
+ required
|
|
|
+ />
|
|
|
+ </div>
|
|
|
+ <button type="submit">Add Product</button>
|
|
|
+ </form>
|
|
|
+ );
|
|
|
+};
|
|
|
+
|
|
|
+export default AddProductForm;
|