mirror of
https://github.com/tiennm99/gsd-framework.git
synced 2026-05-28 18:23:16 +00:00
49142bdfdd
- Controlled form with name and price inputs - Calls store.addItem on form submit - Shows error message on validation failure - Clears inputs on successful add
42 lines
1.0 KiB
React
42 lines
1.0 KiB
React
import { useState } from 'preact/hooks';
|
|
import { store } from '../../store/billStore.js';
|
|
|
|
export function ItemForm() {
|
|
const [name, setName] = useState('');
|
|
const [price, setPrice] = useState('');
|
|
const [error, setError] = useState('');
|
|
|
|
const handleSubmit = (e) => {
|
|
e.preventDefault();
|
|
const result = store.addItem(name, price);
|
|
if (result.success) {
|
|
setName('');
|
|
setPrice('');
|
|
setError('');
|
|
} else {
|
|
setError(result.error);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<form onSubmit={handleSubmit} class="item-form">
|
|
<input
|
|
type="text"
|
|
value={name}
|
|
onInput={(e) => setName(e.target.value)}
|
|
placeholder="Item name"
|
|
aria-label="Item name"
|
|
/>
|
|
<input
|
|
type="text"
|
|
value={price}
|
|
onInput={(e) => setPrice(e.target.value)}
|
|
placeholder="Price (e.g., 15.99)"
|
|
aria-label="Item price"
|
|
/>
|
|
<button type="submit">Add Item</button>
|
|
{error && <p class="error">{error}</p>}
|
|
</form>
|
|
);
|
|
}
|