Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion react/providers/Alert/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const initialVariants = [{
square: false,
standard: false,
outlined: false,
noClickAway: false,
noTimeOut: false,
close: true
}]

Expand All @@ -23,7 +25,7 @@ const Component = ({ variant }) => {

return (
<Button
label="show alert"
label="Show alert"
onClick={() =>
showAlert({
title: variant.title ? 'Alert title' : undefined,
Expand All @@ -42,6 +44,8 @@ const Component = ({ variant }) => {
: variant.largeIcon
? <Icon icon={DeviceLaptopIcon} size={32} />
: undefined,
noClickAway: variant.noClickAway,
noTimeOut: variant.noTimeOut,
onClose: variant.close ? () => {} : undefined
})
}
Expand Down
13 changes: 13 additions & 0 deletions react/providers/Alert/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const handleClose = (state, setState) => (event, reason) => {
const { noClickAway, noTimeOut } = state

if (reason === 'clickaway' && noClickAway) {
return
}

if (reason === 'timeout' && noTimeOut) {
return
}

return setState({ ...state, open: false })
}
17 changes: 12 additions & 5 deletions react/providers/Alert/index.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { createContext, useState, useContext, useMemo } from 'react'

import { handleClose } from './helpers'
import Alert from '../../Alert'
import AlertTitle from '../../AlertTitle'
import Snackbar from '../../Snackbar'
Expand Down Expand Up @@ -32,13 +33,19 @@ const defaultState = {
duration: null,
open: false
}
const handleClose = (state, setState) => () => {
return setState({ ...state, open: false })
}

const AlertProvider = ({ children }) => {
const [state, setState] = useState(defaultState)
const { open, message, title, duration, ...alertProps } = state
// noTimeOut and noClickAway are destructured to not being passed to the DOM through ...alertProps
const {
open,
message,
title,
duration,
noTimeOut, // eslint-disable-line no-unused-vars
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👮 🚨

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment just above to explain that

noClickAway, // eslint-disable-line no-unused-vars
...alertProps
} = state

const value = useMemo(
() => ({
Expand All @@ -63,7 +70,7 @@ const AlertProvider = ({ children }) => {
<Alert
variant="filled"
elevation={6}
onClose={handleClose(state, setState)}
onClose={ev => handleClose(state, setState)(ev, 'click')}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why click? if it's an arbitrary value it can also be undefined or null

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, because we use "clickaway" and "timeout", I think "click" is also explicit

{...alertProps}
>
{title && <AlertTitle>{title}</AlertTitle>}
Expand Down
Loading