UNPKG

3.63 kBJavaScriptView Raw
1/**
2 * External dependencies
3 */
4import { times } from 'lodash';
5
6/**
7 * Creates a table state.
8 *
9 * @param {number} options.rowCount Row count for the table to create.
10 * @param {number} options.columnCount Column count for the table to create.
11 *
12 * @return {Object} New table state.
13 */
14export function createTable( {
15 rowCount,
16 columnCount,
17} ) {
18 return {
19 body: times( rowCount, () => ( {
20 cells: times( columnCount, () => ( {
21 content: '',
22 tag: 'td',
23 } ) ),
24 } ) ),
25 };
26}
27
28/**
29 * Updates cell content in the table state.
30 *
31 * @param {Object} state Current table state.
32 * @param {string} options.section Section of the cell to update.
33 * @param {number} options.rowIndex Row index of the cell to update.
34 * @param {number} options.columnIndex Column index of the cell to update.
35 * @param {Array} options.content Content to set for the cell.
36 *
37 * @return {Object} New table state.
38 */
39export function updateCellContent( state, {
40 section,
41 rowIndex,
42 columnIndex,
43 content,
44} ) {
45 return {
46 [ section ]: state[ section ].map( ( row, currentRowIndex ) => {
47 if ( currentRowIndex !== rowIndex ) {
48 return row;
49 }
50
51 return {
52 cells: row.cells.map( ( cell, currentColumnIndex ) => {
53 if ( currentColumnIndex !== columnIndex ) {
54 return cell;
55 }
56
57 return {
58 ...cell,
59 content,
60 };
61 } ),
62 };
63 } ),
64 };
65}
66
67/**
68 * Inserts a row in the table state.
69 *
70 * @param {Object} state Current table state.
71 * @param {string} options.section Section in which to insert the row.
72 * @param {number} options.rowIndex Row index at which to insert the row.
73 *
74 * @return {Object} New table state.
75 */
76export function insertRow( state, {
77 section,
78 rowIndex,
79} ) {
80 const cellCount = state[ section ][ 0 ].cells.length;
81
82 return {
83 [ section ]: [
84 ...state[ section ].slice( 0, rowIndex ),
85 {
86 cells: times( cellCount, () => ( {
87 content: '',
88 tag: 'td',
89 } ) ),
90 },
91 ...state[ section ].slice( rowIndex ),
92 ],
93 };
94}
95
96/**
97 * Deletes a row from the table state.
98 *
99 * @param {Object} state Current table state.
100 * @param {string} options.section Section in which to delete the row.
101 * @param {number} options.rowIndex Row index to delete.
102 *
103 * @return {Object} New table state.
104 */
105export function deleteRow( state, {
106 section,
107 rowIndex,
108} ) {
109 return {
110 [ section ]: state[ section ].filter( ( row, index ) => index !== rowIndex ),
111 };
112}
113
114/**
115 * Inserts a column in the table state.
116 *
117 * @param {Object} state Current table state.
118 * @param {string} options.section Section in which to insert the column.
119 * @param {number} options.columnIndex Column index at which to insert the column.
120 *
121 * @return {Object} New table state.
122 */
123export function insertColumn( state, {
124 section,
125 columnIndex,
126} ) {
127 return {
128 [ section ]: state[ section ].map( ( row ) => ( {
129 cells: [
130 ...row.cells.slice( 0, columnIndex ),
131 {
132 content: '',
133 tag: 'td',
134 },
135 ...row.cells.slice( columnIndex ),
136 ],
137 } ) ),
138 };
139}
140
141/**
142 * Deletes a column from the table state.
143 *
144 * @param {Object} state Current table state.
145 * @param {string} options.section Section in which to delete the column.
146 * @param {number} options.columnIndex Column index to delete.
147 *
148 * @return {Object} New table state.
149 */
150export function deleteColumn( state, {
151 section,
152 columnIndex,
153} ) {
154 return {
155 [ section ]: state[ section ].map( ( row ) => ( {
156 cells: row.cells.filter( ( cell, index ) => index !== columnIndex ),
157 } ) ).filter( ( row ) => row.cells.length ),
158 };
159}