|
| 1 | +/* |
| 2 | + * 評価方法フィルターのコンポーネント |
| 3 | + */ |
| 4 | + |
| 5 | +"use client"; |
| 6 | +import React, { ReactNode } from "react"; |
| 7 | +import { Evaluation } from "@/app/type"; |
| 8 | +import Checkbox from "../UI/Checkbox"; |
| 9 | + |
| 10 | +const evaluations: Evaluation[] = ["試験", "レポート", "出席", "平常"]; |
| 11 | + |
| 12 | +/** |
| 13 | + * 評価方法フィルターのプロパティ |
| 14 | + */ |
| 15 | +interface EvaluationProp { |
| 16 | + evaluation_included?: Evaluation[]; |
| 17 | + evaluation_excluded?: Evaluation[]; |
| 18 | + setEvaluation: ( |
| 19 | + evaluation_included: Evaluation[], |
| 20 | + evaluation_excluded: Evaluation[], |
| 21 | + ) => void; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * 評価方法フィルターのコンポーネント |
| 26 | + * @param prop 評価方法フィルターのプロパティ |
| 27 | + * @returns 評価方法フィルターのコンポーネント |
| 28 | + */ |
| 29 | +export const EvaluationFilter: React.FC<EvaluationProp> = ( |
| 30 | + prop: EvaluationProp, |
| 31 | +) => { |
| 32 | + const slots: ReactNode[] = []; |
| 33 | + |
| 34 | + slots.push(<div key={"void"} />); |
| 35 | + slots.push(<div key={"label_in"}>含む</div>); |
| 36 | + slots.push(<div key={"label_ex"}>除外</div>); |
| 37 | + |
| 38 | + evaluations.map((ev) => { |
| 39 | + slots.push(<div key={ev + "header"}>{ev.substring(0, 2)}</div>); |
| 40 | + slots.push( |
| 41 | + <CheckboxInGrid |
| 42 | + isInclude={true} |
| 43 | + ev={ev} |
| 44 | + key={ev + "included"} |
| 45 | + prop={prop} |
| 46 | + />, |
| 47 | + ); |
| 48 | + slots.push( |
| 49 | + <CheckboxInGrid |
| 50 | + isInclude={false} |
| 51 | + ev={ev} |
| 52 | + key={ev + "excluded"} |
| 53 | + prop={prop} |
| 54 | + />, |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + return <div className="grid grid-rows-3 grid-flow-col gap-2">{slots}</div>; |
| 59 | +}; |
| 60 | + |
| 61 | +/** |
| 62 | + * グリッド上に並べるチェックボックスのコンポーネント |
| 63 | + * @param param0 プロパティ |
| 64 | + * @param param0.isInclude このチェックボックスが、含めたい評価方法を示しているか否か |
| 65 | + * @param param0.ev 評価方法 |
| 66 | + * @param param0.prop 評価方法のフィルターコンポーネントのプロパティ |
| 67 | + * @returns チェックボックスコンポーネント |
| 68 | + */ |
| 69 | +const CheckboxInGrid: React.FC<{ |
| 70 | + isInclude: boolean; |
| 71 | + ev: Evaluation; |
| 72 | + prop: EvaluationProp; |
| 73 | +}> = ({ isInclude, ev, prop }) => { |
| 74 | + let evaluation_included = prop.evaluation_included ?? []; |
| 75 | + let evaluation_excluded = prop.evaluation_excluded ?? []; |
| 76 | + let myEvaluation = isInclude ? evaluation_included : evaluation_excluded; |
| 77 | + let otherEvaluation = isInclude ? evaluation_excluded : evaluation_included; |
| 78 | + |
| 79 | + // クリックされたときの挙動 |
| 80 | + const onClick = (ev: Evaluation) => { |
| 81 | + if (myEvaluation.includes(ev)) { |
| 82 | + // もともとチェックされていたボックスをクリックしたら、外す |
| 83 | + myEvaluation.splice(myEvaluation.indexOf(ev), 1); |
| 84 | + } else { |
| 85 | + // もともとチェックされていなかったボックスをクリックしたら |
| 86 | + const index = otherEvaluation.indexOf(ev); // 相方のチェックがされているかを確認 |
| 87 | + if (index >= 0) otherEvaluation.splice(index, 1); // 相方のチェックを外す |
| 88 | + console.log(index); |
| 89 | + myEvaluation.push(ev); // 自分のボックスにチェックを入れる |
| 90 | + } |
| 91 | + |
| 92 | + prop.setEvaluation(evaluation_included, evaluation_excluded); |
| 93 | + }; |
| 94 | + |
| 95 | + return ( |
| 96 | + <Checkbox |
| 97 | + checked={myEvaluation.includes(ev)} |
| 98 | + onChange={(_) => onClick(ev)} |
| 99 | + /> |
| 100 | + ); |
| 101 | +}; |
0 commit comments