File size: 4,605 Bytes
8934ee3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | import React, { useState } from 'react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import {
Sheet,
SheetClose,
SheetContent,
SheetFooter,
SheetHeader,
SheetTitle,
SheetTrigger,
} from '@/components/ui/sheet';
import { CellType, Column, useTableStore } from '@/store/tableStore';
const AddColumn: React.FC<{ tableId: string }> = ({ tableId }) => {
const { addColumn } = useTableStore();
const [newOption, setNewOption] = useState('');
const [columnName, setColumnName] = useState('');
const [columnType, setColumnType] = useState<CellType>('text');
const [selectOptions, setSelectOptions] = useState<string[]>([]);
const handleAddOption = () => {
if (newOption.trim()) {
setSelectOptions([...selectOptions, newOption.trim()]);
setNewOption('');
}
};
const handleRemoveOption = (indexToRemove: number) => {
setSelectOptions(
selectOptions.filter((_, index) => index !== indexToRemove)
);
};
const handleSave = () => {
if (columnName && columnType) {
const newColumn: Column = {
name: columnName,
type: columnType,
options: columnType === 'select' ? selectOptions : undefined,
};
addColumn(tableId, newColumn);
setColumnName('');
setColumnType('text');
setSelectOptions([]);
setNewOption('');
}
};
return (
<Sheet>
<SheetTrigger asChild>
<Button>Add Column</Button>
</SheetTrigger>
<SheetContent>
<SheetHeader>
<SheetTitle>Add Column</SheetTitle>
</SheetHeader>
<div className="flex flex-col gap-4 py-4">
<div className="grid grid-cols-4 items-center gap-4">
<Label htmlFor="name" className="text-right">
Name
</Label>
<Input
id="name"
value={columnName}
onChange={(e) => setColumnName(e.target.value)}
className="col-span-3"
/>
</div>
<div className="grid grid-cols-4 items-center gap-4">
<Label htmlFor="type" className="text-right">
Type
</Label>
<Select onValueChange={(value) => setColumnType(value as CellType)}>
<SelectTrigger className="col-span-3">
<SelectValue placeholder="Select column type" />
</SelectTrigger>
<SelectContent>
<SelectItem value="text">Text</SelectItem>
<SelectItem value="number">Number</SelectItem>
<SelectItem value="select">Select</SelectItem>
{/* Add more column types as needed */}
</SelectContent>
</Select>
</div>
{columnType === 'select' && (
<>
<div className="grid grid-cols-4 items-center gap-4">
<Label htmlFor="option" className="text-right">
Option
</Label>
<Input
id="option"
value={newOption}
onChange={(e) => setNewOption(e.target.value)}
className="col-span-2"
/>
<Button onClick={handleAddOption}>Add</Button>
</div>
<div className="grid grid-cols-4 items-center gap-4">
<Label className="text-right">Options</Label>
<ul className="col-span-3 flex list-disc flex-col gap-4 pl-5">
{selectOptions.map((option, index) => (
<li
key={index}
className="flex items-center justify-between"
>
<span>{option}</span>
<Button
variant="destructive"
size="sm"
onClick={() => handleRemoveOption(index)}
>
Remove
</Button>
</li>
))}
</ul>
</div>
</>
)}
</div>
<SheetFooter>
<SheetClose asChild>
<Button type="submit" onClick={handleSave}>
Save changes
</Button>
</SheetClose>
</SheetFooter>
</SheetContent>
</Sheet>
);
};
export default AddColumn;
|