Skip to content

Commit

Permalink
Merge branch 'master' into Add-track-columnst-prop
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeYvas authored Feb 28, 2024
2 parents 0d89ac6 + e5c4596 commit b502ab0
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@
* [UIPQB-55](https://issues.folio.org/browse/UIPQB-55) Regular expressions are incorrect for contains and starts_with operators
* [UIPQB-70](https://issues.folio.org/browse/UIPQB-70) Array fields support verification
* [UIPQB-71](https://issues.folio.org/browse/UIPQB-71) Allow dropdown menus for array types
* [UIPQB-75](https://issues.folio.org/browse/UIPQB-75) Display grouped fields within a list row
14 changes: 14 additions & 0 deletions src/QueryBuilder/ResultViewer/DynamicTable/DynamicTable.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.DynamicTable {
width: calc(100% + calc(2 * var(--gutter-static-two-thirds)));
margin: calc(-1 * var(--gutter-static-one-third)) calc(-1 * var(--gutter-static-two-thirds));
border-collapse: collapse;
}

.DynamicTable td {
vertical-align: top;
}


.DynamicTable th, .DynamicTable td {
padding: var(--gutter-static-one-third) var(--gutter-static-two-thirds);
}
36 changes: 36 additions & 0 deletions src/QueryBuilder/ResultViewer/DynamicTable/DynamicTable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react';
import PropTypes from 'prop-types';
import css from './DynamicTable.css';

export const DynamicTable = ({ properties, values }) => {
const tableBodyRows = JSON.parse(values);

if (!tableBodyRows.length) return null;

return (
<table className={css.DynamicTable}>
<thead>
<tr>{properties?.map((cell) => (
<th key={cell.property} style={{ width: `${100 / properties.length}%` }}>{cell.labelAlias}</th>
))}
</tr>
</thead>
<tbody>
{tableBodyRows.map((row, index) => (
<tr key={index}>
{properties?.map((cell) => (
<td key={cell.property}>
{row[cell.property]}
</td>
))}
</tr>
))}
</tbody>
</table>
);
};

DynamicTable.propTypes = {
properties: PropTypes.arrayOf(PropTypes.object),
values: PropTypes.string,
};
82 changes: 82 additions & 0 deletions src/QueryBuilder/ResultViewer/DynamicTable/DynamicTable.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import React from 'react';
import { render } from '@testing-library/react';
import { DynamicTable } from './DynamicTable';

describe('DynamicTable component', () => {
const properties = [
{
'name': 'code',
'dataType': {
'dataType': 'stringType',
},
'labelAlias': 'Code',
'property': 'code',
},
{
'name': 'distribution_type',
'dataType': {
'dataType': 'stringType',
},
'labelAlias': 'Distribution type',
'property': 'distributionType',
},
{
'name': 'encumbrance',
'dataType': {
'dataType': 'rangedUUIDType',
},
'labelAlias': 'Encumbrance',
'property': 'encumbrance',
},
{
'name': 'fund_id',
'dataType': {
'dataType': 'rangedUUIDType',
},
'labelAlias': 'Fund ID',
'property': 'fundId',
},
{
'name': 'value',
'dataType': {
'dataType': 'numberType',
},
'labelAlias': 'Value',
'property': 'value',
},
];

const values = '[{"code": "STATE-MONOSER", "value": 100.0, "fundId": "bbd4a5e1-c9f3-44b9-bfdf-d184e04f0ba0", "encumbrance": "eb506834-6c70-4239-8d1a-6414a5b08010", "distributionType": "percentage"}]';

it('render null value if there are no rows', () => {
const { container } = render(<DynamicTable properties={properties} values="[]" />);

expect(container).toBeEmptyDOMElement();
});

it('renders table with correct properties and values', () => {
const { getByText } = render(<DynamicTable properties={properties} values={values} />);

properties.forEach(property => {
const label = getByText(property.labelAlias);

expect(label).toBeInTheDocument();
});

const codeValue = getByText('STATE-MONOSER');

expect(codeValue).toBeInTheDocument();

const encumbranceValue = getByText('eb506834-6c70-4239-8d1a-6414a5b08010');

expect(encumbranceValue).toBeInTheDocument();

const fundIdValue = getByText('bbd4a5e1-c9f3-44b9-bfdf-d184e04f0ba0');

expect(fundIdValue).toBeInTheDocument();

const numberValue = getByText('100');

expect(numberValue).toBeInTheDocument();
});
});
8 changes: 6 additions & 2 deletions src/QueryBuilder/ResultViewer/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FormattedDate } from 'react-intl';
import { DATA_TYPES } from '../../constants/dataTypes';
import { DynamicTable } from './DynamicTable/DynamicTable';

export const getTableMetadata = (entityType) => {
const defaultColumns = entityType?.columns?.map((cell) => ({
Expand All @@ -9,6 +10,7 @@ export const getTableMetadata = (entityType) => {
readOnly: false,
selected: cell.visibleByDefault,
dataType: cell.dataType.dataType,
properties: cell.dataType.itemDataType?.properties,
})) || [];

const columnMapping = defaultColumns?.reduce((acc, { value, label }) => {
Expand All @@ -19,10 +21,12 @@ export const getTableMetadata = (entityType) => {

const defaultVisibleColumns = defaultColumns?.filter(col => col.selected).map(col => col.value) || [];
const formatter = defaultColumns.reduce((formatted, column) => {
const { value, dataType } = column;
const { value, dataType, properties } = column;

formatted[value] = (item) => {
if (dataType === DATA_TYPES.DateType) {
if (properties?.length) {
return <DynamicTable properties={properties} values={item[value]} />;
} else if (dataType === DATA_TYPES.DateType) {
return item[value] ? <FormattedDate value={item[value]} /> : '';
} else if (dataType === DATA_TYPES.ArrayType) {
return item[value]?.join(' | ');
Expand Down

0 comments on commit b502ab0

Please sign in to comment.