Skip to content

Commit

Permalink
feat: add insert value menu (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
invisal authored Feb 20, 2024
1 parent 0c76961 commit 475a152
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/app/(components)/ContentMenuHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ function ContextMenuList({ menu }: { menu: OpenContextMenuList }) {
return (
<ContextMenuSub key={menuIndex}>
<ContextMenuSubTrigger inset>{item.title}</ContextMenuSubTrigger>
<ContextMenuSubContent className="w-48">
<ContextMenuSubContent
className={!item.subWidth ? "w-48" : ""}
style={{ width: item.subWidth ? undefined : item.subWidth }}
>
<ContextMenuList menu={item.sub} />
</ContextMenuSubContent>
</ContextMenuSub>
Expand Down
4 changes: 3 additions & 1 deletion src/app/(components)/OptimizeTable/OptimizeTableState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ export default class OptimizeTableState {
getValue(y: number, x: number): unknown {
const rowChange = this.data[y]?.change;
if (rowChange) {
return rowChange[this.headers[x].name] ?? this.getOriginalValue(y, x);
return this.headers[x].name in rowChange
? rowChange[this.headers[x].name]
: this.getOriginalValue(y, x);
}
return this.getOriginalValue(y, x);
}
Expand Down
57 changes: 57 additions & 0 deletions src/components/result/ResultTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,64 @@ export default function ResultTable({ data, tableName }: ResultTableProps) {
state: OptimizeTableState;
event: React.MouseEvent;
}) => {
const randomUUID = crypto.randomUUID();
const timestamp = Math.floor(Date.now() / 1000).toString();
const hasFocus = !!state.getFocus();

function setFocusValue(newValue: unknown) {
const focusCell = state.getFocus();
if (focusCell) {
state.changeValue(focusCell.y, focusCell.x, newValue);
}
}

openContextMenuFromEvent([
{
title: "Insert Value",
disabled: !hasFocus,
subWidth: 200,
sub: [
{
title: <pre>NULL</pre>,
onClick: () => {
setFocusValue(null);
},
},
{
title: <pre>DEFAULT</pre>,
onClick: () => {
setFocusValue(undefined);
},
},
{ separator: true },
{
title: (
<div className="flex flex-col">
<span className="text-xs text-gray-500">Unix Timestamp</span>
<span>{timestamp}</span>
</div>
),
onClick: () => {
setFocusValue(timestamp);
},
},
{ separator: true },
{
title: (
<div className="flex flex-col">
<span className="text-xs text-gray-500">UUID </span>
<span>{randomUUID}</span>
</div>
),
onClick: () => {
setFocusValue(randomUUID);
},
},
],
},
{
separator: true,
},
{
title: "Copy Cell Value",
shortcut: KEY_BINDING.copy.toString(),
Expand Down
3 changes: 2 additions & 1 deletion src/messages/openContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import { LucideIcon } from "lucide-react";
export type OpenContextMenuList = {
type?: "check";
checked?: boolean;
title?: string;
title?: string | JSX.Element;
shortcut?: string;
separator?: boolean;
disabled?: boolean;
destructive?: boolean;
onClick?: () => void;
sub?: OpenContextMenuList;
subWidth?: number;
icon?: LucideIcon;
}[];

Expand Down

0 comments on commit 475a152

Please sign in to comment.