-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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); |
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,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); |
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,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; | ||
} | ||
|
||
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); |
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,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); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 tomaterial-ui-pickers
?There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!