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] Add Pickers TypeScript demos #15103

Merged
merged 1 commit into from
Mar 30, 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
44 changes: 44 additions & 0 deletions docs/src/pages/demos/pickers/DateAndTimePickers.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from 'react';
import PropTypes from 'prop-types';
import { createStyles, withStyles, WithStyles, Theme } from '@material-ui/core/styles';
import TextField from '@material-ui/core/TextField';

const styles = (theme: Theme) =>
createStyles({
container: {
display: 'flex',
flexWrap: 'wrap',
},
textField: {
marginLeft: theme.spacing(1),
marginRight: theme.spacing(1),
width: 200,
},
});

export type Props = WithStyles<typeof styles>;

function DateAndTimePickers(props: Props) {
const { classes } = props;

return (
<form className={classes.container} noValidate>
<TextField
id="datetime-local"
label="Next appointment"
type="datetime-local"
defaultValue="2017-05-24T10:30"
className={classes.textField}
InputLabelProps={{
shrink: true,
}}
/>
</form>
);
}

DateAndTimePickers.propTypes = {
classes: PropTypes.object.isRequired,
} as any;

export default withStyles(styles)(DateAndTimePickers);
44 changes: 44 additions & 0 deletions docs/src/pages/demos/pickers/DatePickers.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from 'react';
import PropTypes from 'prop-types';
import { createStyles, withStyles, WithStyles, Theme } from '@material-ui/core/styles';
import TextField from '@material-ui/core/TextField';

const styles = (theme: Theme) =>
createStyles({
container: {
display: 'flex',
flexWrap: 'wrap',
},
textField: {
marginLeft: theme.spacing(1),
marginRight: theme.spacing(1),
width: 200,
},
});

export type Props = WithStyles<typeof styles>;

function DatePickers(props: Props) {
const { classes } = props;

return (
<form className={classes.container} noValidate>
<TextField
id="date"
label="Birthday"
type="date"
defaultValue="2017-05-24"
className={classes.textField}
InputLabelProps={{
shrink: true,
}}
/>
</form>
);
}

DatePickers.propTypes = {
classes: PropTypes.object.isRequired,
} as any;

export default withStyles(styles)(DatePickers);
60 changes: 60 additions & 0 deletions docs/src/pages/demos/pickers/MaterialUIPickers.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import 'date-fns';
import React from 'react';
import PropTypes from 'prop-types';
import Grid from '@material-ui/core/Grid';
import { createStyles, withStyles, WithStyles } from '@material-ui/core/styles';
import DateFnsUtils from '@date-io/date-fns';
import { MuiPickersUtilsProvider, TimePicker, DatePicker } from 'material-ui-pickers';

const styles = createStyles({
grid: {
width: '60%',
},
});

export type Props = WithStyles<typeof styles>;

export interface State {
selectedDate: any;
Copy link
Member

@eps1lon eps1lon Mar 30, 2019

Choose a reason for hiding this comment

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

@sperry94 Why was the any necessary here? Is there maybe some fix we can propose upstream to material-ui-pickers?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@eps1lon Looks like their typing for date is really just an alias for any

material-ui-pickers date typing file:
date.ts

Created issue mui/material-ui-pickers#970

Copy link
Member

Choose a reason for hiding this comment

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

I will keep an eye on it. I think we should use an actual type here. If we use the correct one an update in material-ui-pickers is fine. If not we get notified with a test failure on update.

Copy link
Member

@dmtrKovalenko dmtrKovalenko Apr 1, 2019

Choose a reason for hiding this comment

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

Please fill free to use Date type here.
In pickers we can provide only Moment | Luxon | Date | Dayjs. That’s one of points that we will change in v3.

But you are using date-fns utility, so you will never get the other types in onChange.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@eps1lon agreed. I made PR #15144 to use the Date type in this demo.

@dmtrKovalenko thank you for your help!

}

class MaterialUIPickers extends React.Component<Props, State> {
state = {
// The first commit of Material-UI
selectedDate: new Date('2014-08-18T21:11:54'),
};

handleDateChange = (date: any) => {
this.setState({ selectedDate: date });
};

render() {
const { classes } = this.props;
const { selectedDate } = this.state;

return (
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<Grid container className={classes.grid} justify="space-around">
<DatePicker
margin="normal"
label="Date picker"
value={selectedDate}
onChange={this.handleDateChange}
/>
<TimePicker
margin="normal"
label="Time picker"
value={selectedDate}
onChange={this.handleDateChange}
/>
</Grid>
</MuiPickersUtilsProvider>
);
}
}

(MaterialUIPickers as React.ComponentClass<Props, State>).propTypes = {
classes: PropTypes.object.isRequired,
} as any;

export default withStyles(styles)(MaterialUIPickers);
47 changes: 47 additions & 0 deletions docs/src/pages/demos/pickers/TimePickers.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React from 'react';
import PropTypes from 'prop-types';
import { createStyles, withStyles, WithStyles, Theme } from '@material-ui/core/styles';
import TextField from '@material-ui/core/TextField';

const styles = (theme: Theme) =>
createStyles({
container: {
display: 'flex',
flexWrap: 'wrap',
},
textField: {
marginLeft: theme.spacing(1),
marginRight: theme.spacing(1),
width: 200,
},
});

export type Props = WithStyles<typeof styles>;

function TimePickers(props: Props) {
const { classes } = props;

return (
<form className={classes.container} noValidate>
<TextField
id="time"
label="Alarm clock"
type="time"
defaultValue="07:30"
className={classes.textField}
InputLabelProps={{
shrink: true,
}}
inputProps={{
step: 300, // 5 min
}}
/>
</form>
);
}

TimePickers.propTypes = {
classes: PropTypes.object.isRequired,
} as any;

export default withStyles(styles)(TimePickers);