ToolJet/frontend/src/Editor/Components/Table.jsx

90 lines
3.2 KiB
React
Raw Normal View History

2021-04-03 05:25:41 +00:00
import React from 'react';
import { resolve, findProp } from '@/_helpers/utils';
import Skeleton from 'react-loading-skeleton';
2021-04-03 05:25:41 +00:00
2021-04-05 07:49:17 +00:00
export const Table = function Table({ id, component, onComponentClick, currentState, onEvent }) {
2021-04-03 05:25:41 +00:00
const backgroundColor = component.definition.styles.backgroundColor.value;
const color = component.definition.styles.textColor.value;
2021-04-05 04:48:58 +00:00
const columns = component.definition.properties.columns.value;
2021-04-06 10:01:05 +00:00
const actions = component.definition.properties.actions || { value: []};
2021-04-03 05:25:41 +00:00
const loadingStateProperty = component.definition.properties.loadingState;
let loadingState = false;
if(loadingStateProperty && currentState) {
loadingState = resolve(loadingStateProperty.value, currentState);
}
2021-04-04 06:26:46 +00:00
console.log('currentState', currentState);
let data = []
if(currentState) {
2021-04-04 17:07:03 +00:00
data = resolve(component.definition.properties.data.value, currentState, []);
2021-04-04 06:26:46 +00:00
console.log('resolved param', data);
}
function findColumnValue(row, column) {
if(column.key) {
return findProp(row, column.key);
} else {
return findProp(row, column.name);
}
}
2021-04-04 06:26:46 +00:00
2021-04-04 17:07:03 +00:00
// Quick fix, need to remove later
data = data ? data : [];
2021-04-03 05:25:41 +00:00
const computedStyles = {
backgroundColor,
color
}
return (
<div class="table-responsive table-bordered" style={{...computedStyles, width: '700px'}} onClick={() => onComponentClick(id, component) }>
<table
class="table table-vcenter table-nowrap">
<thead>
<tr>
2021-04-05 04:48:58 +00:00
{columns.map((column) => <th>{column.name}</th>)}
2021-04-06 10:01:05 +00:00
{actions.value.length > 0 &&
<th>Actions</th>
}
2021-04-03 05:25:41 +00:00
</tr>
</thead>
2021-04-03 05:25:41 +00:00
<tbody>
{data.map((row =>
<tr onClick={(e) => { e.stopPropagation(); onEvent('onRowClicked', { component, data: row }); }}>
{columns.map((column) => <td>{findColumnValue(row, column)}</td>)}
{actions.value.length > 0 &&
<td>
{actions.value.map((action) => (
<button
class="btn btn-sm m-1 btn-light"
onClick={(e) => { e.stopPropagation(); onEvent('onTableActionButtonClicked', { component, data: row, action }); }}
>
{action.buttonText}
</button>
))}
</td>
}
</tr>
))}
2021-04-03 05:25:41 +00:00
</tbody>
2021-04-04 06:26:46 +00:00
{/* <div className="table-footer p-2">
2021-04-03 05:25:41 +00:00
Records 1-10 of 242
2021-04-04 06:26:46 +00:00
</div> */}
2021-04-03 05:25:41 +00:00
</table>
{loadingState &&
<div style={{width: '100%'}} className="p-5">
<Skeleton count={5}/>
</div>
}
2021-04-03 05:25:41 +00:00
</div>
);
};