2017-04-18 6 views
0

난 내 반응-REDUX 응용 프로그램으로 반응-날짜 선택기 모듈을 사용하고 있는데이 같은 REDUX 형식과 호환 만든 :기본값으로 반응-날짜 선택기

const MyDatePicker = props => (
    <div> 
    <DatePicker 
     {...props.input} 
     dateFormat="DD-MM-YYYY" 
     selected={props.input.value 
     ? moment(props.input.value, 'DD-MM-YYYY') 
     : null} 
     placeholderText={props.placeholder} 
     disabled={props.disabled} 
    /> 
    { 
     props.meta.touched && props.meta.error && 
     <span className="error"> 
     { props.intl.formatMessage({ id: props.meta.error }) } 
     </span> 
    } 
    </div> 
); 

문제는 내가 방법을 알고 해달라고 내 모듈에 기본값을 추가하십시오. 이 기본값은 오늘이어야합니다. 이 문제를 어떻게 처리 할 것인가?

+2

이 시도, = {props.input.value 을 선택? 순간 (props.input.value, 'DD-MM-YYYY') : 순간()} – Ved

+0

멋진 작품입니다. 감사!! – user7334203

+0

그게 좋습니다. 환영. – Ved

답변

1

변화 :

selected={props.input.value 
     ? moment(props.input.value, 'DD-MM-YYYY') 
     : null} 

T0

selected={props.input.value ? moment(props.input.value, 'DD-MM-YYYY') : moment()} 


const MyDatePicker = props => (
    <div> 
    <DatePicker 
     {...props.input} 
     dateFormat="DD-MM-YYYY" 
    selected={props.input.value ? moment(props.input.value, 'DD-MM-YYYY') : moment()} 
     placeholderText={props.placeholder} 
     disabled={props.disabled} 
    /> 
    { 
     props.meta.touched && props.meta.error && 
     <span className="error"> 
     { props.intl.formatMessage({ id: props.meta.error }) } 
     </span> 
    } 
    </div> 
); 
1

당신은

const MyDatePicker = props => { 
    var date = new Date(); 
    var todayDate = moment(date).format('DD-MM-YYYY'); 
    return (
    <div> 
    <DatePicker 
     {...props.input} 
     dateFormat="DD-MM-YYYY" 
     selected={props.input.value 
     ? moment(props.input.value).format('DD-MM-YYYY') 
     : todayDate} 
     placeholderText={props.placeholder} 
     disabled={props.disabled} 
    /> 
    { 
     props.meta.touched && props.meta.error && 
     <span className="error"> 
     { props.intl.formatMessage({ id: props.meta.error }) } 
     </span> 
    } 
    </div> 
); 
} 
1

당신은 mapStateToProps 단계에서 초기 형태의 값을 지정할 수 있습니다 날짜를 포맷 할 moment(dt).format()을 사용해야합니다

const mapStateToProps = state => { 
    return { 
    initialValues: { 
     date: moment() 
    } // Use the `initialValues` property to set your initial data 
    }; 
} 

이 또한 여기에 설명 : http://redux-form.com/6.6.3/examples/initializeFromState/