{
args.setData("text", "testing");
}}
getCellContent={getData}
columns={columns}
rows={1000}
/>
);
}
export function IdealSize() {
// trying to be 500x500
const cols: GridColumn[] = [
{ title: "Number", width: 250 },
{ title: "Square", width: 250 },
];
return (
{
args.setData("text", "testing");
}}
getCellContent={getData}
columns={cols}
smoothScrollX={true}
smoothScrollY={true}
rowHeight={50}
headerHeight={50}
rows={9}
/>
);
}
export function DynamicAddRemoveColumns({ columnCount }: { columnCount: number }) {
// trying to be 500x500
const cols: GridColumn[] = [
{ title: "Number", width: 250 },
{ title: "Square", width: 250 },
];
for (let i = 2; i < columnCount; i++) {
cols.push({
title: "Foo",
width: 250,
});
}
return (
);
}
DynamicAddRemoveColumns.args = {
columnCount: 2,
};
export function GridSelectionOutOfRangeNoColumns() {
const dummyCols = useMemo(
() => getDummyCols().map(v => ({ ...v, width: 300, title: "Making column smaller used to crash!" })),
[]
);
const [selected, setSelected] = useState({
current: { cell: [2, 8], range: { width: 1, height: 1, x: 2, y: 8 }, rangeStack: [] },
columns: CompactSelection.empty(),
rows: CompactSelection.empty(),
});
const [cols, setCols] = useState(dummyCols);
const onSelected = useCallback((newSel?: GridSelection) => {
setSelected(newSel);
}, []);
return (
{
if (newSize > 300) {
setCols(dummyCols);
} else {
setCols([]);
}
}}
/>
);
}
type ResizableColumnsSizeMap = Record;
function getResizableColumnsInitSize(): ResizableColumnsSizeMap {
return {
"resize me 0": 120,
"resize me 1": 120,
"resize me 2": 120,
"resize me 3": 120,
"resize me 4": 120,
"resize me 5": 120,
"resize me 6": 120,
"resize me 7": 120,
};
}
function getResizableColumns(sizeMap: ResizableColumnsSizeMap): GridColumn[] {
return Object.entries(sizeMap).map(([title, width]) => ({
title,
width,
icon: "headerString",
hasMenu: true,
}));
}
export function ResizableColumns() {
const [colSizes, setColSizes] = useState(getResizableColumnsInitSize);
const cols = useMemo(() => {
return getResizableColumns(colSizes);
}, [colSizes]);
const onColumnResize = useCallback((column: GridColumn, newSize: number) => {
setColSizes(prevColSizes => {
return {
...prevColSizes,
[column.title]: newSize,
};
});
}, []);
return (
);
}
export function GridSelectionOutOfRangeLessColumnsThanSelection() {
const dummyCols = useMemo(
() => getDummyCols().map(v => ({ ...v, width: 300, title: "Making column smaller used to crash!" })),
[]
);
const [selected, setSelected] = useState({
current: { cell: [2, 8], range: { width: 1, height: 1, x: 2, y: 8 }, rangeStack: [] },
columns: CompactSelection.empty(),
rows: CompactSelection.empty(),
});
const [cols, setCols] = useState(dummyCols);
const onSelected = useCallback((newSel?: GridSelection) => {
setSelected(newSel);
}, []);
return (
{
if (newSize > 300) {
setCols(dummyCols);
} else {
setCols([dummyCols[0]]);
}
}}
/>
);
}
export function GridAddNewRows() {
const cols = useMemo(getDummyCols, []);
const [rowsCount, setRowsCount] = useState(10);
const onRowAppended = useCallback(() => {
setRowsCount(r => r + 1);
}, []);
const [selected, setSelected] = useState(undefined);
const onSelected = useCallback((newSel?: GridSelection) => {
setSelected(newSel);
}, []);
return (
);
}
export function GridNoTrailingBlankRow() {
const cols = useMemo(getDummyCols, []);
const [selected, setSelected] = useState(undefined);
const onSelected = useCallback((newSel?: GridSelection) => {
setSelected(newSel);
}, []);
return (
);
}
export function MarkdownEdits() {
const dummyCols: GridColumn[] = useMemo(() => {
return [
{
title: "MD short",
width: 50,
},
{
title: "MD long",
width: 50,
},
];
}, []);
const dummyCells = useCallback(([col, _row]: Item) => {
if (col === 0) {
const editable: EditableGridCell = {
data: "text",
allowOverlay: true,
kind: GridCellKind.Markdown,
};
return editable;
} else if (col === 1) {
const editable: EditableGridCell = {
data: `text really really really long
## H1
- this
- is
- a
- longer
- example
- to
- test
- scroll
- of
- preview
`,
allowOverlay: true,
kind: GridCellKind.Markdown,
};
return editable;
}
const editable: EditableGridCell = {
data: "text",
allowOverlay: true,
kind: GridCellKind.Markdown,
};
return editable;
}, []);
const [selected, setSelected] = useState({
current: { cell: [2, 8], range: { width: 1, height: 1, x: 2, y: 8 }, rangeStack: [] },
columns: CompactSelection.empty(),
rows: CompactSelection.empty(),
});
const onSelected = useCallback((newSel?: GridSelection) => {
setSelected(newSel);
}, []);
return (
);
}
export const CanEditBoolean = () => {
const [vals, setVals] = useState<[boolean | null | undefined, boolean | null | undefined]>([false, false]);
return (
{
return {
kind: GridCellKind.Boolean,
readonly: col !== 0,
allowOverlay: false,
data: vals[col],
};
}}
onCellEdited={([col], newVal) => {
if (newVal.kind === GridCellKind.Boolean) {
setVals(cv => {
const f = [...cv];
f.splice(col, 1, newVal.data);
return f as [boolean, boolean];
});
}
}}
/>
);
};
export const SimpleEditable = () => {
const [vals, setVals] = useState<[string, string][]>(() => {
const result: [string, string][] = [];
for (let i = 0; i < 2000; i++) {
result.push(["Edit", "Me"]);
}
return result;
});
return (
({
kind: GridCellKind.Text,
allowOverlay: true,
data: vals[row][col],
displayData: vals[row][col],
})}
onCellEdited={([col, row], newVal) => {
const newVals = [...vals];
const newRow: [string, string] = [...newVals[row]];
if (typeof newVal.data === "string") {
newRow[col] = newVal.data;
}
newVals[row] = newRow;
setVals(newVals);
}}
/>
);
};