Skip to content
This repository has been archived by the owner on Oct 27, 2022. It is now read-only.

Fix/issue 42 #46

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion demo/dist/js/bundle.js

Large diffs are not rendered by default.

43 changes: 25 additions & 18 deletions src/DatePicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,33 @@ const VALIDATORS = {
};

function getStateFromProps(value, props) {
const date = moment(value === null ? undefined : value, value ? props.pattern : '', props.locale);
const date = value ? moment(value, props.pattern, props.locale) : null;

return {
date: date.isValid() ? date : moment(undefined, '', props.locale),
value,
date,
value: value || '',
pattern: props.pattern.replace(/ddd/g, '\\d\\d\\d').replace(/[DMY]/g, '0')
};
}

function handleRenderDatepicker(styling, date, isActive, popupShown, onSelect, locale) {
if (!popupShown) {
return null;
}

return (
<div {...styling(['inputEnhancementsPopup', 'inputEnhancementsDatePickerPopup'])}>
<DayPicker
theme={styling(null)}
selectedDays={day => date && DateUtils.isSameDay(date.toDate(), day)}
onDayClick={(e, day, { selected }) => onSelect(selected ? null : moment(day, null, locale))}
locale={locale}
localeUtils={MomentLocaleUtils}
/>
</div>
)
};

export default class DatePicker extends PureComponent {
constructor(props) {
super(props);
Expand All @@ -41,18 +59,7 @@ export default class DatePicker extends PureComponent {
static defaultProps = {
pattern: 'ddd DD/MM/YYYY',
placeholder: moment().format('ddd DD/MM/YYYY'),
onRenderCalendar: (styling, date, isActive, popupShown, onSelect, locale) =>
popupShown && (
<div {...styling(['inputEnhancementsPopup', 'inputEnhancementsDatePickerPopup'])}>
<DayPicker
theme={styling(null)}
selectedDays={day => DateUtils.isSameDay(date.toDate(), day)}
onDayClick={(e, day) => onSelect(moment(day, null, locale))}
locale={locale}
localeUtils={MomentLocaleUtils}
/>
</div>
),
onRenderCalendar: handleRenderDatepicker,
locale: 'en'
};

Expand Down Expand Up @@ -160,14 +167,14 @@ export default class DatePicker extends PureComponent {
}

handleSelect = date => {
const localeMoment = moment(date);
localeMoment.locale(this.props.locale);
const value = localeMoment.format(this.props.pattern);
const value = date ? moment(date).locale(this.props.locale).format(this.props.pattern) : null;

this.setState({
popupShown: false,
isActive: false,
...getStateFromProps(value, this.props)
});

this.props.onChange && this.props.onChange(date);
}

Expand Down
1 change: 0 additions & 1 deletion src/Mask.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export default class Mask extends PureComponent {
constructor(props) {
super(props);


const value = props.value || '';
const [state] = getStateFromProps(value, props);
this.state = {
Expand Down
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var CleanWebpackPlugin = require('clean-webpack-plugin');
var NyanProgressWebpackPlugin = require('nyan-progress-webpack-plugin');

var config = {
port: 3000
devServerPort: 3000
};

try {
Expand Down