Skip to content

Commit 252b869

Browse files
authored
DataRow styles with context (#1081)
1 parent d11f8fe commit 252b869

File tree

3 files changed

+72
-23
lines changed

3 files changed

+72
-23
lines changed

.changeset/brave-trees-smile.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@ensembleui/react-kitchen-sink": patch
3+
"@ensembleui/react-runtime": patch
4+
---
5+
6+
expose context in DataRow styles

apps/kitchen-sink/src/ensemble/screens/widgets.yaml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,11 @@ View:
317317
key: ${dummyData.id}
318318
template:
319319
DataRow:
320+
styles:
321+
backgroundColor: ${colors.teal['300']}
322+
border: 2px double ${index % 2 === 0 ? colors.primary['100'] :"lightgreen"}
323+
borderRadius: ${ensemble.storage.get("skip")}px
324+
padding: ${getDummyProductsByPaginate.body.products.length * 2}px
320325
children:
321326
- Text:
322327
id: ${dummyData.patient+"Name"}
@@ -417,9 +422,14 @@ View:
417422
key: ${dummyData.id}
418423
template:
419424
DataRow:
425+
onTap:
426+
showToast:
427+
message: heha ${data.dummyData.id} ${index}
420428
styles:
421429
backgroundColor: ${index % 2 === 0 ? "beige" :"lightgreen"}
422-
border: 4px double blue
430+
border: 4px double ${dummyData.id % 2 === 0 ? colors.primary['100'] :colors.primary['500']}
431+
textAlign: ${ensemble.storage.get("skip") === 10 ? "right" :"left"}
432+
lineHeight: ${getDummyProductsByPaginate.body.products.length}px
423433
disableSelection: ${dummyData.id === 2}
424434
item-template:
425435
data: ${Object.values(dummyData)}

packages/runtime/src/widgets/DataGrid/DataGrid.tsx

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ import {
1515
useRegisterBindings,
1616
type EnsembleAction,
1717
unwrapWidget,
18-
evaluateDeep,
18+
useEvaluate,
1919
} from "@ensembleui/react-framework";
2020
import React, {
2121
useCallback,
2222
useState,
2323
useMemo,
2424
useRef,
2525
useEffect,
26+
memo,
2627
} from "react";
2728
import type { ReactEventHandler, ReactElement } from "react";
2829
import {
@@ -156,6 +157,42 @@ const ResizableTitle: React.FC<ResizableProps & ResizableState> = (props) => {
156157
);
157158
};
158159

160+
const CustomRowWithStyles: React.FC<
161+
React.PropsWithChildren<{
162+
"data-index"?: number;
163+
"data-styles"?: EnsembleWidgetStyles;
164+
"data-record"?: object;
165+
[key: string]: unknown;
166+
}>
167+
> = memo(
168+
({
169+
"data-index": index,
170+
"data-styles": rowStyles,
171+
"data-record": record,
172+
children,
173+
...props
174+
}) => {
175+
const memoizedContext = useMemo(
176+
() => ({ ...record, index }),
177+
[record, index],
178+
);
179+
180+
const evaluatedRowStyles = useEvaluate(
181+
rowStyles ? (rowStyles as { [key: string]: unknown }) : undefined,
182+
{
183+
context: memoizedContext,
184+
},
185+
);
186+
187+
return (
188+
<tr {...props} style={{ ...evaluatedRowStyles }}>
189+
{children}
190+
</tr>
191+
);
192+
},
193+
);
194+
CustomRowWithStyles.displayName = "CustomRowWithStyles";
195+
159196
const defaultGridMutatorOptions = {
160197
suppressCallbacks: false,
161198
};
@@ -185,11 +222,6 @@ export const DataGrid: React.FC<GridProps> = (props) => {
185222
const selectionColWidth = props.selectionColWidth || 50;
186223
const containerRef = useRef<HTMLDivElement>(null);
187224
const { namedData } = useTemplateData({ ...itemTemplate });
188-
const components = {
189-
header: {
190-
cell: ResizableTitle,
191-
},
192-
};
193225

194226
// on row tap action
195227
const onTapAction = useEnsembleAction(itemTemplate.template.properties.onTap);
@@ -200,6 +232,15 @@ export const DataGrid: React.FC<GridProps> = (props) => {
200232
[onTapAction?.callback],
201233
);
202234

235+
const components = {
236+
header: {
237+
cell: ResizableTitle,
238+
},
239+
body: {
240+
row: CustomRowWithStyles,
241+
},
242+
};
243+
203244
// on row selected action
204245
const onRowsSelected = useEnsembleAction(props.onRowsSelected);
205246
const onRowsSelectedCallback = useCallback(
@@ -476,22 +517,14 @@ export const DataGrid: React.FC<GridProps> = (props) => {
476517
dataSource={namedData}
477518
key={resolvedWidgetId}
478519
onChange={onChange}
479-
onRow={(record, recordIndex) => {
480-
const rowStyles = itemTemplate.template.properties.styles;
481-
const recordStyles = rowStyles
482-
? evaluateDeep(
483-
rowStyles as {
484-
[key: string]: unknown;
485-
},
486-
defaultScreenContext.model,
487-
{ ...record, index: recordIndex },
488-
)
489-
: {};
490-
return {
491-
onClick: () => onTapActionCallback(record, recordIndex),
492-
style: recordStyles,
493-
};
494-
}}
520+
onRow={(record, recordIndex) => ({
521+
"data-index": recordIndex,
522+
"data-record": record,
523+
"data-styles": itemTemplate.template.properties.styles,
524+
onClick: itemTemplate.template.properties.onTap
525+
? (): unknown => onTapActionCallback(record, recordIndex)
526+
: undefined,
527+
})}
495528
pagination={paginationObject}
496529
rowKey={(data: unknown) => {
497530
const identifier: string = evaluate(

0 commit comments

Comments
 (0)