-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: color table rows if first attribute of collection is color attr…
…ibute (#1226)
- Loading branch information
Showing
6 changed files
with
92 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { colord } from "colord" | ||
import { observer } from "mobx-react-lite" | ||
import React, { useEffect, useRef } from "react" | ||
import { Row } from "react-data-grid" | ||
import { TRenderRowProps } from "./case-table-types" | ||
import { useCollectionContext } from "../../hooks/use-collection-context" | ||
import { useDataSetContext } from "../../hooks/use-data-set-context" | ||
import { IDataSet } from "../../models/data/data-set" | ||
import { parseColorToHex } from "../../utilities/color-utils" | ||
|
||
// have to balance background visibility vs. visibility/accessibility of rendered text | ||
const kAlphaForRowBackground = 0.1 | ||
|
||
function getRowColor(data: IDataSet | undefined, collectionId: string, caseId: string) { | ||
if (!data) return | ||
// check first attribute of this collection or parent collections for a color attribute | ||
const collectionIndex = data?.getCollectionIndex(collectionId) ?? -1 | ||
for (let i = collectionIndex; i >= 0; --i) { | ||
const collection = i < data.collections.length ? data.collections[i] : undefined | ||
const firstAttribute = collection?.attributes[0] ?? data.attributes[0] | ||
if (firstAttribute?.userType === "color") { | ||
const firstValue = data.getStrValue(caseId, firstAttribute.id) | ||
const firstColor = firstValue && parseColorToHex(firstValue, { colorNames: true }) | ||
if (firstColor) { | ||
// reduce alpha for text contrast/visibility | ||
return colord(firstColor).alpha(kAlphaForRowBackground).toHex() | ||
} | ||
} | ||
} | ||
} | ||
|
||
export const CustomRow = observer(function CustomRow(props: TRenderRowProps) { | ||
const data = useDataSetContext() | ||
const collectionId = useCollectionContext() | ||
const rowRef = useRef<HTMLDivElement | null>(null) | ||
const { row: { __id__: caseId } } = props | ||
const rowColor = getRowColor(data, collectionId, caseId) ?? "" | ||
|
||
useEffect(() => { | ||
if (rowRef.current) { | ||
rowRef.current.style.backgroundColor = rowColor | ||
} | ||
}, [rowColor]) | ||
|
||
return <Row ref={rowRef} {...props}></Row> | ||
}) | ||
|
||
export function customRenderRow(key: React.Key, props: TRenderRowProps) { | ||
return <CustomRow key={key} {...props} /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters