-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
48 changed files
with
1,647 additions
and
152 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
87 changes: 87 additions & 0 deletions
87
docs/data/data-grid/selection/CellSelectionFormulaField.js
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,87 @@ | ||
import * as React from 'react'; | ||
import Button from '@mui/material/Button'; | ||
import TextField from '@mui/material/TextField'; | ||
import Stack from '@mui/material/Stack'; | ||
import { DataGridPremium, useGridApiRef } from '@mui/x-data-grid-premium'; | ||
import { useDemoData } from '@mui/x-data-grid-generator'; | ||
|
||
export default function CellSelectionFormulaField() { | ||
const apiRef = useGridApiRef(); | ||
const [value, setValue] = React.useState(''); | ||
const [cellSelectionModel, setCellSelectionModel] = React.useState({}); | ||
const [numberOfSelectedCells, setNumberOfSelectedCells] = React.useState(0); | ||
|
||
const { data } = useDemoData({ | ||
dataSet: 'Commodity', | ||
rowLength: 10, | ||
maxColumns: 6, | ||
}); | ||
|
||
const handleCellSelectionModelChange = React.useCallback((newModel) => { | ||
setCellSelectionModel(newModel); | ||
}, []); | ||
|
||
const handleValueChange = React.useCallback((event) => { | ||
setValue(event.target.value); | ||
}, []); | ||
|
||
const updateSelectedCells = React.useCallback(() => { | ||
const updates = []; | ||
|
||
Object.entries(cellSelectionModel).forEach(([id, fields]) => { | ||
const updatedRow = { ...apiRef.current.getRow(id) }; | ||
|
||
Object.entries(fields).forEach(([field, isSelected]) => { | ||
if (isSelected) { | ||
updatedRow[field] = value; | ||
} | ||
}); | ||
|
||
updates.push(updatedRow); | ||
}); | ||
|
||
apiRef.current.updateRows(updates); | ||
}, [apiRef, cellSelectionModel, value]); | ||
|
||
React.useEffect(() => { | ||
const selectedCells = apiRef.current.getSelectedCellsAsArray(); | ||
setNumberOfSelectedCells(selectedCells.length); | ||
|
||
if (selectedCells.length > 1) { | ||
setValue('(multiple values)'); | ||
} else if (selectedCells.length === 1) { | ||
setValue( | ||
apiRef.current.getCellValue(selectedCells[0].id, selectedCells[0].field), | ||
); | ||
} else { | ||
setValue(''); | ||
} | ||
}, [apiRef, cellSelectionModel]); | ||
|
||
return ( | ||
<div style={{ width: '100%' }}> | ||
<Stack sx={{ mb: 2 }} direction="row" spacing={2}> | ||
<TextField | ||
label="Selected cell value" | ||
disabled={numberOfSelectedCells === 0} | ||
value={value} | ||
onChange={handleValueChange} | ||
fullWidth | ||
/> | ||
<Button disabled={numberOfSelectedCells === 0} onClick={updateSelectedCells}> | ||
Update selected cells | ||
</Button> | ||
</Stack> | ||
<div style={{ height: 400 }}> | ||
<DataGridPremium | ||
apiRef={apiRef} | ||
rowSelection={false} | ||
cellSelectionModel={cellSelectionModel} | ||
onCellSelectionModelChange={handleCellSelectionModelChange} | ||
cellSelection | ||
{...data} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
} |
99 changes: 99 additions & 0 deletions
99
docs/data/data-grid/selection/CellSelectionFormulaField.tsx
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,99 @@ | ||
import * as React from 'react'; | ||
import Button from '@mui/material/Button'; | ||
import TextField from '@mui/material/TextField'; | ||
import Stack from '@mui/material/Stack'; | ||
import { | ||
DataGridPremium, | ||
GridCellSelectionModel, | ||
GridRowModelUpdate, | ||
useGridApiRef, | ||
} from '@mui/x-data-grid-premium'; | ||
import { useDemoData } from '@mui/x-data-grid-generator'; | ||
|
||
export default function CellSelectionFormulaField() { | ||
const apiRef = useGridApiRef(); | ||
const [value, setValue] = React.useState(''); | ||
const [cellSelectionModel, setCellSelectionModel] = | ||
React.useState<GridCellSelectionModel>({}); | ||
const [numberOfSelectedCells, setNumberOfSelectedCells] = React.useState(0); | ||
|
||
const { data } = useDemoData({ | ||
dataSet: 'Commodity', | ||
rowLength: 10, | ||
maxColumns: 6, | ||
}); | ||
|
||
const handleCellSelectionModelChange = React.useCallback( | ||
(newModel: GridCellSelectionModel) => { | ||
setCellSelectionModel(newModel); | ||
}, | ||
[], | ||
); | ||
|
||
const handleValueChange = React.useCallback( | ||
(event: React.ChangeEvent<HTMLInputElement>) => { | ||
setValue(event.target.value); | ||
}, | ||
[], | ||
); | ||
|
||
const updateSelectedCells = React.useCallback(() => { | ||
const updates: GridRowModelUpdate[] = []; | ||
|
||
Object.entries(cellSelectionModel).forEach(([id, fields]) => { | ||
const updatedRow = { ...apiRef.current.getRow(id) }; | ||
|
||
Object.entries(fields).forEach(([field, isSelected]) => { | ||
if (isSelected) { | ||
updatedRow[field] = value; | ||
} | ||
}); | ||
|
||
updates.push(updatedRow); | ||
}); | ||
|
||
apiRef.current.updateRows(updates); | ||
}, [apiRef, cellSelectionModel, value]); | ||
|
||
React.useEffect(() => { | ||
const selectedCells = apiRef.current.getSelectedCellsAsArray(); | ||
setNumberOfSelectedCells(selectedCells.length); | ||
|
||
if (selectedCells.length > 1) { | ||
setValue('(multiple values)'); | ||
} else if (selectedCells.length === 1) { | ||
setValue( | ||
apiRef.current.getCellValue(selectedCells[0].id, selectedCells[0].field), | ||
); | ||
} else { | ||
setValue(''); | ||
} | ||
}, [apiRef, cellSelectionModel]); | ||
|
||
return ( | ||
<div style={{ width: '100%' }}> | ||
<Stack sx={{ mb: 2 }} direction="row" spacing={2}> | ||
<TextField | ||
label="Selected cell value" | ||
disabled={numberOfSelectedCells === 0} | ||
value={value} | ||
onChange={handleValueChange} | ||
fullWidth | ||
/> | ||
<Button disabled={numberOfSelectedCells === 0} onClick={updateSelectedCells}> | ||
Update selected cells | ||
</Button> | ||
</Stack> | ||
<div style={{ height: 400 }}> | ||
<DataGridPremium | ||
apiRef={apiRef} | ||
rowSelection={false} | ||
cellSelectionModel={cellSelectionModel} | ||
onCellSelectionModelChange={handleCellSelectionModelChange} | ||
cellSelection | ||
{...data} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
} |
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,30 @@ | ||
import * as React from 'react'; | ||
import Button from '@mui/material/Button'; | ||
import { DataGridPremium } from '@mui/x-data-grid-premium'; | ||
import { useDemoData } from '@mui/x-data-grid-generator'; | ||
|
||
export default function CellSelectionGrid() { | ||
const [rowSelection, setRowSelection] = React.useState(false); | ||
|
||
const { data } = useDemoData({ | ||
dataSet: 'Commodity', | ||
rowLength: 10, | ||
maxColumns: 6, | ||
}); | ||
|
||
return ( | ||
<div style={{ width: '100%' }}> | ||
<Button sx={{ mb: 2 }} onClick={() => setRowSelection(!rowSelection)}> | ||
Toggle row selection | ||
</Button> | ||
<div style={{ height: 400 }}> | ||
<DataGridPremium | ||
rowSelection={rowSelection} | ||
checkboxSelection={rowSelection} | ||
cellSelection | ||
{...data} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
} |
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,30 @@ | ||
import * as React from 'react'; | ||
import Button from '@mui/material/Button'; | ||
import { DataGridPremium } from '@mui/x-data-grid-premium'; | ||
import { useDemoData } from '@mui/x-data-grid-generator'; | ||
|
||
export default function CellSelectionGrid() { | ||
const [rowSelection, setRowSelection] = React.useState(false); | ||
|
||
const { data } = useDemoData({ | ||
dataSet: 'Commodity', | ||
rowLength: 10, | ||
maxColumns: 6, | ||
}); | ||
|
||
return ( | ||
<div style={{ width: '100%' }}> | ||
<Button sx={{ mb: 2 }} onClick={() => setRowSelection(!rowSelection)}> | ||
Toggle row selection | ||
</Button> | ||
<div style={{ height: 400 }}> | ||
<DataGridPremium | ||
rowSelection={rowSelection} | ||
checkboxSelection={rowSelection} | ||
cellSelection | ||
{...data} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
} |
11 changes: 11 additions & 0 deletions
11
docs/data/data-grid/selection/CellSelectionGrid.tsx.preview
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,11 @@ | ||
<Button sx={{ mb: 2 }} onClick={() => setRowSelection(!rowSelection)}> | ||
Toggle row selection | ||
</Button> | ||
<div style={{ height: 400 }}> | ||
<DataGridPremium | ||
rowSelection={rowSelection} | ||
checkboxSelection={rowSelection} | ||
cellSelection | ||
{...data} | ||
/> | ||
</div> |
50 changes: 50 additions & 0 deletions
50
docs/data/data-grid/selection/CellSelectionRangeStyling.js
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 * as React from 'react'; | ||
import { styled, lighten, darken, alpha } from '@mui/material/styles'; | ||
import { DataGridPremium, gridClasses } from '@mui/x-data-grid-premium'; | ||
import { useDemoData } from '@mui/x-data-grid-generator'; | ||
|
||
const StyledDataGridPremium = styled(DataGridPremium)(({ theme }) => { | ||
const borderColor = | ||
theme.palette.mode === 'light' | ||
? lighten(alpha(theme.palette.divider, 1), 0.88) | ||
: darken(alpha(theme.palette.divider, 1), 0.68); | ||
|
||
const selectedCellBorder = alpha(theme.palette.primary.main, 0.5); | ||
|
||
return { | ||
[`& .${gridClasses.cell}`]: { | ||
border: `1px solid transparent`, | ||
borderRight: `1px solid ${borderColor}`, | ||
borderBottom: `1px solid ${borderColor}`, | ||
}, | ||
[`& .${gridClasses.cell}.Mui-selected`]: { | ||
borderColor: alpha(theme.palette.primary.main, 0.1), | ||
}, | ||
[`& .${gridClasses.cell}.Mui-selected.${gridClasses['cell--rangeTop']}`]: { | ||
borderTopColor: selectedCellBorder, | ||
}, | ||
[`& .${gridClasses.cell}.Mui-selected.${gridClasses['cell--rangeBottom']}`]: { | ||
borderBottomColor: selectedCellBorder, | ||
}, | ||
[`& .${gridClasses.cell}.Mui-selected.${gridClasses['cell--rangeLeft']}`]: { | ||
borderLeftColor: selectedCellBorder, | ||
}, | ||
[`& .${gridClasses.cell}.Mui-selected.${gridClasses['cell--rangeRight']}`]: { | ||
borderRightColor: selectedCellBorder, | ||
}, | ||
}; | ||
}); | ||
|
||
export default function CellSelectionRangeStyling() { | ||
const { data } = useDemoData({ | ||
dataSet: 'Commodity', | ||
rowLength: 10, | ||
maxColumns: 6, | ||
}); | ||
|
||
return ( | ||
<div style={{ height: 400, width: '100%' }}> | ||
<StyledDataGridPremium rowSelection={false} cellSelection {...data} /> | ||
</div> | ||
); | ||
} |
Oops, something went wrong.