Skip to content

Commit

Permalink
Merge pull request #646 from contember/fix/grid-static-render
Browse files Browse the repository at this point in the history
fix(react-datagrid): pass env as a static context when analyzing cells
  • Loading branch information
matej21 authored Dec 6, 2023
2 parents 36b6b76 + 3a07d9f commit 9360d35
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/react-datagrid/src/grid/createDataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const dummyStateMethods: DataGridStateMethods = {
}

const createInitialState = (props: DataGridProps<{}>, environment: Environment): DataGridState<any> => {
const columns = extractDataGridColumns(props.children)
const columns = extractDataGridColumns(props.children, environment)
return {
columns,
paging: {
Expand Down
7 changes: 4 additions & 3 deletions packages/react-datagrid/src/internal/gridTemplateAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ import { ChildrenAnalyzer, Leaf } from '@contember/react-multipass-rendering'
import type { ReactNode } from 'react'
import { DataGridColumnProps, DataGridColumns } from '../types'
import { DataGridColumn } from '../grid'
import { Environment } from '@contember/react-binding'

class BoxedGridColumnProps {
public constructor(public readonly value: DataGridColumnProps) {}
}

const gridColumnLeaf = new Leaf(node => new BoxedGridColumnProps(node.props), DataGridColumn)

const gridTemplateAnalyzer = new ChildrenAnalyzer<BoxedGridColumnProps>([gridColumnLeaf], {
const gridTemplateAnalyzer = new ChildrenAnalyzer<BoxedGridColumnProps, never, Environment>([gridColumnLeaf], {
ignoreUnhandledNodes: false,
unhandledNodeErrorMessage: `DataGrid: encountered an illegal child node.`,
})

export const extractDataGridColumns = (nodes: ReactNode): DataGridColumns<any> => {
const processed = gridTemplateAnalyzer.processChildren(nodes, undefined)
export const extractDataGridColumns = (nodes: ReactNode, env: Environment): DataGridColumns<any> => {
const processed = gridTemplateAnalyzer.processChildren(nodes, env)

let counter = 0
const suffixes: Record<string, number> = {}
Expand Down
2 changes: 1 addition & 1 deletion packages/react-datagrid/src/internal/useDataGridState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const DATA_GRID_DEFAULT_ITEMS_PER_PAGE = 50
export const useDataGridState = (props: Pick<DataGridProps<{ tile?: unknown }>, 'children' | 'itemsPerPage' | 'entities' | 'dataGridKey' | 'tile'>): [DataGridState<any>, DataGridStateMethods] => {
const dataGridKey = useDataGridKey(props)
const environment = useEnvironment()
const columns = useMemo(() => extractDataGridColumns(props.children), [props.children])
const columns = useMemo(() => extractDataGridColumns(props.children, environment), [environment, props.children])
const [pageState, updatePaging] = useGridPagingState(props.itemsPerPage ?? DATA_GRID_DEFAULT_ITEMS_PER_PAGE, dataGridKey)
const [hiddenColumns, setIsColumnHidden] = useHiddenColumnsState(columns, dataGridKey)
const [orderDirections, setOrderBy] = useOrderBys(columns, updatePaging, dataGridKey)
Expand Down

0 comments on commit 9360d35

Please sign in to comment.