-
-
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
[DatePicker] Add keyboard support to inline mode #4945
Closed
Closed
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
f553b74
Merge pull request #1 from callemall/master
caesay 1ac8ee1
DropDownMenu now interacts with the keyboard
caesay d611348
Add back wrongly removed method
caesay c81bcc7
Fixed drop arrow styling
caesay 3028b7d
Merge remote-tracking branch 'upstream/master'
caesay 26759fe
Merge remote-tracking branch 'upstream/master' into BL-1292
caesay 34f76b6
Add keyboard support to inline DatePicker
caesay 1dba654
Add prop to DatePicker to enable keyboard
caesay ce4aa8a
Add docs demo for DatePicker keyboard support
caesay ffb5fb2
fix linting errors
caesay 2bf3af4
fix bug when keyboard enabled datepicker loses focus
caesay c6c8c3a
Fix loss of focus issue with select field
caesay d0bd92b
fix linting errors
caesay 7eda1a8
fix object undefined error
caesay 7960bb0
fix DropDownMenu arrow positioning
caesay 1deb0bc
handle enter key when calander open
caesay b3ed601
fix Popover position for datepicker
caesay cbb5257
fix lint error
caesay fa6e725
now opens dropdown on space and down arrow
caesay 563bea8
fix icon button reference
caesay b1048d9
Merge pull request #2 from BluestoneEU/BL-1292
caesay 2c76fe0
merge upstream/master
caesay d03d959
enable keyboard support for controlled datepicker
caesay 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
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ import React, {Component, PropTypes} from 'react'; | |
import {dateTimeFormat, formatIso, isEqualDate} from './dateUtils'; | ||
import DatePickerDialog from './DatePickerDialog'; | ||
import TextField from '../TextField'; | ||
import keycode from 'keycode'; | ||
|
||
class DatePicker extends Component { | ||
static propTypes = { | ||
|
@@ -50,6 +51,10 @@ class DatePicker extends Component { | |
* Disables the DatePicker. | ||
*/ | ||
disabled: PropTypes.bool, | ||
/** | ||
* The error content to display | ||
*/ | ||
errorText: PropTypes.string, | ||
/** | ||
* Used to change the first day of week. It varies from | ||
* Saturday to Monday between different locales. | ||
|
@@ -65,6 +70,14 @@ class DatePicker extends Component { | |
* @returns {any} The formatted date. | ||
*/ | ||
formatDate: PropTypes.func, | ||
/** | ||
* The hint content to display | ||
*/ | ||
hintText: PropTypes.string, | ||
/** | ||
* Tells the datepicker to handle keyboard input. The container must also be set to inline for this to take effect. | ||
*/ | ||
keyboardEnabled: PropTypes.bool, | ||
/** | ||
* Locale used for formatting the `DatePicker` date strings. Other than for 'en-US', you | ||
* must provide a `DateTimeFormat` that supports the chosen `locale`. | ||
|
@@ -150,6 +163,7 @@ class DatePicker extends Component { | |
|
||
state = { | ||
date: undefined, | ||
keyboardActivated: false, | ||
}; | ||
|
||
componentWillMount() { | ||
|
@@ -170,7 +184,7 @@ class DatePicker extends Component { | |
} | ||
|
||
getDate() { | ||
return this.state.date; | ||
return this.state.date instanceof Date ? this.state.date : undefined; | ||
} | ||
|
||
/** | ||
|
@@ -182,6 +196,9 @@ class DatePicker extends Component { | |
* (get the current system date while doing so) | ||
* else set it to the currently selected date | ||
*/ | ||
if (this.shouldHandleKeyboard) | ||
this.refs.input.focus(); | ||
|
||
if (this.state.date !== undefined) { | ||
this.setState({ | ||
dialogDate: this.getDate(), | ||
|
@@ -200,6 +217,12 @@ class DatePicker extends Component { | |
this.openDialog(); | ||
} | ||
|
||
shouldHandleKeyboard = () => { | ||
return this.props.keyboardEnabled && | ||
!this.props.disabled && | ||
this.props.container === 'inline'; | ||
} | ||
|
||
handleAccept = (date) => { | ||
if (!this.isControlled()) { | ||
this.setState({ | ||
|
@@ -211,13 +234,80 @@ class DatePicker extends Component { | |
} | ||
}; | ||
|
||
handleFocus = (event) => { | ||
event.target.blur(); | ||
handleInputFocus = (event) => { | ||
if (this.shouldHandleKeyboard()) { | ||
this.setState({keyboardActivated: true}, this.focus); | ||
} else { | ||
event.target.blur(); | ||
} | ||
|
||
if (this.props.onFocus) { | ||
this.props.onFocus(event); | ||
} | ||
}; | ||
|
||
handleInputBlur = () => { | ||
this.setState({ | ||
keyboardActivated: false, | ||
date: this.state.date instanceof Date ? this.state.date : undefined, | ||
}); | ||
} | ||
|
||
handleKeyDown = (event) => { | ||
if (!this.shouldHandleKeyboard) | ||
return; | ||
|
||
const key = keycode(event); | ||
switch (key) { | ||
case 'tab': | ||
if (this.state.keyboardActivated) | ||
this.setState({keyboardActivated: false}, this.refs.dialogWindow.dismiss); | ||
break; | ||
case 'right': | ||
case 'left': | ||
case 'up': | ||
case 'down': | ||
event.stopPropagation(); | ||
break; | ||
} | ||
} | ||
|
||
handleKeyUp = (event) => { | ||
if (!this.shouldHandleKeyboard) | ||
return; | ||
|
||
const key = keycode(event); | ||
switch (key) { | ||
case 'enter': | ||
if (this.refs.dialogWindow.state.open) { | ||
event.stopPropagation(); | ||
event.preventDefault(); | ||
this.refs.dialogWindow.dismiss(); | ||
} | ||
break; | ||
} | ||
} | ||
|
||
handleInputChange = (event) => { | ||
if (!this.refs.dialogWindow.state.open) { | ||
this.refs.dialogWindow.show(); | ||
} | ||
|
||
const filtered = event.target.value.replace(/[^0-9\-\/]/gi, '').replace('/', '-'); | ||
let dt = undefined; | ||
if (filtered.length === 10) { | ||
// we split this manually as Date.parse is implementation specific | ||
// and also because it doesn't use the browser's timezone. | ||
const parts = filtered.split('-'); | ||
if (parts.length === 3) | ||
dt = new Date(parts[0], parts[1] - 1, parts[2]); // Note: months are 0 based | ||
} | ||
|
||
this.setState({ | ||
date: !dt || isNaN(dt.getTime()) ? filtered : dt, | ||
}); | ||
} | ||
|
||
handleTouchTap = (event) => { | ||
if (this.props.onTouchTap) { | ||
this.props.onTouchTap(event); | ||
|
@@ -265,6 +355,7 @@ class DatePicker extends Component { | |
disableYearSelection, | ||
firstDayOfWeek, | ||
formatDate: formatDateProp, | ||
keyboardEnabled, | ||
locale, | ||
maxDate, | ||
minDate, | ||
|
@@ -282,20 +373,35 @@ class DatePicker extends Component { | |
|
||
const {prepareStyles} = this.context.muiTheme; | ||
const formatDate = formatDateProp || this.formatDate; | ||
const rawDate = this.state.date instanceof Date ? | ||
formatDate(this.state.date) : | ||
this.state.date; | ||
const inputError = rawDate !== undefined && !(this.state.date instanceof Date) ? | ||
'Enter a valid date' : | ||
this.props.errorText; | ||
const hintText = keyboardEnabled && this.state.keyboardActivated ? 'yyyy-mm-dd' : this.props.hintText; | ||
|
||
return ( | ||
<div className={className} style={prepareStyles(Object.assign({}, style))}> | ||
<div ref="root" className={className} style={prepareStyles(Object.assign({}, style))}> | ||
<TextField | ||
{...other} | ||
onFocus={this.handleFocus} | ||
onFocus={this.handleInputFocus} | ||
onBlur={this.handleInputBlur} | ||
onKeyDown={this.handleKeyDown} | ||
onKeyUp={this.handleKeyUp} | ||
onTouchTap={this.handleTouchTap} | ||
tabIndex={this.shouldHandleKeyboard() ? 0 : 1} | ||
onChange={this.handleInputChange} | ||
ref="input" | ||
style={textFieldStyle} | ||
value={this.state.date ? formatDate(this.state.date) : ''} | ||
value={rawDate ? rawDate : ''} | ||
errorText={inputError} | ||
hintText={hintText} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ref. earlier comment on sorting props. |
||
/> | ||
<DatePickerDialog | ||
DateTimeFormat={DateTimeFormat} | ||
autoOk={autoOk} | ||
anchorEl={this.refs.root} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also here. |
||
cancelLabel={cancelLabel} | ||
container={container} | ||
containerStyle={dialogContainerStyle} | ||
|
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
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
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
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.
I have seen other places in the code where props like these are alphabetically sorted. Is that a convention? Should it be done here?
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.
@nicolaiskogheim propType declarations are definitely alphabetical. I don't know about prop assignments, but either way this is just the docs file and the previous examples don't follow this rule either.
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.
Noted, thanks.