2017-04-18 12 views
0

私は私の反応-Reduxのアプリで反応し、DatePickerのモジュールを使用していると私はこのようなReduxの形式との互換性を作っ:デフォルト値に反応し、DatePickerの

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 を選択し、これを試してみてください?瞬間(小道具の入力値、 '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

あなたがmapStateToProps段階で初期フォームの値を指定することができ、日付

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

をフォーマットする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/

関連する問題