-
Notifications
You must be signed in to change notification settings - Fork 0
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
4 changed files
with
159 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import React from "react"; | ||
import Snackbar from "@material-ui/core/Snackbar"; | ||
|
||
const SnackbarContext = React.createContext(); | ||
export const SnackbarProvider = (() => { | ||
const queue = []; | ||
return (props) => { | ||
const [open, setOpen] = React.useState(false); | ||
const [messageInfo, setMessageInfo] = React.useState({}); | ||
const processQueue = () => { | ||
if (queue.length > 0) { | ||
setMessageInfo(queue.shift()); | ||
setOpen(true); | ||
} | ||
}; | ||
const handleClick = (message, autoHideDuration = 3000) => { | ||
queue.push({ | ||
message, | ||
autoHideDuration, | ||
key: new Date().getTime(), | ||
}); | ||
|
||
if (open) { | ||
// immediately begin dismissing current message | ||
// to start showing new one | ||
setOpen(false); | ||
} else { | ||
processQueue(); | ||
} | ||
}; | ||
|
||
const handleClose = (event, reason) => { | ||
console.log(reason); | ||
if (reason === "clickaway") { | ||
return; | ||
} | ||
setOpen(false); | ||
}; | ||
|
||
const handleExited = () => { | ||
processQueue(); | ||
}; | ||
return ( | ||
<SnackbarContext.Provider value={{ addMessage: handleClick }}> | ||
<Snackbar | ||
key={messageInfo.key} | ||
anchorOrigin={{ | ||
vertical: "bottom", | ||
horizontal: "left", | ||
}} | ||
open={open} | ||
autoHideDuration={messageInfo.autoHideDuration} | ||
onClose={handleClose} | ||
onExited={handleExited} | ||
ContentProps={{ | ||
"aria-describedby": "message-id", | ||
}} | ||
message={<span id="message-id">{messageInfo.message}</span>} | ||
/> | ||
|
||
{props.children} | ||
</SnackbarContext.Provider> | ||
); | ||
}; | ||
})(); | ||
|
||
export function useSnackbar() { | ||
return React.useContext(SnackbarContext); | ||
} |
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,19 @@ | ||
```js | ||
import { SnackbarProvider, useSnackbar } from "./Snackbar"; | ||
function DisplaySnackbar() { | ||
const { addMessage } = useSnackbar(); | ||
return ( | ||
<> | ||
<button onClick={() => addMessage("Snackbar 1")}> | ||
Display snackbar 1 | ||
</button> | ||
<button onClick={() => addMessage("Snackbar 2")}> | ||
Display snackbar 2 | ||
</button> | ||
</> | ||
); | ||
} | ||
<SnackbarProvider> | ||
<DisplaySnackbar /> | ||
</SnackbarProvider>; | ||
``` |
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,68 @@ | ||
import React from "react"; | ||
import { render, fireEvent, waitForDomChange } from "react-testing-library"; | ||
import { SnackbarProvider, useSnackbar } from "../Snackbar"; | ||
import sleep from "../sleep"; | ||
test("that it displays for 3 seconds when nothing is added", async () => { | ||
function Comp() { | ||
const { addMessage } = useSnackbar(); | ||
|
||
return ( | ||
<div data-testid="click"> | ||
<button | ||
data-testid="first-message" | ||
onClick={() => addMessage("first-message")} | ||
> | ||
click me | ||
</button> | ||
</div> | ||
); | ||
} | ||
const { getByText, getByTestId, queryByText } = render( | ||
<SnackbarProvider> | ||
<Comp /> | ||
</SnackbarProvider>, | ||
); | ||
fireEvent.click(getByTestId("first-message")); | ||
expect(getByText(/first-message/i)).toBeInTheDocument(); | ||
await sleep(3500); | ||
expect(queryByText(/first-message/i)).toBeNull(); | ||
}); | ||
test("that it displays a snackbar when you do useSnackbar", async () => { | ||
function Comp() { | ||
const { addMessage } = useSnackbar(); | ||
|
||
return ( | ||
<div data-testid="click"> | ||
<button | ||
data-testid="first-message" | ||
onClick={() => addMessage("first-message", 50)} | ||
> | ||
click me | ||
</button> | ||
<button | ||
data-testid="second-message" | ||
onClick={() => addMessage("second-message", 50)} | ||
> | ||
click me | ||
</button> | ||
</div> | ||
); | ||
} | ||
|
||
const { getByText, getByTestId, container, queryByText } = render( | ||
<SnackbarProvider> | ||
<Comp /> | ||
</SnackbarProvider>, | ||
); | ||
fireEvent.click(getByTestId("first-message")); | ||
expect(getByText(/first-message/i)).toBeInTheDocument(); | ||
fireEvent.click(getByTestId("second-message")); | ||
await waitForDomChange(container); | ||
|
||
expect(getByText(/second-message/i)).toBeInTheDocument(); | ||
fireEvent.blur(getByTestId("click")); | ||
debugger; | ||
expect(getByText(/second-message/i)).toBeInTheDocument(); | ||
await sleep(400); | ||
expect(queryByText(/second-message/i)).toBeNull(); | ||
}); |