2015-11-06 44 views
0

如何绑定flux.xml中的输入字段?查看todo的示例应用程序,他们绑定输入框上的onchange事件并将其绑定到onchange方法。对多个字段进行相同的技术似乎很乏味 - 为每个字段设置一个单独的方法和事件处理程序。有没有办法从提交的输入框中获取值?我应该将字段值存储在状态中吗?如何在flux.xml中绑定输入字段react.js

import React from 'react'; 
import registerUser from '../actions/registerUser'; 

class Register extends React.Component { 

    constructor(props) { 
     super(props); 
     this.state = { 
      username:'', 
      password:'', 
      email:'', 
      errorMessage:null 
     }; 
     //this.state = this.getStoreState(); 
    } 


    register = (event, value) => { 

     event.preventDefault(); 
     event.stopPropagation(); 

     console.log(value); 
     let username = this.state.username.trim(); 
     let password = this.state.password.trim(); 
     let email = this.state.email.trim(); 

     if (username && password && email) { 
      this.context.executeAction(registerUser, { 
       username: username, 
       password: password, 
       email: email 
      }); 
     } else { 
      this.state.errorMessage = "missing required fields" 
     } 
     this.setState({text: event.target.value}); 
    }; 

    //todo handle error and success 
    render() { 
     return (
      <div> 
       {this.state.errorMessage} 
       <form className="pure-form pure-form-stacked"> 
        <fieldset> 
         <legend>Register</legend> 
         <input type="email" placeholder="Email" /> 
         <input type="text" placeholder="Username" /> 
         <input type="password" placeholder="Password" /> 
         <button onClick={this.register} type="submit" className="pure-button pure-button-primary">Sign Up</button> 
        </fieldset> 
       </form> 
      </div> 
     ); 
    } 
} 

export default Register; 
+0

你可以绑定的所有字段相同的变化方法,添加一个参数上哪个字段进行更新。然后在set状态下根据该参数设置状态[field]。 – thsorens

回答

0

像这样创建输入组件。

import React from 'react'; 
 

 
import Actions from './../flux/Actions'; 
 
import JInput from './common/jInput'; 
 

 
import BasicStore from './../flux/Basic.Store'; 
 

 
let AppCtrlSty = { 
 
\t height: '100%', 
 
\t padding: '0 10px 0 0' 
 
} 
 

 
let checkBoxSty = { 
 
\t boxSizing: 'border-box', 
 
\t display: 'inline-block', 
 
\t lineHeight: '18px', 
 
\t marginLeft: '2px', 
 
\t outline: 'none', 
 
\t position: 'relative' 
 
}; 
 

 
let radioSty = {color: "blue"} 
 

 
let input3Sty = {color: 'green'}; 
 

 
let inputLabel = {margin: '0 5px'}; 
 

 
let textInput1 = {name: 'text', type: 'text', textValue: '', focus: true}; 
 
let checkInput1 = {name: 'checkbox', type: 'checkbox', style: checkBoxSty}; 
 
let colorInput = {name: 'color', type: 'color'}; 
 
let numberInput = {name: 'number', type: 'number', min: 0, max: 100}; 
 
let rangeInput = {name: 'range', type: 'range', min: 0, max: 100}; 
 

 
let radioInput1 = {name: 'radioGroup', type: 'radio', radioValue: 'set'}; 
 
let radioInput2 = {name: 'radioGroup', type: 'radio', radioValue: 'setkey'}; 
 
let radioInput3 = {name: 'radioGroup', type: 'radio', radioValue: 'key'}; 
 

 
class AppCtrlRender extends React.Component { 
 
    \t render() { 
 
    \t \t let inputData = this.state.data; 
 

 
\t \t textInput1.textValue = inputData.text; 
 
\t \t checkInput1.checkedValue = inputData.checkbox; 
 
\t \t colorInput.colorValue = inputData.color; 
 
\t \t numberInput.numberValue = inputData.number; 
 
\t \t rangeInput.numberValue = inputData.range; 
 

 
\t \t let currentRadioGroupValue = this.state.data.radioGroup; 
 
\t \t radioInput1.radioChecked = (currentRadioGroupValue == radioInput1.radioValue); 
 
\t \t radioInput2.radioChecked = (currentRadioGroupValue == radioInput2.radioValue); 
 
\t \t radioInput3.radioChecked = (currentRadioGroupValue == radioInput3.radioValue); 
 

 
\t \t let selected = inputData.checkbox ? 'true' : 'false'; 
 
\t \t let radioGroupName1 = 'key1'; //must be distinct for each use of JRadioGroup 
 
\t \t let radioValue = inputData.radioGroup; 
 
\t \t return (
 
\t \t \t <div id='AppCtrlSty' style={AppCtrlSty}> 
 
\t \t \t \t React 1.4 Form input<br/><br/> 
 
\t \t \t \t Text: <JInput input={textInput1} handleChange={this.handleValueChange} /><br/><br/> 
 
\t \t \t \t Checkbox: <JInput input={checkInput1} handleChange={this.handleValueChange} /> Value: {selected}<br/><br/> 
 
\t \t \t \t Color: <JInput input={colorInput} handleChange={this.handleValueChange} /> Value: {colorInput.colorValue}<br/><br/> 
 
\t \t \t \t Number: <JInput input={numberInput} handleChange={this.handleValueChange} /> Value: {numberInput.numberValue}<br/><br/> 
 
\t \t \t \t Range: <JInput input={rangeInput} handleChange={this.handleValueChange} /> Value: {rangeInput.numberValue}<br/><br/> 
 

 
\t \t \t \t Radio Input: &nbsp; 
 
\t \t \t \t <JInput input={radioInput1} handleChange={this.handleValueChange} />&nbsp;Set &nbsp; 
 
\t \t \t \t <JInput input={radioInput2} handleChange={this.handleValueChange} />&nbsp;Set/Key &nbsp; 
 
\t \t \t \t <JInput input={radioInput3} handleChange={this.handleValueChange} />&nbsp;Key &nbsp; 
 
\t \t \t \t Value: {radioValue} 
 
\t \t \t </div> 
 
\t \t); 
 
\t } 
 
} 
 

 
function getState() { return {data: BasicStore.getData()}; }; 
 

 
export default class AppCtrl extends AppCtrlRender { 
 
\t constructor() { 
 
\t super(); 
 
\t \t this.state = getState(); 
 
\t } 
 

 
\t componentDidMount() { this.unsubscribe = BasicStore.listen(this.storeDidChange); } 
 
\t componentWillUnmount() { this.unsubscribe(); } 
 

 
\t storeDidChange =() => { this.setState(getState()); } 
 
\t handleValueChange = (name, value) => { Actions.editRecord(name, value); } 
 
}

import React from 'react'; 
 

 
class JInputRender extends React.Component { 
 
    \t render() { 
 
\t \t let inputSty = this.props.input.style ? this.props.input.style : {color: 'red'}; 
 
\t \t let textValue = this.state.textValue; 
 
\t \t let colorValue = this.props.input.colorValue ? this.props.input.colorValue : '#1A3212'; 
 
\t \t let checkedValue = (this.props.input.checkedValue != null) ? this.props.input.checkedValue : false; 
 
\t \t let numberValue = this.props.input.numberValue ? this.props.input.numberValue : 0; 
 
\t \t let radioValue = this.props.input.radioValue ? this.props.input.radioValue : ''; 
 
\t \t let radioChecked = (this.props.input.radioChecked != null) ? this.props.input.radioChecked : false; 
 
\t \t let min = this.props.input.min ? this.props.input.min : 0; 
 
\t \t let max = this.props.input.max ? this.props.input.max : 100; 
 
\t \t let step = this.props.input.step ? this.props.input.step : 1; 
 
\t \t let inputType = this.props.input.type ? this.props.input.type : 'text'; 
 

 
\t \t let returnRadio = (
 
\t \t \t \t <input 
 
\t \t \t \t \t ref="inputRef" 
 
\t \t \t \t \t type={inputType} 
 
\t \t \t \t \t style={inputSty} 
 
\t \t \t \t \t checked={radioChecked} 
 
\t \t \t \t \t value={radioValue} 
 
\t \t \t \t \t onChange={this.handleValueChange} /> 
 
\t \t \t) 
 

 
\t \t let returnChecked = (
 
\t \t \t \t <input 
 
\t \t \t \t \t ref="inputRef" 
 
\t \t \t \t \t type={inputType} 
 
\t \t \t \t \t style={inputSty} 
 
\t \t \t \t \t checked={checkedValue} 
 
\t \t \t \t \t onChange={this.handleCheckedChange} /> 
 
\t \t \t) 
 

 
\t \t let returnColor = (
 
\t \t \t \t <input 
 
\t \t \t \t \t type={inputType} 
 
\t \t \t \t \t ref="inputRef" 
 
\t \t \t \t \t style={inputSty} 
 
\t \t \t \t \t value={colorValue} 
 
\t \t \t \t \t onChange={this.handleValueChange} /> 
 
\t \t \t) 
 

 
\t \t let returnNumber = (
 
\t \t \t \t <input 
 
\t \t \t \t \t type={inputType} 
 
\t \t \t \t \t ref="inputRef" 
 
\t \t \t \t \t style={inputSty} 
 
\t \t \t \t \t value={numberValue} 
 
\t \t \t \t \t min={min} max={max} step={step} 
 
\t \t \t \t \t onChange={this.handleValueChange} /> 
 
\t \t \t) 
 

 
\t \t let returnText = (
 
\t \t \t \t <input 
 
\t \t \t \t \t type={inputType} 
 
\t \t \t \t \t ref="inputRef" 
 
\t \t \t \t \t style={inputSty} 
 
\t \t \t \t \t value={textValue} 
 
\t \t \t \t \t onChange={this.handleTextValueChange} /> 
 
\t \t \t) 
 
\t \t let returnIt = {}; 
 
\t \t switch (inputType) { 
 
\t \t \t case 'checkbox': returnIt = returnChecked; break; 
 
\t \t \t case 'radio': returnIt = returnRadio; break; 
 
\t \t \t case 'color': returnIt = returnColor; break; 
 
\t \t \t case 'number': 
 
\t \t \t case 'range': returnIt = returnNumber; break; 
 
\t \t \t default: returnIt = returnText; break; 
 
\t \t } 
 

 
\t \t return (returnIt); 
 
\t } 
 
} 
 

 
export default class JInput extends JInputRender { 
 
\t constructor() { 
 
\t super(); 
 
\t \t this.state = {textValue: ''}; 
 
\t } 
 

 
\t componentDidMount =() => { 
 
\t \t if (this.props.input.textValue) this.setState({textValue: this.props.input.textValue}); 
 
\t \t if (this.props.input.focus) this.refs.inputRef.focus(); 
 
\t } 
 
\t componentWillReceiveProps = (nextProps) => { 
 
\t \t if (nextProps.input.textValue && (this.state.textValue != nextProps.input.textValue)) 
 
\t \t \t {this.setState({textValue: nextProps.input.textValue});} 
 
\t } 
 

 
\t handleCheckedChange = (event) => { this.props.handleChange(this.props.input.name, event.target.checked); } 
 
\t handleTextValueChange = (event) => { 
 
\t \t let newValue = event.target.value; 
 
\t \t this.setState({textValue: newValue}); 
 
\t \t this.props.handleChange(this.props.input.name, newValue); 
 
\t } 
 
\t handleValueChange = (event) => { this.props.handleChange(this.props.input.name, event.target.value); } 
 
}

相关问题