ToolJet/frontend/src/Editor/Components/Listview.jsx
Froosty 082be1b63d
Feature/list view border color (#4300)
* Update widgetConfig.js

* Update Listview.jsx

* Update widgetConfig.js

* Update Listview.jsx

* Update frontend/src/Editor/Components/Listview.jsx

Co-authored-by: Kavin Venkatachalam <50441969+kavinvenkatachalam@users.noreply.github.com>

Co-authored-by: Kavin Venkatachalam <50441969+kavinvenkatachalam@users.noreply.github.com>
2022-10-11 09:10:30 +05:30

115 lines
3.9 KiB
JavaScript

import React, { useRef, useState, useEffect } from 'react';
import { SubContainer } from '../SubContainer';
import _ from 'lodash';
export const Listview = function Listview({
id,
component,
width,
height,
containerProps,
removeComponent,
properties,
styles,
fireEvent,
setExposedVariable,
darkMode,
}) {
const fallbackProperties = { height: 100, showBorder: false, data: [] };
const fallbackStyles = { visibility: true, disabledState: false };
const { data, rowHeight, showBorder } = { ...fallbackProperties, ...properties };
const { visibility, disabledState, borderRadius } = { ...fallbackStyles, ...styles };
const backgroundColor =
['#fff', '#ffffffff'].includes(styles.backgroundColor) && darkMode ? '#232E3C' : styles.backgroundColor;
const borderColor = styles.borderColor ?? 'transparent';
const computedStyles = {
backgroundColor,
border: '1px solid',
borderColor,
height,
display: visibility ? 'flex' : 'none',
borderRadius: borderRadius ?? 0,
};
const [selectedRowIndex, setSelectedRowIndex] = useState(undefined);
function onRowClicked(index) {
setSelectedRowIndex(index);
setExposedVariable('selectedRowId', index);
setExposedVariable('selectedRow', childrenData[index]);
fireEvent('onRowClicked');
// eslint-disable-next-line react-hooks/exhaustive-deps
}
const parentRef = useRef(null);
const [childrenData, setChildrenData] = useState({});
useEffect(() => {
setExposedVariable('data', {});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useEffect(() => {
setExposedVariable('data', childrenData);
if (selectedRowIndex != undefined) {
setExposedVariable('selectedRowId', selectedRowIndex);
setExposedVariable('selectedRow', childrenData[selectedRowIndex]);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [childrenData]);
return (
<div
data-disabled={disabledState}
className="jet-listview"
id={id}
ref={parentRef}
onClick={() => containerProps.onComponentClick(id, component)}
style={computedStyles}
data-cy={`draggable-widget-${String(component.name).toLowerCase()}`}
>
<div className="rows w-100">
{(_.isArray(data) ? data : []).map((listItem, index) => (
<div
className={`list-item w-100 ${showBorder ? 'border-bottom' : ''}`}
style={{ position: 'relative', height: `${rowHeight}px`, width: '100%' }}
key={index}
data-cy={`${String(component.name).toLowerCase()}-row-${index}`}
onClick={(event) => {
event.stopPropagation();
onRowClicked(index);
}}
>
<SubContainer
parentComponent={component}
containerCanvasWidth={width}
parent={`${id}`}
parentName={component.name}
{...containerProps}
readOnly={index !== 0}
customResolvables={{ listItem }}
parentRef={parentRef}
removeComponent={removeComponent}
listViewItemOptions={{ index }}
exposedVariables={childrenData[index]}
onOptionChange={function ({ component, optionName, value }) {
setChildrenData((prevData) => {
const changedData = { [component.name]: { [optionName]: value } };
const existingDataAtIndex = prevData[index] ?? {};
const newDataAtIndex = {
...prevData[index],
[component.name]: { ...existingDataAtIndex[component.name], ...changedData[component.name] },
};
const newChildrenData = { ...prevData, [index]: newDataAtIndex };
return { ...prevData, ...newChildrenData };
});
}}
/>
</div>
))}
</div>
</div>
);
};