DateField.js 2.0 KB
import React,{PropTypes} from 'react';
import ReactDOM from 'react-dom';  
import cx from 'classnames';
import s from './form.scss';
import moment from 'moment';
import {Form,DatePicker} from 'antd';
const FormItem = Form.Item;

class DateField extends React.Component {
    constructor(props) {
        super(props);    
        this.dateChange=this.dateChange.bind(this);
        this.state={
            value:''
        }
    }
    static propTypes = {  
        label:PropTypes.string,
        options:PropTypes.object
    } 
    static defaultProps={ 
    }
    componentDidMount(){ 
        // const {field={}}=this.props;
        // const value=field.value?moment(field.value).format():'';
        // field.onChange(value);
    }
    componentWillReceiveProps(nextProps){
        // const {field={}}=nextProps;
        // const value=field.value?moment(field.value).format():'';
        // if(this.props.field.value!=field.value){
        //     field.onChange(value);
        // } 
    }
    dateChange(value, dateString){
        const {field={}}=this.props; 
        if(value){
            // console.log(moment(value).format());
            field.onChange(moment(value).format());
        }else{
            field.onChange('');
        } 
    }
    render(){    
        const {label,options,name,tabular,field={}}=this.props;
        const tabularStyle=tabular?{width:"100%"}:{};
        // console.log('@@@@@@@@@@@@',field.value)
        const value=field.value?moment(field.value).format():null;
        console.log(value)
        return( 
           <FormItem
                label={label}
                labelCol={{ span: 6 }} 
                wrapperCol={{ span: tabular?24:14 }}
                required={options.is_required}
                validateStatus={null}
                help=""
            > 
              <DatePicker  name={name} style={{display:'inline-block',...tabularStyle}} value={value} placeholder={options.tooltip}  onChange={this.dateChange} ></DatePicker>
            </FormItem>
        )
    }
}



export default DateField;