Skip to content

Commit

Permalink
fix(react-datagrid): move DataGridColumnsContext Provider to Controll…
Browse files Browse the repository at this point in the history
…ed DataGrid + remove static render of datagrids
  • Loading branch information
matej21 committed Aug 5, 2024
1 parent 6cc7fb0 commit 958f451
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 64 deletions.
7 changes: 5 additions & 2 deletions packages/react-datagrid/src/grid/createControlledDataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Component } from '@contember/react-binding'
import { ComponentType } from 'react'
import { DataGridColumns, DataGridMethods, DataGridState } from '../types'
import { ControlledDataView, DataViewInfo } from '@contember/react-dataview'
import { DataGridColumnsContext } from '../internal/contexts'

export type ControlledDataGridProps =
& {
Expand All @@ -14,6 +15,8 @@ export type ControlledDataGridProps =
export const createControlledDataGrid = <P extends {}>(Renderer: ComponentType<P & ControlledDataGridProps>) => Component<ControlledDataGridProps & P>(({ state, methods, info, columns, ...props }) => {
const renderer = <Renderer state={state} methods={methods} info={info} columns={columns} {...props as P} />
return (
<ControlledDataView state={state} methods={methods} info={info} children={renderer}/>
<DataGridColumnsContext.Provider value={columns}>
<ControlledDataView state={state} methods={methods} info={info} children={renderer}/>
</DataGridColumnsContext.Provider>
)
})
}, () => null)
65 changes: 3 additions & 62 deletions packages/react-datagrid/src/grid/createDataGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { Component, Environment, Filter, QueryLanguage, SugaredQualifiedEntityList } from '@contember/react-binding'
import { Component, SugaredQualifiedEntityList } from '@contember/react-binding'
import { ComponentType, ReactNode } from 'react'
import { DataGridMethods, DataGridState } from '../types'
import { ControlledDataGridProps, createControlledDataGrid } from './createControlledDataGrid'
import { extractDataGridColumns } from '../internal/gridTemplateAnalyzer'
import { useDataGrid } from './useDataGrid'
import { DataGridColumnsContext } from '../internal/contexts'
import { replaceGraphQlLiteral } from '@contember/client'

export type DataGridProps<P extends {}> =
& {
Expand All @@ -22,62 +18,7 @@ export const createDataGrid = <P extends {}>(Renderer: ComponentType<P & Control
return Component<DataGridProps<P>>(props => {
const dataGridProps = useDataGrid(props)
return (
<DataGridColumnsContext.Provider value={dataGridProps.columns}>
<ControlledDataGrid {...dataGridProps} {...(props as unknown as P)} />
</DataGridColumnsContext.Provider>
<ControlledDataGrid {...dataGridProps} {...(props as unknown as P)} />
)
}, (props, environment) => {
const fakeState = createInitialState(props, environment)
const columns = extractDataGridColumns(props.children, environment)
return <ControlledDataGrid state={fakeState} info={dummyInfo} methods={dummyStateMethods} columns={columns} {...(props as unknown as P)} />
})
}

const dummyInfo = { paging: { pagesCount: undefined, totalCount: undefined } }
const dummyStateMethods: DataGridMethods = {

filtering: {
setFilter: () => null,
},
sorting: {
setOrderBy: () => null,
},
paging: {
goToPage: () => null,
setItemsPerPage: () => null,
},
selection: {
setLayout: () => null,
setVisibility: () => null,
},
}

const createInitialState = (props: DataGridProps<{}>, environment: Environment): DataGridState => {

const entities = QueryLanguage.desugarQualifiedEntityList({ entities: props.entities }, environment)
const filter = resolveFilter({ and: [entities.filter ?? {}] })
return {
key: '_',
paging: {
itemsPerPage: props.itemsPerPage ?? 50,
pageIndex: 0,
},
filtering: {
filter: filter,
artifact: {},
filterTypes: {},
},
selection: {
values: {},
layouts: [],
},
sorting: {
orderBy: [],
directions: {},
},
entities: entities,
}
}
const resolveFilter = (input?: Filter): Filter<never> => {
return replaceGraphQlLiteral<unknown>(input) as Filter<never>
}, () => null)
}

0 comments on commit 958f451

Please sign in to comment.