-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Convert Date Picker to Functional Component #23880
Merged
srikarparsi
merged 3 commits into
Expensify:main
from
shubham1206agra:datepicker-functional
Aug 2, 2023
Merged
Changes from 2 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 |
---|---|---|
@@ -1,47 +1,37 @@ | ||
import React from 'react'; | ||
import React, {useEffect, useRef} from 'react'; | ||
import moment from 'moment'; | ||
import _ from 'underscore'; | ||
import TextInput from '../TextInput'; | ||
import CONST from '../../CONST'; | ||
import * as Browser from '../../libs/Browser'; | ||
import {propTypes, defaultProps} from './datepickerPropTypes'; | ||
import withWindowDimensions, {windowDimensionsPropTypes} from '../withWindowDimensions'; | ||
import './styles.css'; | ||
|
||
const datePickerPropTypes = { | ||
...propTypes, | ||
...windowDimensionsPropTypes, | ||
}; | ||
function DatePicker({maxDate, minDate, onInputChange, innerRef, label, value, placeholder, errorText, containerStyles, disabled, onBlur}) { | ||
const inputRef = useRef(null); | ||
|
||
class DatePicker extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.setDate = this.setDate.bind(this); | ||
this.showDatepicker = this.showDatepicker.bind(this); | ||
} | ||
|
||
componentDidMount() { | ||
useEffect(() => { | ||
// Adds nice native datepicker on web/desktop. Not possible to set this through props | ||
this.inputRef.setAttribute('type', 'date'); | ||
this.inputRef.setAttribute('max', moment(this.props.maxDate).format(CONST.DATE.MOMENT_FORMAT_STRING)); | ||
this.inputRef.setAttribute('min', moment(this.props.minDate).format(CONST.DATE.MOMENT_FORMAT_STRING)); | ||
this.inputRef.classList.add('expensify-datepicker'); | ||
} | ||
inputRef.current.setAttribute('type', 'date'); | ||
inputRef.current.setAttribute('max', moment(maxDate).format(CONST.DATE.MOMENT_FORMAT_STRING)); | ||
inputRef.current.setAttribute('min', moment(minDate).format(CONST.DATE.MOMENT_FORMAT_STRING)); | ||
inputRef.current.classList.add('expensify-datepicker'); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
/** | ||
* Trigger the `onChange` handler when the user input has a complete date or is cleared | ||
* @param {String} text | ||
*/ | ||
setDate(text) { | ||
const setDate = (text) => { | ||
if (!text) { | ||
this.props.onInputChange(''); | ||
onInputChange(''); | ||
return; | ||
} | ||
|
||
const asMoment = moment(text, true); | ||
if (asMoment.isValid()) { | ||
this.props.onInputChange(asMoment.format(CONST.DATE.MOMENT_FORMAT_STRING)); | ||
onInputChange(asMoment.format(CONST.DATE.MOMENT_FORMAT_STRING)); | ||
} | ||
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. this was previously an empty string |
||
} | ||
|
||
|
@@ -50,50 +40,47 @@ class DatePicker extends React.Component { | |
* On mWeb chrome the user needs to tap on the field again in order to bring the datepicker. But our current styles | ||
* don't make this very obvious. To avoid confusion we open the datepicker when the user focuses the field | ||
*/ | ||
showDatepicker() { | ||
if (!this.inputRef || !Browser.isMobileChrome()) { | ||
const showDatepicker = () => { | ||
if (!inputRef.current || !Browser.isMobileChrome()) { | ||
return; | ||
} | ||
|
||
this.inputRef.click(); | ||
inputRef.current.click(); | ||
} | ||
|
||
render() { | ||
return ( | ||
<TextInput | ||
forceActiveLabel | ||
ref={(el) => { | ||
this.inputRef = el; | ||
return ( | ||
<TextInput | ||
forceActiveLabel | ||
ref={(el) => { | ||
inputRef.current = el; | ||
|
||
if (_.isFunction(this.props.innerRef)) { | ||
this.props.innerRef(el); | ||
} | ||
}} | ||
onFocus={this.showDatepicker} | ||
label={this.props.label} | ||
accessibilityLabel={this.props.label} | ||
accessibilityRole={CONST.ACCESSIBILITY_ROLE.TEXT} | ||
onInputChange={this.setDate} | ||
value={this.props.value} | ||
placeholder={this.props.placeholder} | ||
errorText={this.props.errorText} | ||
containerStyles={this.props.containerStyles} | ||
disabled={this.props.disabled} | ||
onBlur={this.props.onBlur} | ||
/> | ||
); | ||
} | ||
if (_.isFunction(innerRef)) { | ||
innerRef(el); | ||
} | ||
}} | ||
onFocus={showDatepicker} | ||
label={label} | ||
accessibilityLabel={label} | ||
accessibilityRole={CONST.ACCESSIBILITY_ROLE.TEXT} | ||
onInputChange={setDate} | ||
value={value} | ||
placeholder={placeholder} | ||
errorText={errorText} | ||
containerStyles={containerStyles} | ||
disabled={disabled} | ||
onBlur={onBlur} | ||
/> | ||
); | ||
} | ||
|
||
DatePicker.propTypes = datePickerPropTypes; | ||
DatePicker.displayName = 'DatePicker'; | ||
DatePicker.propTypes = propTypes; | ||
DatePicker.defaultProps = defaultProps; | ||
|
||
export default withWindowDimensions( | ||
React.forwardRef((props, ref) => ( | ||
<DatePicker | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...props} | ||
innerRef={ref} | ||
/> | ||
)), | ||
); | ||
export default React.forwardRef((props, ref) => ( | ||
<DatePicker | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...props} | ||
innerRef={ref} | ||
/> | ||
)); |
Oops, something went wrong.
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.
here, maxDate and minDate are not dependencies: https://github.com/Expensify/App/pull/20864/files