import React, { FormEvent } from 'react'; import { IconButton } from '../button/IconButton'; import styles from './caloriesInput.css'; type CaloriesInputProps = { addCalories: (calories: { title: string, count: number}) => void, saveCalories: (calories: { title: string, count: number}) => void, item?: CalorieValue }; interface ICalorieInputForm extends HTMLFormElement { caloriesTitle: HTMLInputElement; caloriesCount: HTMLInputElement; } function CaloriesInput ({ addCalories, saveCalories, item }: CaloriesInputProps) { function onSubmit (event: FormEvent) { event.preventDefault(); const form = event.currentTarget; const title = form.caloriesTitle.value; const count = Number(form.caloriesCount.value) || 0; if (!item) { addCalories({ title, count }); form.querySelectorAll('input').forEach((formElement) => { formElement.value = ''; formElement.blur(); }); } else { saveCalories({ title, count}); } } const { title: itemTitle = '', count: itemCount = '' } = item || {}; const buttonIcon = item ? 'tick' : 'plus'; return (
); } export { CaloriesInput };