A grid is rows and columns of cells that the user selects and edits. Grid is the one under Composer’s sheets and
tables. It draws only the cells in view, so it stays fast for large data.
Grid also handles selection, keyboard movement and resizing. You pass it a getCells function, and it calls the
function for the cells it shows.
@param ― iterable An iterable object to convert to an array.
@param ― mapfn A mapping function to call on every element of the array.
@param ― thisArg Value of 'this' used to invoke the mapfn.
from({
ArrayLike<T>.length: number
length: 40 }, () => ['', '', '', '']),
13
];
14
15
/** Blank columns past the totals, so the sheet runs off the right edge too. */
16
const
const BLANK_COLUMNS: 6
Blank columns past the totals, so the sheet runs off the right edge too.
BLANK_COLUMNS = 6;
17
18
const
const TOTAL: number
TOTAL =
const COLUMNS: string[]
COLUMNS.
Array<string>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length - 1;
19
20
/** Each row's total, from the quarters before it; a cell that is not a number counts as zero, and a blank row has none. */
21
const
consttotal: (row: string[]) =>string
Each row's total, from the quarters before it; a cell that is not a number counts as zero, and a blank row has none.
total = (
row: string[]
row: string[]) =>
22
row: string[]
row.
Array<string>.every<"">(predicate: (value: string, index: number, array: string[]) => value is "", thisArg?: any): this is S[] (+1 overload)
Determines whether all the members of an array satisfy the specified test.
@param ― predicate A function that accepts up to three arguments. The every method calls
the predicate function for each element in the array until the predicate returns a value
which is coercible to the Boolean value false, or until the end of the array.
@param ― thisArg An object to which the this keyword can refer in the predicate function.
If thisArg is omitted, undefined is used as the this value.
every((
value: string
value) =>
value: string
value === '')
23
? ''
24
:
var String: StringConstructor
(value?: any) => string
Allows manipulation and formatting of text strings and determination and location of substrings within strings.
Returns a copy of a section of an array.
For both start and end, a negative index can be used to indicate an offset from the end of the array.
For example, -2 refers to the second to last element of the array.
@param ― start The beginning index of the specified portion of the array.
If start is undefined, then the slice begins at index 0.
@param ― end The end index of the specified portion of the array. This is exclusive of the element at the index 'end'.
If end is undefined, then the slice extends to the end of the array.
Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
@param ― callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
@param ― initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
reduce((
sum: number
sum,
value: string
value) =>
sum: number
sum + (
var Number: NumberConstructor
(value?: any) => number
An object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.
Number(
value: string
value) || 0), 0),
28
);
29
30
/** The cells of a plane, keyed `col,row`: the header row is a frozen plane, and the values are the grid plane. */
Calls a defined callback function on each element of an array, and returns an array that contains the results.
@param ― callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
map((
label: string
label,
col: number
col) => [
35
`${
col: number
col},0`,
36
{
value: string
value:
label: string
label,
readonly: true
readonly: true },
37
]),
38
);
39
}
40
return
var Object: ObjectConstructor
Provides functionality common to all JavaScript objects.
Object.
ObjectConstructor.fromEntries(entries: Iterable<readonly any[]>): any (+1 overload)
Returns an object created by key-value entries for properties and methods
@param ― entries An iterable object that contains key-value entries for properties and methods.
Calls a defined callback function on each element of an array. Then, flattens the result into
a new array.
This is identical to a map followed by flat with depth 1.
@param ― callback A function that accepts up to three arguments. The flatMap method calls the
callback function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the callback function. If
thisArg is omitted, undefined is used as the this value.
Calls a defined callback function on each element of an array, and returns an array that contains the results.
@param ― callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
map((
value: string
value,
col: number
col) => [`${
col: number
col},${
rowIndex: number
rowIndex}`, {
value: string
value }]),
43
[`${
const TOTAL: number
TOTAL},${
rowIndex: number
rowIndex}`, {
value: string
value:
consttotal: (row: string[]) =>string
Each row's total, from the quarters before it; a cell that is not a number counts as zero, and a blank row has none.
total(
row: string[]
row),
readonly: true
readonly: true }],
44
]),
45
);
46
};
47
48
const
const frozen: {
frozenRowsStart: number;
}
frozen = {
frozenRowsStart: number
frozenRowsStart: 1 };
49
const
const columnDefault: {
grid: {
size: number;
};
}
columnDefault = {
grid: {
size: number;
}
grid: {
size: number
size: 112 } };
50
const
const rowDefault: {
frozenRowsStart: {
size: number;
readonly: boolean;
};
grid: {
size: number;
};
}
rowDefault = {
51
frozenRowsStart: {
size: number;
readonly: boolean;
}
frozenRowsStart: {
size: number
size: 32,
readonly: boolean
readonly: true },
52
grid: {
size: number;
}
grid: {
size: number
size: 32 },
53
};
54
55
/** A small budget: double-click or type into a cell to edit it, and the row's total follows. */
56
exportconst
constBudgetSheet: () =>React.JSX.Element
A small budget: double-click or type into a cell to edit it, and the row's total follows.
Calls a defined callback function on each element of an array, and returns an array that contains the results.
@param ― callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
Calls a defined callback function on each element of an array, and returns an array that contains the results.
@param ― callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
map((
cell: string
cell,
column: number
column) => (
column: number
column ===
const col: number
col ?
value: string
value :
cell: string
cell))
74
:
cells: string[]
cells,
75
),
76
);
77
}
78
};
79
80
// Enter and Tab close the editor and move the focus down or across, as a spreadsheet does.
Allows getting a ref to the component instance.
Once the component unmounts, React will set ref.current to null
(or call the ref with null if you passed a callback ref).
toCells returns the header row for the frozenRowsStart plane and the values for the grid plane. handleBlur
writes the new text into rows. That makes a new getCells, so the grid asks again and the row’s total follows.
Grid.Content also gets three props that make the sheet behave like a spreadsheet:
limitRows is the length of rows, which ends in 40 blank rows for the user to fill in.
limitColumns runs six columns past the data, so the sheet runs off the right edge. handleBlur writes only into
a row’s four cells, so it drops text typed in a blank column.
overscroll='trap' keeps wheel and trackpad scrolls in the grid, so the page neither scrolls nor swipes back.
The story at the top of this page runs this example.