Skip to content

Commit

Permalink
[useSnackBar] Add explicit return type (#36052)
Browse files Browse the repository at this point in the history
Signed-off-by: Zeeshan Tamboli <zeeshan.tamboli@gmail.com>
Co-authored-by: Zeeshan Tamboli <zeeshan.tamboli@gmail.com>
  • Loading branch information
sai6855 and ZeeshanTamboli authored Mar 6, 2023
1 parent 2a937f5 commit d3ce75a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
17 changes: 16 additions & 1 deletion docs/pages/base/api/use-snackbar.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,22 @@
"ref": { "type": { "name": "React.Ref&lt;any&gt", "description": "React.Ref&lt;any&gt" } },
"resumeHideDuration": { "type": { "name": "number", "description": "number" } }
},
"returnValue": {},
"returnValue": {
"getRootProps": {
"type": {
"name": "&lt;TOther extends Record&lt;string, ((event: any) =&gt void) | undefined&gt = {}&gt(externalProps?: TOther) =&gt UseSnackbarRootSlotProps&lt;TOther&gt",
"description": "&lt;TOther extends Record&lt;string, ((event: any) =&gt void) | undefined&gt = {}&gt(externalProps?: TOther) =&gt UseSnackbarRootSlotProps&lt;TOther&gt"
},
"required": true
},
"onClickAway": {
"type": {
"name": "(event: React.SyntheticEvent&lt;any&gt | Event) =&gt void",
"description": "(event: React.SyntheticEvent&lt;any&gt | Event) =&gt void"
},
"required": true
}
},
"name": "useSnackbar",
"filename": "/packages/mui-base/src/useSnackbar/useSnackbar.ts",
"demos": "<ul><li><a href=\"/base/react-snackbar/#hook\">Unstyled Snackbar</a></li></ul>"
Expand Down
5 changes: 4 additions & 1 deletion docs/translations/api-docs/use-snackbar/use-snackbar.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
"open": "If <code>true</code>, the component is shown.",
"resumeHideDuration": "The number of milliseconds to wait before dismissing after user interaction.\nIf <code>autoHideDuration</code> prop isn't specified, it does nothing.\nIf <code>autoHideDuration</code> prop is specified but <code>resumeHideDuration</code> isn't,\nwe default to <code>autoHideDuration / 2</code> ms."
},
"returnValueDescriptions": {}
"returnValueDescriptions": {
"getRootProps": "Resolver for the root slot's props.",
"onClickAway": "Callback fired when a \"click away\" event is detected."
}
}
10 changes: 6 additions & 4 deletions packages/mui-base/src/useSnackbar/useSnackbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { unstable_useEventCallback as useEventCallback } from '@mui/utils';
import {
UseSnackbarParameters,
SnackbarCloseReason,
UseSnackbarRootSlotProps,
UseSnackbarReturnValue,
} from './useSnackbar.types';
import extractEventHandlers from '../utils/extractEventHandlers';

Expand All @@ -18,7 +18,7 @@ import extractEventHandlers from '../utils/extractEventHandlers';
*
* - [useSnackbar API](https://mui.com/base/api/use-snackbar/)
*/
export default function useSnackbar(parameters: UseSnackbarParameters) {
export default function useSnackbar(parameters: UseSnackbarParameters): UseSnackbarReturnValue {
const {
autoHideDuration = null,
disableWindowBlurListener = false,
Expand Down Expand Up @@ -145,9 +145,11 @@ export default function useSnackbar(parameters: UseSnackbarParameters) {
return undefined;
}, [disableWindowBlurListener, handleResume, open]);

const getRootProps = <TOther extends Record<string, React.EventHandler<any> | undefined> = {}>(
const getRootProps: UseSnackbarReturnValue['getRootProps'] = <
TOther extends Parameters<UseSnackbarReturnValue['getRootProps']>[0],
>(
otherHandlers: TOther = {} as TOther,
): UseSnackbarRootSlotProps<TOther> => {
) => {
const propsEventHandlers = extractEventHandlers(parameters) as Partial<UseSnackbarParameters>;
const externalEventHandlers = {
...propsEventHandlers,
Expand Down
15 changes: 15 additions & 0 deletions packages/mui-base/src/useSnackbar/useSnackbar.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,18 @@ export interface UseSnackbarRootSlotOwnProps {
ref?: React.Ref<any>;
role: React.AriaRole;
}

export interface UseSnackbarReturnValue {
/**
* Resolver for the root slot's props.
* @param externalProps props for the root slot
* @returns props that should be spread on the root slot
*/
getRootProps: <TOther extends Record<string, ((event: any) => void) | undefined> = {}>(
externalProps?: TOther,
) => UseSnackbarRootSlotProps<TOther>;
/**
* Callback fired when a "click away" event is detected.
*/
onClickAway: (event: React.SyntheticEvent<any> | Event) => void;
}

0 comments on commit d3ce75a

Please sign in to comment.