|
| 1 | +import { useState } from "react"; |
| 2 | +import { usePlayground } from "./state"; |
| 3 | +import type { ColumnOption, ColumnType } from "./types"; |
| 4 | + |
| 5 | +const typeOptions: { label: string; value: ColumnType }[] = [ |
| 6 | + { label: "Text", value: "text" }, |
| 7 | + { label: "Number", value: "number" }, |
| 8 | + { label: "Boolean", value: "boolean" }, |
| 9 | + { label: "Select", value: "select" }, |
| 10 | + { label: "Multi-select", value: "multi-select" }, |
| 11 | + { label: "Date", value: "date" }, |
| 12 | +]; |
| 13 | + |
| 14 | +export const ColumnEditor = () => { |
| 15 | + const { columns, addColumn, updateColumn, removeColumn } = usePlayground(); |
| 16 | + const [draft, setDraft] = useState({ name: "", type: "text" as ColumnType }); |
| 17 | + |
| 18 | + const onAddOption = (id: string) => { |
| 19 | + const col = columns.find((c) => c.id === id); |
| 20 | + if (!col) return; |
| 21 | + const options: ColumnOption[] = col.options ?? []; |
| 22 | + const next = [ |
| 23 | + ...options, |
| 24 | + { |
| 25 | + id: Math.random().toString(36).slice(2, 8), |
| 26 | + label: `Option ${options.length + 1}`, |
| 27 | + }, |
| 28 | + ]; |
| 29 | + updateColumn(id, { options: next }); |
| 30 | + }; |
| 31 | + |
| 32 | + return ( |
| 33 | + <div className="space-y-3"> |
| 34 | + <div className="space-y-2"> |
| 35 | + {columns.map((col) => ( |
| 36 | + <div |
| 37 | + key={col.id} |
| 38 | + className="rounded border border-gray-200 dark:border-gray-700 p-2 bg-white dark:bg-gray-900" |
| 39 | + > |
| 40 | + <div className="flex items-center justify-between gap-2"> |
| 41 | + <div className="flex items-center gap-2"> |
| 42 | + <input |
| 43 | + className="rounded border border-gray-300 dark:border-gray-600 px-2 py-1 text-sm bg-white dark:bg-gray-950 dark:text-gray-100" |
| 44 | + value={col.name} |
| 45 | + onChange={(e) => |
| 46 | + updateColumn(col.id, { name: e.target.value }) |
| 47 | + } |
| 48 | + /> |
| 49 | + <select |
| 50 | + className="rounded border border-gray-300 dark:border-gray-600 px-2 py-1 text-sm bg-white dark:bg-gray-950 dark:text-gray-100" |
| 51 | + value={col.type} |
| 52 | + onChange={(e) => |
| 53 | + updateColumn(col.id, { type: e.target.value as ColumnType }) |
| 54 | + } |
| 55 | + > |
| 56 | + {typeOptions.map((opt) => ( |
| 57 | + <option key={opt.value} value={opt.value}> |
| 58 | + {opt.label} |
| 59 | + </option> |
| 60 | + ))} |
| 61 | + </select> |
| 62 | + </div> |
| 63 | + <button |
| 64 | + className="text-xs text-red-600 dark:text-red-400 hover:underline" |
| 65 | + onClick={() => removeColumn(col.id)} |
| 66 | + > |
| 67 | + Remove |
| 68 | + </button> |
| 69 | + </div> |
| 70 | + {(col.type === "select" || col.type === "multi-select") && ( |
| 71 | + <div className="mt-2 space-y-1"> |
| 72 | + <div className="flex items-center justify-between text-xs text-gray-500"> |
| 73 | + <span>Options</span> |
| 74 | + <button |
| 75 | + className="text-blue-600 dark:text-blue-400 hover:underline" |
| 76 | + onClick={() => onAddOption(col.id)} |
| 77 | + > |
| 78 | + Add option |
| 79 | + </button> |
| 80 | + </div> |
| 81 | + <div className="flex flex-wrap gap-2"> |
| 82 | + {(col.options ?? []).map((opt, idx) => ( |
| 83 | + <div key={opt.id} className="flex items-center gap-1"> |
| 84 | + <input |
| 85 | + className="rounded border border-gray-300 dark:border-gray-600 px-2 py-1 text-xs bg-white dark:bg-gray-950 dark:text-gray-100" |
| 86 | + value={opt.label} |
| 87 | + onChange={(e) => { |
| 88 | + const next = [...(col.options ?? [])]; |
| 89 | + next[idx] = { ...opt, label: e.target.value }; |
| 90 | + updateColumn(col.id, { options: next }); |
| 91 | + }} |
| 92 | + /> |
| 93 | + <button |
| 94 | + className="text-[11px] text-red-600 dark:text-red-400 hover:text-red-700 dark:hover:text-red-300" |
| 95 | + onClick={() => { |
| 96 | + const next = (col.options ?? []).filter( |
| 97 | + (o) => o.id !== opt.id, |
| 98 | + ); |
| 99 | + updateColumn(col.id, { options: next }); |
| 100 | + }} |
| 101 | + > |
| 102 | + ✕ |
| 103 | + </button> |
| 104 | + </div> |
| 105 | + ))} |
| 106 | + </div> |
| 107 | + </div> |
| 108 | + )} |
| 109 | + </div> |
| 110 | + ))} |
| 111 | + </div> |
| 112 | + |
| 113 | + <div className="flex items-end gap-2"> |
| 114 | + <div className="flex flex-col w-40"> |
| 115 | + <label className="text-xs text-gray-500">Name</label> |
| 116 | + <input |
| 117 | + className="rounded border border-gray-300 dark:border-gray-600 px-2 py-1 text-sm bg-white dark:bg-gray-950 dark:text-gray-100" |
| 118 | + value={draft.name} |
| 119 | + onChange={(e) => |
| 120 | + setDraft((prev) => ({ ...prev, name: e.target.value })) |
| 121 | + } |
| 122 | + placeholder="Column name" |
| 123 | + /> |
| 124 | + </div> |
| 125 | + <div className="flex flex-col w-40"> |
| 126 | + <label className="text-xs text-gray-500">Type</label> |
| 127 | + <select |
| 128 | + className="rounded border border-gray-300 dark:border-gray-600 px-2 py-1 text-sm bg-white dark:bg-gray-950 dark:text-gray-100" |
| 129 | + value={draft.type} |
| 130 | + onChange={(e) => |
| 131 | + setDraft((prev) => ({ |
| 132 | + ...prev, |
| 133 | + type: e.target.value as ColumnType, |
| 134 | + })) |
| 135 | + } |
| 136 | + > |
| 137 | + {typeOptions.map((opt) => ( |
| 138 | + <option key={opt.value} value={opt.value}> |
| 139 | + {opt.label} |
| 140 | + </option> |
| 141 | + ))} |
| 142 | + </select> |
| 143 | + </div> |
| 144 | + <button |
| 145 | + className="rounded bg-blue-600 px-3 py-1 text-sm text-white hover:bg-blue-700 disabled:opacity-60 dark:bg-blue-600 dark:hover:bg-blue-500" |
| 146 | + disabled={!draft.name.trim()} |
| 147 | + onClick={() => { |
| 148 | + addColumn({ name: draft.name.trim(), type: draft.type }); |
| 149 | + setDraft({ name: "", type: "text" }); |
| 150 | + }} |
| 151 | + > |
| 152 | + Add column |
| 153 | + </button> |
| 154 | + </div> |
| 155 | + </div> |
| 156 | + ); |
| 157 | +}; |
0 commit comments