mirror of
https://github.com/tiennm99/ai-coding-workflow-labs.git
synced 2026-09-16 20:21:24 +00:00
- Controlled form with useState for local state - Calls store.addPerson on form submit - Shows error message if addPerson fails - Clears input on successful add - Uses Preact hooks for form state management
33 lines
813 B
React
33 lines
813 B
React
import { useState } from 'preact/hooks';
|
|
import { store } from '../../store/billStore.js';
|
|
|
|
export function PersonForm() {
|
|
const [name, setName] = useState('');
|
|
const [error, setError] = useState('');
|
|
|
|
const handleSubmit = (e) => {
|
|
e.preventDefault();
|
|
const result = store.addPerson(name);
|
|
if (result.success) {
|
|
setName('');
|
|
setError('');
|
|
} else {
|
|
setError(result.error);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<form onSubmit={handleSubmit} class="person-form">
|
|
<input
|
|
type="text"
|
|
value={name}
|
|
onInput={(e) => setName(e.target.value)}
|
|
placeholder="Enter name"
|
|
aria-label="Person name"
|
|
/>
|
|
<button type="submit">Add Person</button>
|
|
{error && <p class="error">{error}</p>}
|
|
</form>
|
|
);
|
|
}
|