import { useState } from 'react'; import { Dialog, DialogTitle, DialogContent, DialogActions, TextField, Button, Box, InputLabel, FormGroup, FormControl, FormControlLabel, Checkbox, Select, MenuItem, } from '@mui/material'; import { StuffAndCategoryAndAmount, Stuff } from '../types/types'; import { stuffApi } from '../services/api'; import { GENERAL_ERRORS } from '../constants/errorMessages'; import { useMessage } from './MessageContext'; import '../App.css' const AddStuffAmountDialog = ({ openDialog, setOpenDialog, newItem, setNewItem, onSubmit, title = "材料の追加", children, }: { openDialog: boolean, setOpenDialog: (open: boolean) => void, newItem: StuffAndCategoryAndAmount, setNewItem: (item: StuffAndCategoryAndAmount) => void, onSubmit: () => void, title?: string, children?: React.ReactNode, }) => { const onChangeCategory = async (category: string) => { setNewItem({ ...newItem, stuffId: null, category }) const result = category ? await stuffApi.getStuffs(category) : undefined setStuffs(result) } const [stuffs, setStuffs] = useState(); const [newAddition, setNewAddition] = useState(false); // エラーメッセージ表示 const { showErrorMessage } = useMessage(); return ( setOpenDialog(false)} disableScrollLock={true}> {title} } label="食材を新規追加" checked={newAddition} onChange={(e) => setNewAddition((e.target as HTMLInputElement).checked)} /> {/*材料カテゴリ選択 */} カテゴリ {!newAddition && {stuffs ? <> 材料名(選択) : <> {newItem.category ? '読み込み中...' : 'カテゴリを選択してください...'} } } {/* タスクタイトル入力フィールド */} {newAddition && setNewItem({ ...newItem, stuffName: e.target.value })} sx={{ marginBottom: 2 }} />} {/* 数量入力フィールド */} { const value = e.target.value; const parsedValue = parseInt(value, 10); // 数値に変換 if (/*!isNaN(parsedValue) && */ isNaN(parsedValue) || parsedValue >= 1) { //負数除外 setNewItem({ ...newItem, amount: parsedValue }); // number型で保存 } }} onKeyDown={(e) => { if (e.key === '-' || e.key === 'e' || e.key === 'E') { e.preventDefault(); } }} className="numberField" type="number" inputProps={{ inputMode: "numeric", pattern: "[0-9]*" }} // ここで整数のみ許可 /> {children} {/* ここに追加要素がレンダリングされる */} ) } export default AddStuffAmountDialog;