added missing onClick prop to ToastButton

This commit is contained in:
Sebastian Sdorra
2019-12-16 16:55:03 +01:00
parent f2e19b6c56
commit cdeb70c62c
3 changed files with 39 additions and 4 deletions

View File

@@ -2559,6 +2559,8 @@ exports[`Storyshots Table|Table TextColumn 1`] = `
</table> </table>
`; `;
exports[`Storyshots Toast Click to close 1`] = `null`;
exports[`Storyshots Toast Danger 1`] = `null`; exports[`Storyshots Toast Danger 1`] = `null`;
exports[`Storyshots Toast Info 1`] = `null`; exports[`Storyshots Toast Info 1`] = `null`;

View File

@@ -1,12 +1,13 @@
import React, { FC, useContext } from "react"; import React, { FC, useContext, MouseEvent } from "react";
import { ToastThemeContext, Themeable } from "./themes"; import { ToastThemeContext, Themeable } from "./themes";
import styled from "styled-components"; import styled from "styled-components";
type Props = { type Props = {
icon?: string; icon?: string;
onClick?: () => void;
}; };
const ThemedButton = styled.div.attrs(props => ({ const ThemedButton = styled.button.attrs(props => ({
className: "button" className: "button"
}))<Themeable>` }))<Themeable>`
color: ${props => props.theme.primary}; color: ${props => props.theme.primary};
@@ -25,10 +26,18 @@ const ToastButtonIcon = styled.i`
margin-right: 0.25rem; margin-right: 0.25rem;
`; `;
const ToastButton: FC<Props> = ({ icon, children }) => { const ToastButton: FC<Props> = ({ icon, onClick, children }) => {
const theme = useContext(ToastThemeContext); const theme = useContext(ToastThemeContext);
const handleClick = (e: MouseEvent<HTMLButtonElement>) => {
if (onClick) {
e.preventDefault();
onClick();
}
};
return ( return (
<ThemedButton theme={theme}> <ThemedButton theme={theme} onClick={handleClick}>
{icon && <ToastButtonIcon className={`fas fa-fw fa-${icon}`} />} {children} {icon && <ToastButtonIcon className={`fas fa-fw fa-${icon}`} />} {children}
</ThemedButton> </ThemedButton>
); );

View File

@@ -26,7 +26,31 @@ const Animator = () => {
); );
}; };
const Closeable = () => {
const [show, setShow] = useState(true);
const hide = () => {
setShow(false);
};
if (!show) {
return null;
}
return (
<Toast type="success" title="Awesome feature">
<p>Close the message with a click</p>
<ToastButtons>
<ToastButton icon="times" onClick={hide}>
Click to close
</ToastButton>
</ToastButtons>
</Toast>
);
};
toastStories.add("Open/Close", () => <Animator />); toastStories.add("Open/Close", () => <Animator />);
toastStories.add("Click to close", () => <Closeable />);
types.forEach(type => { types.forEach(type => {
toastStories.add(type.charAt(0).toUpperCase() + type.slice(1), () => ( toastStories.add(type.charAt(0).toUpperCase() + type.slice(1), () => (