Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be63d25
commit 6074f21
Showing
17 changed files
with
674 additions
and
8 deletions.
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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import DayPickerInput from 'react-day-picker/DayPickerInput'; | ||
import moment from 'moment'; | ||
import MomentLocaleUtils, {formatDate, parseDate} from 'react-day-picker/moment'; | ||
import 'react-day-picker/lib/style.css'; | ||
import Input from 'components/Input'; | ||
import {instance} from '../translate'; | ||
import {setInputValue} from '../util'; | ||
import './DateInput.scss'; | ||
|
||
const WrappedInput = React.forwardRef((props, ref) => { | ||
const onChange = (value, event) => props.onChange(event); | ||
const validateInput = value => moment(value).isValid(); | ||
|
||
return <Input {...props} ref={ref} pattern={validateInput} onChange={onChange}/>; | ||
}); | ||
|
||
class DateInput extends React.Component { | ||
static propTypes = { | ||
autoCorrect: PropTypes.bool, | ||
forwardedRef: PropTypes.oneOfType([ | ||
PropTypes.func, | ||
PropTypes.object, | ||
]), | ||
onDayChange: PropTypes.func, | ||
} | ||
|
||
constructor(props) { | ||
super(props); | ||
this.inputRef = this.props.forwardedRef || React.createRef(); | ||
this.state = { | ||
hasError: false, | ||
isInvalid: false, | ||
value: this.props.value, | ||
}; | ||
} | ||
|
||
handleBlur = event => { | ||
const {autoCorrect, onBlur} = this.props; | ||
const {value} = this.state; | ||
|
||
if (autoCorrect && this.state.isInvalid) { | ||
this.setState({hasError: true}); | ||
|
||
setTimeout(() => { | ||
const {dayPickerProps, format, formatDate} = this.inputRef.current.props; | ||
setInputValue(event.target, formatDate(value, format, dayPickerProps.locale)); | ||
}, 600); | ||
} | ||
|
||
if (typeof onBlur === 'function') { | ||
onBlur(event); | ||
} | ||
} | ||
|
||
handleDayChange = (day, modifiers, input) => { | ||
const {onDayChange} = this.props; | ||
const inputValue = input.getInput().value; | ||
const isInvalid = modifiers.disabled || (!day && inputValue); | ||
|
||
this.setState(state => ({ | ||
hasError: false, | ||
isInvalid, | ||
value: isInvalid ? state.value : day, | ||
}), () => { | ||
onDayChange(day, modifiers, input); | ||
}); | ||
}; | ||
|
||
render() { | ||
const {hasError} = this.state; | ||
|
||
return ( | ||
<DayPickerInput | ||
{...this.props} | ||
ref={this.inputRef} | ||
component={WrappedInput} | ||
format="YYYY-MM-DD" | ||
formatDate={formatDate} | ||
onDayChange={this.handleDayChange} | ||
parseDate={parseDate} | ||
dayPickerProps={{ | ||
...this.props.dayPickerProps, | ||
locale: instance.language, | ||
localeUtils: MomentLocaleUtils, | ||
}} | ||
inputProps={{ | ||
...this.props.inputProps, | ||
className: hasError ? 'shake-animation' : '', | ||
onBlur: this.handleBlur, | ||
}} | ||
/> | ||
); | ||
} | ||
} | ||
|
||
export default React.forwardRef((props, ref) => ( | ||
<DateInput {...props} forwardedRef={ref}/> | ||
)); | ||
|
||
export { | ||
WrappedInput, | ||
}; |
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,49 @@ | ||
/* stylelint-disable selector-class-pattern */ | ||
.DayPickerInput-Overlay { | ||
margin-top: 4px; | ||
text-align: center; | ||
color: var(--text-color); | ||
background-color: var(--input-background-color); | ||
border: 1px solid var(--input-border-color); | ||
border-radius: 4px; | ||
box-shadow: 0 2px 6px 0 rgba(0, 0, 0, 0.18); | ||
} | ||
|
||
.DayPicker-Months { | ||
flex-wrap: unset; | ||
} | ||
|
||
.DayPicker-NavButton, | ||
.DayPicker-Day { | ||
cursor: default; | ||
} | ||
|
||
.DayPicker:not(.DayPicker--interactionDisabled) .DayPicker-Day:not(.DayPicker-Day--disabled):not(.DayPicker-Day--selected):not(.DayPicker-Day--outside):hover { | ||
color: var(--text-color2); | ||
background-color: var(--primary-color); | ||
border-radius: 2px; | ||
} | ||
|
||
.DayPicker-Day--today { | ||
color: var(--secondary-color); | ||
font-weight: normal; | ||
|
||
&:hover:not(.DayPicker-Day--disabled):not(.DayPicker-Day--outside) { | ||
color: var(--text-color2); | ||
} | ||
} | ||
|
||
.DayPicker-Day--selected:not(.DayPicker-Day--disabled):not(.DayPicker-Day--outside) { | ||
color: var(--text-color2); | ||
background-color: var(--primary-color); | ||
|
||
&:hover { | ||
background-color: var(--primary-color); | ||
} | ||
} | ||
|
||
.DayPicker-Day--disabled:not(.DayPicker-Day--today), | ||
.DayPicker-Day--outside:not(.DayPicker-Day--today) { | ||
color: var(--text-color); | ||
opacity: 0.5; | ||
} |
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
Oops, something went wrong.