@mui/material#debounce TypeScript Examples
The following examples show how to use
@mui/material#debounce.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SlideFadeTransition.tsx From firecms with MIT License | 4 votes |
SlideFade = React.forwardRef(function SlideFade(props: SlideProps, ref) {
const {
children,
in: inProp,
timeout,
onExitAnimation,
...other
} = props;
const theme: any = useTheme();
const childrenRef = React.useRef<any>(null);
const handleRefIntermediary = useForkRef(children.ref, childrenRef);
const handleRef = useForkRef(handleRefIntermediary, ref);
const normalizedTransitionCallback = (callback: any) => (isAppearing: boolean) => {
if (callback) {
// onEnterXxx and onExitXxx callbacks have a different arguments.length value.
if (isAppearing === undefined) {
callback(childrenRef.current);
} else {
callback(childrenRef.current, isAppearing);
}
}
};
const handleEnter = normalizedTransitionCallback((node: any) => {
setTranslateValue(node);
reflow(node);
});
const handleEntering = normalizedTransitionCallback((node: any) => {
const transitionProps = getTransitionProps(
{ timeout },
{
mode: "enter"
}
);
node.style.webkitTransition = theme.transitions.create("-webkit-transform", {
...transitionProps,
easing: theme.transitions.easing.easeOut
});
node.style.transition = theme.transitions.create("transform", {
...transitionProps,
easing: theme.transitions.easing.easeOut
});
node.style.webkitTransform = "none";
node.style.transform = "none";
node.style.opacity = 1;
});
const handleExit: any = normalizedTransitionCallback((node: any) => {
const transitionProps = getTransitionProps(
{ timeout },
{
mode: "exit"
}
);
node.style.opacity = 0.5;
node.style.webkitTransition = theme.transitions.create(["-webkit-transform", "opacity"], {
...transitionProps,
easing: theme.transitions.easing.sharp
});
node.style.transition = theme.transitions.create(["transform", "opacity"], {
...transitionProps,
easing: theme.transitions.easing.sharp
});
setTranslateValue(node);
});
const handleExited: any = normalizedTransitionCallback((node: any) => {
// No need for transitions when the component is hidden
node.style.webkitTransition = "";
node.style.transition = "";
});
const updatePosition = React.useCallback(() => {
if (childrenRef.current) {
setTranslateValue(childrenRef.current);
}
}, []);
React.useEffect(() => {
// Skip configuration where the position is screen size invariant.
if (inProp) {
return undefined;
}
const handleResize = debounce(() => {
if (childrenRef.current) {
setTranslateValue(childrenRef.current);
}
});
const containerWindow = ownerWindow(childrenRef.current);
containerWindow.addEventListener("resize", handleResize);
return () => {
handleResize.clear();
containerWindow.removeEventListener("resize", handleResize);
};
}, [inProp]);
React.useEffect(() => {
if (!inProp) {
// We need to update the position of the drawer when the direction change and
// when it's hidden.d
updatePosition();
}
}, [inProp, updatePosition]);
return (
<Transition
nodeRef={childrenRef}
onEnter={handleEnter}
onEntering={handleEntering}
onExit={handleExit}
onExited={handleExited}
appear={true}
in={inProp}
timeout={timeout}
{...other}
>
{((state: any, childProps: any) => {
return React.cloneElement(children, {
ref: handleRef,
style: {
visibility: state === "exited" && !inProp ? "hidden" : undefined,
...children.props.style
},
...childProps
});
}) as any}
</Transition>
);
})
Example #2
Source File: DataGrid.tsx From mui-toolpad with MIT License | 4 votes |
DataGridComponent = React.forwardRef(function DataGridComponent(
{
columns: columnsProp,
rows: rowsProp,
rowIdField: rowIdFieldProp,
error: errorProp,
selection,
onSelectionChange,
...props
}: ToolpadDataGridProps,
ref: React.ForwardedRef<HTMLDivElement>,
) {
const nodeRuntime = useNode<ToolpadDataGridProps>();
const handleResize = React.useMemo(
() =>
debounce((params: GridColumnResizeParams) => {
if (!nodeRuntime) {
return;
}
nodeRuntime.updateAppDomConstProp('columns', (columns) =>
columns?.map((column) =>
column.field === params.colDef.field ? { ...column, width: params.width } : column,
),
);
}, 500),
[nodeRuntime],
);
React.useEffect(() => handleResize.clear(), [handleResize]);
const handleColumnOrderChange = React.useMemo(
() =>
debounce((params: GridColumnOrderChangeParams) => {
if (!nodeRuntime) {
return;
}
nodeRuntime.updateAppDomConstProp('columns', (columns) => {
if (!columns) {
return columns;
}
const old = columns.find((colDef) => colDef.field === params.field);
if (!old) {
return columns;
}
const withoutOld = columns.filter((column) => column.field !== params.field);
return [
...withoutOld.slice(0, params.targetIndex),
old,
...withoutOld.slice(params.targetIndex),
];
});
}, 500),
[nodeRuntime],
);
React.useEffect(() => handleColumnOrderChange.clear(), [handleColumnOrderChange]);
const rowsInput = rowsProp || EMPTY_ROWS;
const hasExplicitRowId: boolean = React.useMemo(() => {
const hasRowIdField: boolean = !!(rowIdFieldProp && rowIdFieldProp !== 'id');
const parsedRows = rowsInput;
return parsedRows.length === 0 || hasRowIdField || !!parsedRows[0].id;
}, [rowIdFieldProp, rowsInput]);
const rows: GridRowsProp = React.useMemo(
() => (hasExplicitRowId ? rowsInput : rowsInput.map((row, id) => ({ ...row, id }))),
[hasExplicitRowId, rowsInput],
);
const columnsInitRef = React.useRef(false);
const hasColumnsDefined = columnsProp && columnsProp.length > 0;
React.useEffect(() => {
if (!nodeRuntime || hasColumnsDefined || rows.length <= 0 || columnsInitRef.current) {
return;
}
let inferredColumns = inferColumns(rows);
if (!hasExplicitRowId) {
inferredColumns = inferredColumns.filter((column) => column.field !== 'id');
}
nodeRuntime.updateAppDomConstProp('columns', inferredColumns);
columnsInitRef.current = true;
}, [hasColumnsDefined, rows, nodeRuntime, hasExplicitRowId]);
const getRowId = React.useCallback(
(row: any) => {
return rowIdFieldProp && row[rowIdFieldProp] ? row[rowIdFieldProp] : row.id;
},
[rowIdFieldProp],
);
const onSelectionModelChange = React.useCallback(
(ids) => {
onSelectionChange(ids.length > 0 ? rows.find((row) => row.id === ids[0]) : null);
},
[rows, onSelectionChange],
);
const columns: GridColumns = columnsProp || EMPTY_COLUMNS;
return (
<div ref={ref} style={{ height: 350, width: '100%' }}>
<DataGridPro
components={{ Toolbar: GridToolbar, LoadingOverlay: SkeletonLoadingOverlay }}
onColumnResize={handleResize}
onColumnOrderChange={handleColumnOrderChange}
rows={rows}
columns={columns}
key={rowIdFieldProp}
getRowId={getRowId}
onSelectionModelChange={onSelectionModelChange}
selectionModel={selection ? [selection.id] : []}
error={errorProp}
componentsProps={{
errorOverlay: { message: typeof errorProp === 'string' ? errorProp : errorProp?.message },
}}
{...props}
/>
</div>
);
})