It is notoriously difficult to create reusable Form components. That's because there is so much variation in the inputs and what you end up doing with them. Since we will only have a few forms, we are going to make each one separately. If we end up creating lots of forms with identical characteristics, we can refactor them out later.
Go ahead and create Signup and Login directories and populate them with the standard files.
$ mkdir app/components/{Signup,Login}$ touch app/components/Signup/{README.md,index.js,style.css,spec.js} \ app/components/Login/{README.md,index.js,style.css,spec.js}Then we should create the Signup component in our index.js file.
import React from "react"import cssModules from "react-css-modules"import style from "./style.css"import { default as Button } from "../Button"export class Signup extends React.Component { render() { return ( <div className={style.wrapper}> <div className={style.form}> <div className={style.inputGroup}> <input placeholder="Username" className={style.input} type="text" id="signup-username" /> </div> <div className={style.inputGroup}> <input placeholder="Email" className={style.input} type="text" id="signup-email" /> </div> <div className={style.inputGroup}> <input placeholder="Password" className={style.input} type="password" id="signup-password" /> </div> <div className={style.inputGroup}> <input placeholder="Verify Password" className={style.input} type="password" id="signup-verify-password" /> </div> <Button style={{ width: "100%" }} type="primary"> Submit </Button> </div> </div> ) }}export default cssModules(Signup, style)There are many ways to style inputs. If you have a preference for another input style, by all means, use that. For now, we're just going to implement some basic styling to make our form more usable. Within style.css, add the following:
.wrapper { display: flex; flex-flow: column nowrap; justify-content: center; padding-bottom: 2rem; width: 400px;}.form { display: flex; flex-direction: column; justify-content: center; padding: 2rem; width: 400px;}.input { padding: 1rem 1rem; border-radius: 3px; border: 1px solid #ccc; font-size: 1.1em; outline: none;}.inputGroup { display: flex; flex-flow: column nowrap; padding: 10px 0;}To get this to render, let's quickly change our Home component to the code below.
import React from "react"import cssModules from "react-css-modules"import style from "./style.css"import { default as Signup } from "../Signup"export class Home extends React.Component { render() { return ( <div> <Signup /> </div> ) }}export default cssModules(Home, style)We should also create the Login, which will be almost identical but with fewer fields.
import React from "react"import cssModules from "react-css-modules"import style from "./style.css"import { default as Button } from "../Button"export class Login extends React.Component { render() { return ( <div className={style.wrapper}> <div className={style.form}> <div className={style.inputGroup}> <input placeholder="Email" className={style.input} type="text" id="signup-email" /> </div> <div className={style.inputGroup}> <input placeholder="Password" className={style.input} type="password" id="signup-password" /> </div> <Button style={{ width: "100%" }} type="primary"> Submit </Button> </div> </div> ) }}export default cssModules(Login, style)Also add the same styles to the style.css file. At some point in the future, we can write code that is more DRY with shared CSS, but for now, let's do it the easy way and just copy-paste.
We will eventually want to submit our form, so let's create a submit function that we will attach to our button.
In each of our forms, add a function and update our submit button to handle a click event.
...export class Signup extends React.Component { constructor(props) { super(props) this.submit = this.submit.bind(this) } submit(e) { console.log("Submit button clicked") } render() { return ( <div className={style.wrapper}> ... <Button onClick={this.submit} style={{ width: "100%" }} type="primary"> Submit </Button> </div> ) }}...Add the same submit function and onClick event to our Login component. Be sure to bind the submit function in the constructor as well.
Note: Whenever you write a function that needs access to the this context, you need to bind it--usually in the constructor. For example, a component that simply returns some jsx does not need to be bound, while a function that takes in a value from an event triggered by another element does need to be bound.
Now when click on the button, we see a log in our console that says "Submit button clicked". We will eventually use this button to submit the form, but for now, we are just going to use it to write our tests.
There's not a lot that we need to worry about as far as unit tests go at this point. We need to make sure that 1. The form renders, 2. The form has a submit function, and 3. The submit button calls the submit function.
Remember, since our components are just classes, we can instantiate them by simply calling new <Component> and immediately have access to all their functions to test.
import React from "react"import expect from "expect"import { shallow } from "enzyme"import { Signup } from "./"describe("<Signup />", () => { it("should render", () => { const renderedComponent = shallow(<Signup />) expect(renderedComponent.is("div")).toEqual(true) }) it("should have a submit function", () => { const component = new Signup() expect(component.submit).toExist() }) // TODO need update // it('should call submit function when button is clicked', () => { // const renderedComponent = shallow( // <Signup /> // ) // const spy = expect.spyOn(renderedComponent.instance(), 'submit') // renderedComponent.find('button').simulate('click') // expect(spy).toHaveBeenCalled() // })})The first new test is really simple. Since our components are just JavaScript classes, we can don't actually have to render them to test certain parts. We're checking to make sure there is a submit function, and all we need to do to run this test is instantiate the class using new, then checking that the submit function exists on that class. This is just plain-old JavaScript.
You probably noticed the use of a spy in the last test. It would behoove you to learn more about spies for the purpose of writing better tests. A good place to start is the expect documentation to see a few use cases.
Now that we have our form in place, we need to submit it. We will cover that in the next lesson.
The last thing we should do is add some basic styling to our Home as well as our Login component in the event the user already has an account. We're also going to add an image that we can absolutely position behind our input.
...import { default as Signup } from "../Signup"import { default as Login } from "../Login"export class Home extends React.Component { constructor(props) { super(props) this.state = { formState: "signup" } } render() { return ( <div className={style.leader}> <h1 className={style.title}>Phoenix Chat</h1> {this.state.formState === "signup" ? <Signup /> : null} {this.state.formState === "login" ? <Login /> : null} <img role="presentation" className={style.circles} src="/learnphoenix-static-assets/images/circles-full.png" /> </div> ) }}export default cssModules(Home, style)You may have also noticed the role="presentation", which is a replacement for the alt text for images where it does not make sense to add alt text. In this case, since this image is just going to be a background image, we don't need alt text.
And we should center the content to make it look nicer:
.leader { height: 100vh; display: flex; position: relative; align-items: center; justify-content: center; flex-flow: column nowrap; overflow: hidden; z-index: 100; background: white;}.title { font-size: 1.8em; color: rgb(239, 95, 78);}.changeLink { cursor: pointer; color: #42a5f5;}.circles { position: absolute; right: 0; left: 0; top: 0; margin: auto; height: 650px; opacity: 0.05; z-index: -1; transform: scale(3);}The only tricky piece here is how we're handling the image. We're reducing the opacity and scaling it up by an order of 3. Then we're positioning it in the center and putting the z-index at -1 so it stays behind our form.
It's not good practice to have random z-index numbers all over the place, but in this instance, it doesn't matter. A common way to do this is to have something like 10 built-in z-index values at 100, 200, ..., 1000 and use whichever makes the most sense for the current element.
For now, if you want to access the Login component, you can change the hard-coded formState to "login". In the future, this will be handled programmatically.