Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs] Polish Snackbar demos #15129

Merged
merged 3 commits into from
Mar 31, 2019
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
10 changes: 3 additions & 7 deletions docs/src/pages/demos/snackbars/ConsecutiveSnackbars.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@ const styles = theme => ({
class ConsecutiveSnackbars extends React.Component {
queue = [];

constructor(props) {
super(props);

this.state = {
open: false,
};
}
state = {
open: false,
};

handleClick = message => () => {
this.queue.push({
Expand Down
12 changes: 4 additions & 8 deletions docs/src/pages/demos/snackbars/ConsecutiveSnackbars.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,9 @@ export interface State {
class ConsecutiveSnackbars extends React.Component<Props, State> {
queue: SnackbarMessage[] = [];

constructor(props: Props) {
super(props);

this.state = {
open: false,
};
}
state: State = {
open: false,
};

handleClick = (message: string) => () => {
this.queue.push({
Expand Down Expand Up @@ -74,7 +70,7 @@ class ConsecutiveSnackbars extends React.Component<Props, State> {

render() {
const { classes } = this.props;
const { messageInfo = {} as SnackbarMessage } = this.state;
const { messageInfo = {} as Partial<SnackbarMessage> } = this.state;
Copy link
Member Author

Choose a reason for hiding this comment

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

This was an unsafe cast. It allowed messageInfo.message.length while it might throw at runtime if the queue is empty.


return (
<div>
Expand Down
10 changes: 3 additions & 7 deletions docs/src/pages/demos/snackbars/DirectionSnackbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@ function TransitionDown(props) {
}

class DirectionSnackbar extends React.Component {
constructor() {
super();

this.state = {
open: false,
};
}
state = {
open: false,
};

handleClick = Transition => () => {
this.setState({ open: true, Transition });
Expand Down
12 changes: 4 additions & 8 deletions docs/src/pages/demos/snackbars/DirectionSnackbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,10 @@ export interface State {
Transition?: React.ComponentType<TransitionProps>;
}

class DirectionSnackbar extends React.Component<void, State> {
constructor() {
super();

this.state = {
open: false,
};
}
class DirectionSnackbar extends React.Component<{}, State> {
state: State = {
Copy link
Member Author

Choose a reason for hiding this comment

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

The generic argument in React.Component is required for correct setState type while the explicit annotation in the class field is required for correct types in this.state. Otherwise TypeScript will infer the apparent type ({ open: boolean }) which means this.state.Transition is access of a not existing key.

open: false,
};

handleClick = (Transition: React.ComponentType<TransitionProps>) => () => {
this.setState({ open: true, Transition });
Expand Down
8 changes: 4 additions & 4 deletions docs/src/pages/demos/snackbars/IntegrationNotistack.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import Button from '@material-ui/core/Button';
import { SnackbarProvider, withSnackbar } from 'notistack';

class App extends React.Component {
static propTypes = {
enqueueSnackbar: PropTypes.func.isRequired,
};

handleClick = () => {
this.props.enqueueSnackbar('I love snacks.');
};
Expand All @@ -27,6 +23,10 @@ class App extends React.Component {
}
}

App.propTypes = {
enqueueSnackbar: PropTypes.func.isRequired,
};

const MyApp = withSnackbar(App);

function IntegrationNotistack() {
Expand Down
8 changes: 4 additions & 4 deletions docs/src/pages/demos/snackbars/IntegrationNotistack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import Button from '@material-ui/core/Button';
import { SnackbarProvider, VariantType, withSnackbar, withSnackbarProps } from 'notistack';

class App extends React.Component<withSnackbarProps> {
static propTypes = {
enqueueSnackbar: PropTypes.func.isRequired,
};

handleClick = () => {
this.props.enqueueSnackbar('I love snacks.');
};
Expand All @@ -27,6 +23,10 @@ class App extends React.Component<withSnackbarProps> {
}
}

(App as React.ComponentClass<withSnackbarProps>).propTypes = {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is quite ugly but it's the pattern we use throughout the codebase. Allows separation of interface and implementation and doesn't block readability (usually what we're interested in first is render).

enqueueSnackbar: PropTypes.func.isRequired,
};

const MyApp = withSnackbar(App);

function IntegrationNotistack() {
Expand Down