Skip to content

Commit

Permalink
feat: add initialDate attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
arfedulov committed Jul 8, 2018
1 parent 40dd5df commit 23e8008
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ moment.locale('ru')
| ``inline`` | {bool} If ``true`` inline picker displayed. Default: ``false`` |
| ``startMode`` | {string} Display mode to start. One of ['year', 'month', 'day']. Default: ``day`` |
| ``closable`` | {bool} If true, popup closes after selecting a date |
| ``initialDate`` | {string|moment|Date} Date to display initially when no date is selected |

### TimeInput

Expand All @@ -145,6 +146,7 @@ moment.locale('ru')
| ``inline`` | {bool} If ``true`` inline picker displayed. Default: ``false`` |
| ``startMode`` | {string} Display mode to start. One of ['year', 'month', 'day']. Default: ``day`` |
| ``closable`` | {bool} If true, popup closes after selecting a date-time |
| ``initialDate`` | {string|moment|Date} Date to display initially when no date is selected |

### DatesRangeInput

Expand All @@ -155,6 +157,7 @@ moment.locale('ru')
| ``popupPosition``| {string} One of ['top left', 'top right', 'bottom left', 'bottom right', 'right center', 'left center', 'top center', 'bottom center']. Default: ``top left``|
| ``inline`` | {bool} If ``true`` inline picker displayed. Default: ``false`` |
| ``closable`` | {bool} If true, popup closes after selecting a dates range |
| ``initialDate`` | {string|moment|Date} Open a calendar on this date |

### YearInput

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "semantic-ui-calendar-react",
"version": "0.6.0",
"version": "0.7.0",
"description": "date/time picker built from semantic-ui elements",
"main": "dist/index.js",
"scripts": {
Expand Down
33 changes: 25 additions & 8 deletions src/containers/withStateInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,17 @@ const getTime = ({hour = '00', minute = '00'}) => {
return `${hour}:${minute}`;
};

const parseDate = (value, format) => {
const date = moment(value, format);
return date.isValid()? date : moment();
const parseDate = (value, format, defaultVal) => {
let date = moment(value, format);
if (date.isValid()) {
return date;
} else if (defaultVal) {
date = moment(defaultVal, format);
if (date.isValid()) {
return date;
}
}
return moment();
};

function withStateInput(WrappedComponent) {
Expand Down Expand Up @@ -67,7 +75,13 @@ function withStateInput(WrappedComponent) {
/* Characters that separate date and time values. */
divider: PropTypes.string,
/* If true, popup closes after selecting a date/time */
closable: PropTypes.bool
closable: PropTypes.bool,
/* Date to display initially when no date is selected */
initialDate: PropTypes.oneOfType([
PropTypes.string,
PropTypes.instanceOf(moment),
PropTypes.instanceOf(Date)
])
}

static defaultProps = {
Expand All @@ -83,11 +97,14 @@ function withStateInput(WrappedComponent) {
const {
value,
dateFormat,
startMode
startMode,
initialDate,
} = props;
const initialDate = value? moment(value, dateFormat) : moment().startOf('month');
const _initialDate = value?
moment(value, dateFormat) : initialDate?
moment(initialDate, dateFormat) : moment().startOf('month');
this.state = {
dateToShow: initialDate, // moment
dateToShow: _initialDate, // moment
month: '', // str
year: '', // str
activeHour: '', // str
Expand Down Expand Up @@ -327,7 +344,7 @@ function withStateInput(WrappedComponent) {
}

render() {
const activeDate = parseDate(this.props.value, this.props.dateFormat);
const activeDate = parseDate(this.props.value, this.props.dateFormat, this.props.initialDate);
const wrapperState = {
...this.state,
activeDate: activeDate,
Expand Down

0 comments on commit 23e8008

Please sign in to comment.