-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DataGrid] Implement useControlState hook, and add control state on s…
…electionModel (#1823)
- Loading branch information
Showing
36 changed files
with
496 additions
and
202 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
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from './gridState'; | ||
export * from './useGridApi'; | ||
export * from './useGridControlState'; | ||
export * from './useGridReducer'; | ||
export * from './useGridSelector'; | ||
export * from './useGridState'; |
78 changes: 78 additions & 0 deletions
78
packages/grid/_modules_/grid/hooks/features/core/useGridControlState.ts
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,78 @@ | ||
import React from 'react'; | ||
import { GridApiRef } from '../../../models/api/gridApiRef'; | ||
import { GridControlStateApi } from '../../../models/api/gridControlStateApi'; | ||
import { ControlStateItem } from '../../../models/controlStateItem'; | ||
import { useGridApiMethod } from '../../root/useGridApiMethod'; | ||
|
||
export function useGridControlState(apiRef: GridApiRef) { | ||
const controlStateMapRef = React.useRef<Record<string, ControlStateItem<any>>>({}); | ||
|
||
const updateControlState = React.useCallback((controlStateItem: ControlStateItem<any>) => { | ||
const { stateId, stateSelector, ...others } = controlStateItem; | ||
|
||
controlStateMapRef.current[stateId] = { | ||
...others, | ||
stateId, | ||
stateSelector: !stateSelector ? (state) => state[stateId] : stateSelector, | ||
}; | ||
}, []); | ||
|
||
const applyControlStateConstraint = React.useCallback( | ||
(newState) => { | ||
let shouldUpdate = true; | ||
const updatedStateIds: string[] = []; | ||
const controlStateMap = controlStateMapRef.current!; | ||
|
||
Object.keys(controlStateMap).forEach((stateId) => { | ||
const controlState = controlStateMap[stateId]; | ||
const oldState = controlState.stateSelector(apiRef.current.state); | ||
const newSubState = controlState.stateSelector(newState); | ||
const hasSubStateChanged = oldState !== newSubState; | ||
|
||
if (updatedStateIds.length >= 1 && hasSubStateChanged) { | ||
// Each hook modify its own state and it should not leak | ||
// Events are here to forward to other hooks and apply changes. | ||
// You are trying to update several states in a no isolated way. | ||
throw new Error( | ||
`You're not allowed to update several sub-state in one transaction. You already updated ${updatedStateIds[0]}, therefore, you're not allowed to update ${controlState.stateId} in the same transaction.`, | ||
); | ||
} | ||
|
||
if (hasSubStateChanged) { | ||
if (controlState.propOnChange) { | ||
const newModel = newSubState; | ||
if (controlState.propModel !== newModel) { | ||
controlState.propOnChange(newModel); | ||
} | ||
shouldUpdate = | ||
controlState.propModel === undefined || controlState.propModel === newModel; | ||
} else if (controlState.propModel !== undefined) { | ||
shouldUpdate = oldState !== controlState.propModel; | ||
} | ||
if (shouldUpdate) { | ||
updatedStateIds.push(controlState.stateId); | ||
} | ||
} | ||
}); | ||
|
||
return { | ||
shouldUpdate, | ||
postUpdate: () => { | ||
updatedStateIds.forEach((stateId) => { | ||
if (controlStateMap[stateId].onChangeCallback) { | ||
const model = controlStateMap[stateId].stateSelector(newState); | ||
controlStateMap[stateId].onChangeCallback!(model); | ||
} | ||
}); | ||
}, | ||
}; | ||
}, | ||
[apiRef], | ||
); | ||
|
||
const controlStateApi: GridControlStateApi = { | ||
updateControlState, | ||
applyControlStateConstraint, | ||
}; | ||
useGridApiMethod(apiRef, controlStateApi, 'controlStateApi'); | ||
} |
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
2 changes: 1 addition & 1 deletion
2
packages/grid/_modules_/grid/hooks/features/selection/gridSelectionState.ts
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import { GridRowId } from '../../../models/gridRows'; | ||
|
||
export type GridSelectionState = Record<string, GridRowId>; | ||
export type GridSelectionState = GridRowId[]; |
Oops, something went wrong.