React and React Native by Adam Boduch
Author:Adam Boduch [Boduch, Adam]
Language: eng
Format: azw3, epub, pdf
Publisher: Packt Publishing
Published: 2017-03-08T05:00:00+00:00
Now let's look at the container component that controls the state of these inputs:
import React, { Component } from 'react'; import { fromJS } from 'immutable'; import InputsForm from './InputsForm'; // Validates the given "name". It should have a space, // and it should have more than 3 characters. There are // many scenarios not accounted for here, but are easy // to add. function validateName(name) { if (name.search(/ /) === -1) { return 'First and last name, separated with a space'; } else if (name.length < 4) { return 'Less than 4 characters? Srsly?'; } return null; } class InputsFormContainer extends Component { state = { data: fromJS({ // "Name" value and change handler. nameValue: '', // When the name changes, we use "validateName()" // to set "nameValidationState" and // "nameValidationText". nameChange: (e) => { this.data = this.data.merge({ nameValue: e.target.value, nameValidationState: validateName(e.target.value) === null ? 'success' : 'error', nameValidationText: validateName(e.target.value), }); }, // "Password" value and change handler. passwordValue: '', passwordChange: (e) => { this.data = this.data.set( 'passwordValue', e.target.value ); }, }), } // Getter for "Immutable.js" state data... get data() { return this.state.data; } // Setter for "Immutable.js" state data... set data(data) { this.setState({ data }); } render() { return ( <InputsForm {...this.data.toJS()} /> ); } } export default InputsFormContainer;
The event handlers for the inputs are part of the state and get passed to InputsForm as properties. Now let's take a look at some checkboxes and radio buttons. We'll use the <Radio> and the <Checkbox> react-bootstrap components:
import React, { PropTypes } from 'react'; import { Panel, Radio, Checkbox, FormGroup, } from 'react-bootstrap'; const RadioForm = (props) => ( <Panel header={(<h3>Radios & Checkboxes</h3>)}> { /* Renders a group of related radio buttons. Note that each radio needs to have the same "name" property, otherwise, the user will be able to select multiple radios in the same group. The "checked", "disabled", and "onChange" properties all come from the container component. */} <FormGroup> <Radio name="radio" onChange={props.checkboxEnabledChange} checked={props.checkboxEnabled} disabled={!props.radiosEnabled} > Checkbox enabled </Radio> <Radio name="radio" onChange={props.checkboxDisabledChange} checked={!props.checkboxEnabled} disabled={!props.radiosEnabled} > Checkbox disabled </Radio> </FormGroup> { /* Renders a checkbox and uses the same approach as the radios above: setting it's properties from state that's passed in from the container. */} <FormGroup> <Checkbox onChange={props.checkboxChange} checked={props.radiosEnabled} disabled={!props.checkboxEnabled} > Radios enabled </Checkbox> </FormGroup> </Panel> ); RadioForm.propTypes = { checkboxEnabled: PropTypes.bool.isRequired, radiosEnabled: PropTypes.bool.isRequired, checkboxEnabledChange: PropTypes.func.isRequired, checkboxDisabledChange: PropTypes.func.isRequired, checkboxChange: PropTypes.func.isRequired, }; export default RadioForm;
The idea is that the radio buttons toggle the enabled state of the checkbox and the checkbox toggles the enabled state of the radios. Note that, although the two <Radio> elements are in the same <FormGroup>, they need to have the same name property value. Otherwise, you'll be able to select both radios at the same time. Here's what this form looks like:
Download
React and React Native by Adam Boduch.epub
React and React Native by Adam Boduch.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9914)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9795)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8296)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7778)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Windows APT Warfare by Sheng-Hao Ma(6825)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6555)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6423)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Kotlin in Action by Dmitry Jemerov(5062)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4316)
Functional Programming in JavaScript by Mantyla Dan(4038)
Solidity Programming Essentials by Ritesh Modi(3995)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3786)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3730)
The Ultimate iOS Interview Playbook by Avi Tsadok(3705)
