Home Reference Source

application/metaproperties/date-meta-property.js

import React from 'react';
import DatePicker from 'material-ui/DatePicker';
import shallowCompare from 'react-addons-shallow-compare';
import moment from 'moment';

class DateMetaProperty extends React.Component {
    constructor(props) {
        super(props);
        this.displayName = 'DateMetaProperty';
    }

    shouldComponentUpdate(nextProps, nextState) {
        if (nextProps.value != this.props.value) {
            return true;
        }
        if (nextProps.disabled != this.props.disabled) {
            return true;
        }
        return false;
    }

    rando() {
        const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

        let str = [];
        for (let i=0; i<5; i++) {
            str.push(chars.charAt(Math.floor(Math.random() * chars.length)));
        }

        return str.join('');
    }

    handleChange(e, date) {
        const useDate = new moment(date).utc().format('Y-MM-DD');
        const propName = this.props.name;

        let fakeEvent = {
            target: {
                getAttribute: function() {
                    return propName;
                },
                value: useDate
            }
        };
        this.props.onChange(fakeEvent);
    }

    render() {
        const name = this.props.name ? this.props.name : 'date_' + this.rando();
        let defaultDate;
        if (this.props.value) {
            defaultDate = new moment(this.props.value, 'Y-MM-DD').toDate();
        }

        return (
            <DatePicker
                name={name}
                defaultDate={defaultDate}
                floatingLabelText={this.props.floatingLabelText}
                onChange={this.handleChange.bind(this)}
                autoOk={true}
                firstDayOfWeek={0}
                />
        );
    }
}

export default DateMetaProperty;