import Button from '../../button'; import Field from '..'; import Input from '../../input'; import React from 'react'; import ReactDOM from 'react-dom'; import { Provider, connect } from 'react-redux'; import { combineReducers, createStore } from 'redux'; function formReducer(state = { email: 'frankqian@qq.com' }, action) { switch (action.type) { case 'save_fields': return { ...state, ...action.payload, }; default: return state; } } interface IProps { dispatch: (action: any) => void; email?: string; } interface IState { formReducer: { email?: string; }; } class Demo extends React.Component { field: Field; constructor(props) { super(props); this.field = new Field(this, { onChange: (name, value) => { console.log('onChange', name, value); this.field.setValue('newlen', value.length); this.props.dispatch({ type: 'save_fields', payload: { [name]: value, }, }); }, }); } componentWillReceiveProps(nextProps) { this.field.setValues({ email: nextProps.email, newlen: nextProps.email.length, }); } setEmail() { this.props.dispatch({ type: 'save_fields', payload: { email: 'qq@gmail.com', }, }); } render() { const init = this.field.init; const newLen = init('newlen', { initValue: this.props.email.length }); return (
now length is:{newLen.value}

email: {this.props.email}

); } } const ReduxDemo = connect((state: IState) => { return { email: state.formReducer.email, }; })(Demo); const store = createStore( combineReducers({ formReducer, }), ); ReactDOM.render(
, document.getElementById('field-demo-7'), );