Categories
Reactstrap

Reactstrap — Input Groups

Reactstrap is a version Bootstrap made for React.

It’s a set of React components that have Boostrap styles.

In this article, we’ll look at how to add input groups, jumbotrons, and layouts with Reactstrap.

Buttons and Dropdowns

We can add buttons and dropdowns as input group addons.

For example, we can write:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import {
  InputGroup,
  InputGroupAddon,
  InputGroupButtonDropdown,
  Input,
  Button,
  DropdownToggle,
  DropdownMenu,
  DropdownItem
} from "reactstrap";

export default function App() {
  const [dropdownOpen, setDropdownOpen] = React.useState(false);
  const [splitButtonOpen, setSplitButtonOpen] = React.useState(false);

  const toggleDropDown = () => setDropdownOpen(!dropdownOpen);

  const toggleSplit = () => setSplitButtonOpen(!splitButtonOpen);

  return (
    <div>
      <InputGroup>
        <InputGroupAddon addonType="prepend">
          <Button>button</Button>
        </InputGroupAddon>
        <Input />
      </InputGroup>
      <br />
      <InputGroup>
        <Input />
        <InputGroupButtonDropdown
          addonType="append"
          isOpen={dropdownOpen}
          toggle={toggleDropDown}
        >
          <DropdownToggle caret>Button Dropdown</DropdownToggle>
          <DropdownMenu>
            <DropdownItem header>Header</DropdownItem>
            <DropdownItem disabled>Action</DropdownItem>
            <DropdownItem>Action</DropdownItem>
            <DropdownItem divider />
            <DropdownItem>Action</DropdownItem>
          </DropdownMenu>
        </InputGroupButtonDropdown>
      </InputGroup>
      <br />
      <InputGroup>
        <InputGroupButtonDropdown
          addonType="prepend"
          isOpen={splitButtonOpen}
          toggle={toggleSplit}
        >
          <Button outline>Button</Button>
          <DropdownToggle split outline />
          <DropdownMenu>
            <DropdownItem header>Header</DropdownItem>
            <DropdownItem disabled>Action</DropdownItem>
            <DropdownItem>Action</DropdownItem>
            <DropdownItem divider />
            <DropdownItem>Action</DropdownItem>
          </DropdownMenu>
        </InputGroupButtonDropdown>
        <Input placeholder="name" />
        <InputGroupAddon addonType="append">
          <Button color="secondary">button</Button>
        </InputGroupAddon>
      </InputGroup>
    </div>
  );
}

We have the InputGroup with the InputGroupAddon component.

We have one with Button s inside and we can also add InputGroupButtonDropdown to add the button dropdown.

DropdownToggle has the dropdown toggle.

We can optionally make a split button with a Button and a DropdownToggle with the split prop.

The style cal be outlined with the outlined prop.

Jumbotron

We can add a jumbotron to display content in a way that gets people’s attention.

It gets attention by being bigger than other content.

To add a jumbotron, we use the Jumbotron component.

For example, we can write:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import { Jumbotron, Button } from "reactstrap";

export default function App() {
  return (
    <div>
      <Jumbotron>
        <h1 className="display-3">Hello, world!</h1>
        <p className="lead">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ut
          lectus tellus. Vestibulum vulputate id ex nec tincidunt. Pellentesque
          eu nunc finibus, bibendum magna sit amet, vulputate nunc.
        </p>
        <hr className="my-2" />
        <p>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ut
          lectus tellus. Vestibulum vulputate id ex nec tincidunt. Pellentesque
          eu nunc finibus, bibendum magna sit amet, vulputate nunc.
        </p>
        <p className="lead">
          <Button color="primary">Learn More</Button>
        </p>
      </Jumbotron>
    </div>
  );
}

We add the Jumbotron component with content inside.

We add class names to add margins.

Fluid Jumbotron

We can make the jumbotron fluid with the fluid prop.

For example, we can write:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import { Jumbotron, Container } from "reactstrap";

export default function App() {
  return (
    <div>
      <Jumbotron fluid>
        <Container fluid>
          <h1 className="display-3">Fluid jumbotron</h1>
          <p className="lead">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ut
            lectus tellus. Vestibulum vulputate id ex nec tincidunt.
            Pellentesque eu nunc finibus, bibendum magna sit amet, vulputate
            nunc.
          </p>
        </Container>
      </Jumbotron>
    </div>
  );
}

We have the Jumbotron and the Container set to fluid so that we can display the jumbotron responsively.

Layout Components

We can add layouts to our app with the Row and Col components.

For instance, we can write:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import { Container, Row, Col } from "reactstrap";

export default function App() {
  return (
    <div>
      <Container>
        <Row>
          <Col>.col</Col>
        </Row>
        <Row>
          <Col>.col</Col>
          <Col>.col</Col>
          <Col>.col</Col>
        </Row>
        <Row>
          <Col xs="3">.col-3</Col>
          <Col xs="auto">.col-auto - variable width content</Col>
          <Col xs="3">.col-3</Col>
        </Row>
        <Row>
          <Col sm={{ size: "auto", offset: 1 }}>.col-sm-auto .offset-sm-1</Col>
          <Col sm={{ size: "auto", offset: 1 }}>.col-sm-auto .offset-sm-1</Col>
        </Row>
      </Container>
    </div>
  );
}

We have the Row and Col components to create the grid.

Col can be sized with breakpoint props like xs to make it display the size we want given that the screen hits the given breakpoint or wider.

The prop takes a string or an object.

If it’s an object, the column size would be in the size property.

offset has the offset by the given distance.

Conclusion

We can add button groups and dropdowns to input groups.

Also, we can layout items with rows, columns, and jumbotrons.

Categories
Reactstrap

Reactstrap — Custom Inputs and Input Groups

Reactstrap is a version Bootstrap made for React.

It’s a set of React components that have Boostrap styles.

In this article, we’ll look at how to add custom inputs and input groups with Reactstrap.

Custom Inputs

We can add custom inputs to our app.

For example, we can write:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import { CustomInput, Form, FormGroup, Label } from "reactstrap";

export default function App() {
  return (
    <div>
      <Form>
        <FormGroup>
          <Label for="exampleCheckbox">Checkboxes</Label>
          <div>
            <CustomInput
              type="checkbox"
              id="exampleCustomCheckbox"
              label="mango"
            />
            <CustomInput
              type="checkbox"
              id="exampleCustomCheckbox2"
              label="orange"
            />
            <CustomInput type="checkbox" label="pear" disabled />
            <CustomInput
              type="checkbox"
              id="checkbox"
              label="checkbox"
              htmlFor="checkbox"
              disabled
            />
          </div>
        </FormGroup>
        <FormGroup>
          <Label for="exampleCheckbox">Radios</Label>
          <div>
            <CustomInput type="radio" name="name" label="foo" />
            <CustomInput type="radio" name="customRadio" label="qux" />
            <CustomInput type="radio" id="name" label="bar" disabled />
            <CustomInput
              type="radio"
              label="baz"
              htmlFor="exampleCustomRadio4_X"
              disabled
            />
          </div>
        </FormGroup>
        <FormGroup>
          <Label for="exampleCheckbox">Switches</Label>
          <div>
            <CustomInput
              type="switch"
              id="switch"
              name="fruit"
              label="banana"
            />
            <CustomInput type="switch" id="grape" name="fruit" label="grape" />
            <CustomInput type="switch" id="apple" label="apple" disabled />
            <CustomInput type="switch" id="orange" label="orange" disabled />
          </div>
        </FormGroup>
        <FormGroup>
          <Label for="exampleCheckbox">Inline</Label>
          <div>
            <CustomInput
              type="checkbox"
              label="An inline custom input"
              inline
            />
            <CustomInput type="checkbox" label="and another one" inline />
          </div>
        </FormGroup>
        <FormGroup>
          <Label for="select">Custom Select</Label>
          <CustomInput type="select" id="select" name="customSelect">
            <option value="">Select</option>
            <option>Value 1</option>
            <option>Value 2</option>
            <option>Value 3</option>
          </CustomInput>
        </FormGroup>
        <FormGroup>
          <Label for="multiselect">Custom Multiple Select</Label>
          <CustomInput
            type="select"
            id="multiselect"
            name="customSelect"
            multiple
          >
            <option value="">Select</option>
            <option>Value 1</option>
            <option>Value 2</option>
            <option>Value 3</option>
          </CustomInput>
        </FormGroup>
        <FormGroup>
          <Label for="custom-select">Custom Select Disabled</Label>
          <CustomInput
            type="select"
            id="custom-select"
            name="custom-select"
            disabled
          >
            <option value="">Select</option>
            <option>Value 1</option>
            <option>Value 2</option>
            <option>Value 3</option>
          </CustomInput>
        </FormGroup>
        <FormGroup>
          <Label for="multiple-select">Custom Multiple Select Disabled</Label>
          <CustomInput
            type="select"
            id="multiple-select"
            name="customSelect"
            multiple
            disabled
          >
            <option value="">Select</option>
            <option>Value 1</option>
            <option>Value 2</option>
            <option>Value 3</option>
          </CustomInput>
        </FormGroup>
        <FormGroup>
          <Label for="range">Custom Range</Label>
          <CustomInput type="range" id="range" name="range" />
        </FormGroup>
        <FormGroup>
          <Label for="file">File Browser</Label>
          <CustomInput type="file" id="file" name="file" />
        </FormGroup>
        <FormGroup>
          <Label for="file-2">File Browser with Custom Label</Label>
          <CustomInput
            type="file"
            id="file-2"
            name="customFile"
            label="pick a file"
          />
        </FormGroup>
        <FormGroup>
          <Label for="file-3">File Browser Disabled</Label>
          <CustomInput type="file" id="file-3" name="file-3" disabled />
        </FormGroup>
      </Form>
    </div>
  );
}

We use the CustomInput component with various props.

type has the type of input to render.

It also takes options as children if the type is select .

We can make inputs inline with the inline prop.

If the type is set to switch , them we get a switch.

Input Group

Input groups are containers for inputs.

We can put content other than inputs inside input groups in addition to inputs itself.

For example, we can write:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import { InputGroup, InputGroupAddon, InputGroupText, Input } from "reactstrap";

export default function App() {
  return (
    <div>
      <InputGroup>
        <InputGroupAddon addonType="prepend">
          <InputGroupText>@</InputGroupText>
        </InputGroupAddon>
        <Input placeholder="username" />
      </InputGroup>
      <br />
      <InputGroup>
        <InputGroupAddon addonType="prepend">
          <InputGroupText>
            <Input addon type="checkbox" />
          </InputGroupText>
        </InputGroupAddon>
        <Input placeholder="I agree" />
      </InputGroup>
      <br />
      <InputGroup>
        <Input placeholder="username" />
        <InputGroupAddon addonType="append">
          <InputGroupText>[@example](http://twitter.com/example "Twitter profile for @example").com</InputGroupText>
        </InputGroupAddon>
      </InputGroup>
      <br />
      <InputGroup>
        <InputGroupAddon addonType="prepend">
          <InputGroupText>$</InputGroupText>
          <InputGroupText>$</InputGroupText>
        </InputGroupAddon>
        <Input placeholder="amount" />
        <InputGroupAddon addonType="append">
          <InputGroupText>$</InputGroupText>
          <InputGroupText>$</InputGroupText>
        </InputGroupAddon>
      </InputGroup>
      <br />
      <InputGroup>
        <InputGroupAddon addonType="prepend">$</InputGroupAddon>
        <Input placeholder="Amount" min={0} max={100} type="number" step="1" />
        <InputGroupAddon addonType="append">.00</InputGroupAddon>
      </InputGroup>
    </div>
  );
}

We have the InputGroup component to use as the input group container.

InputGroupText has the content for the input group addon.

We can have text or other elements inside it.

InputGroupAddon has the addons on each side.

addonType specifies the side that it’ll be on.

If it’s prepend , it’ll be on the left side.

If it’s append , then it’ll be on the right side.

Addon Sizing

The size of the addon can be changed.

For example, we can write:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import { InputGroup, InputGroupAddon, Input } from "reactstrap";

export default function App() {
  return (
    <div>
      <InputGroup size="lg">
        <InputGroupAddon addonType="prepend">lg</InputGroupAddon>
        <Input />
      </InputGroup>
      <br />
      <InputGroup>
        <InputGroupAddon addonType="prepend">normal</InputGroupAddon>
        <Input />
      </InputGroup>
      <br />
      <InputGroup size="sm">
        <InputGroupAddon addonType="prepend">sm</InputGroupAddon>
        <Input />
      </InputGroup>
    </div>
  );
}

to change their size with the size prop.

The prop is on the InputGroup component so that the sizing applies to everything inside.

Conclusion

We can add custom inputs add inputs flexibly.

Also, we can add input groups to add elements to the left or right of the input.

Categories
Reactstrap

Reactstrap — Form Validation and Customizations

Reactstrap is a version Bootstrap made for React.

It’s a set of React components that have Boostrap styles.

In this article, we’ll look at how to add form validation and customizations with Reactstrap.

Form Validation

Reactstrap comes with styles for form validation.

We can apply them with the valid and invalid props.

For example, we can add form fields with those styles by writing:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import {
  Form,
  FormGroup,
  Label,
  Input,
  FormFeedback,
  FormText
} from "reactstrap";

export default function App() {
  return (
    <div>
      <Form>
        <FormGroup>
          <Label for="exampleEmail">Input without validation</Label>
          <Input />
          <FormFeedback>can't see this</FormFeedback>
          <FormText>some text.</FormText>
        </FormGroup>
        <FormGroup>
          <Label>Valid input</Label>
          <Input valid />
          <FormFeedback valid>looks good</FormFeedback>
          <FormText>some text.</FormText>
        </FormGroup>
        <FormGroup>
          <Label>Invalid input</Label>
          <Input invalid />
          <FormFeedback>invalid input</FormFeedback>
          <FormText>some text.</FormText>
        </FormGroup>
        <FormGroup>
          <Label>Input without validation</Label>
          <Input />
          <FormFeedback tooltip>looks good</FormFeedback>
          <FormText>some text.</FormText>
        </FormGroup>
        <FormGroup>
          <Label>Valid input</Label>
          <Input valid />
          <FormFeedback valid tooltip>
            looks good
          </FormFeedback>
          <FormText>some text.</FormText>
        </FormGroup>
        <FormGroup>
          <Label>Invalid input</Label>
          <Input invalid />
          <FormFeedback tooltip>invalid input</FormFeedback>
          <FormText>some text.</FormText>
        </FormGroup>
      </Form>
    </div>
  );
}

We have various things in this form.

The valid or invalid props are added to Input s to style them according to their validity.

We put the validation feedback in the FormFeedback component.

valid and invalid can also be added to FormFeedback to style them.

tooltip makes the form feedback a tooltip.

Valid inputs will be green.

Invalid inputs will be red.

There’s also an icon on the right side of the input.

Inline Checkboxes

We can add inline checkboxes with the check and inline props.

For example, we can write:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import { Form, FormGroup, Label, Input } from "reactstrap";

export default function App() {
  return (
    <div>
      <Form>
        <FormGroup check inline>
          <Label check>
            <Input type="checkbox" /> apple
          </Label>
        </FormGroup>
        <FormGroup check inline>
          <Label check>
            <Input type="checkbox" /> orange
          </Label>
        </FormGroup>
      </Form>
    </div>
  );
}

We add both props to the FormGroup to make it inline.

Also, we added the check prop to the Label to make it show the checkbox with Bootstrap styles.

Input Sizing

The sizing of inputs can be changed with the bsSize prop.

For example, we can write:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import { Form, Input } from "reactstrap";

export default function App() {
  return (
    <div>
      <Form>
        <Input placeholder="lg" bsSize="lg" />
        <Input placeholder="default" />
        <Input placeholder="sm" bsSize="sm" />
        <Input type="select" bsSize="lg">
          <option>Large</option>
        </Input>
        <Input type="select">
          <option>Default</option>
        </Input>
        <Input type="select" bsSize="sm">
          <option>Small</option>
        </Input>
      </Form>
    </div>
  );
}

We can set the value to lg or sm to set the size to large or small respectively.

Input Grid Sizing

We can change the input grid size with the size prop.

For example, we can write:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import { Col, Form, FormGroup, Label, Input } from "reactstrap";

export default function App() {
  return (
    <div>
      <Form>
        <FormGroup row>
          <Label for="email" sm={2} size="lg">
            Email
          </Label>
          <Col sm={10}>
            <Input
              type="email"
              name="email"
              id="email"
              placeholder="lg"
              bsSize="lg"
            />
          </Col>
        </FormGroup>
        <FormGroup row>
          <Label for="email2" sm={2}>
            Email
          </Label>
          <Col sm={10}>
            <Input
              type="email"
              name="email"
              id="email2"
              placeholder="default"
            />
          </Col>
        </FormGroup>
      </Form>
    </div>
  );
}

We have the FormGroup component with the row prop to make them display as rows.

And we have the size prop on the label and bsSize on the input to change the size of them.

Hidden Labels

We can add hidden labels to our form with the hidden prop.

For example, we can write:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import { Button, Form, FormGroup, Label, Input } from "reactstrap";

export default function App() {
  return (
    <div>
      <Form inline>
        <FormGroup>
          <Label for="email" hidden>
            Email
          </Label>
          <Input type="email" name="email" id="email" placeholder="Email" />
        </FormGroup>
        <FormGroup>
          <Label for="password" hidden>
            Password
          </Label>
          <Input
            type="password"
            name="password"
            id="password"
            placeholder="Password"
          />
        </FormGroup>
        <Button>Submit</Button>
      </Form>
    </div>
  );
}

to add the hidden props to the labels.

Now we won’t see them.

Conclusion

We can add styles form validation.

Also, hidden labels can be added and sizes can be changed with props.

Categories
Reactstrap

Reactstrap — Forms

Reactstrap is a version Bootstrap made for React.

It’s a set of React components that have Boostrap styles.

In this article, we’ll look at how to add forms with Reactstrap.

Form Grid

We can put form components in a grid.

For example, we can write:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import {
  Col,
  Button,
  Form,
  FormGroup,
  Label,
  Input,
  FormText
} from "reactstrap";

export default function App() {
  return (
    <div>
      <Form>
        <FormGroup row>
          <Label for="email" sm={2}>
            Email
          </Label>
          <Col sm={10}>
            <Input
              type="email"
              name="email"
              id="email"
              placeholder="placeholder"
            />
          </Col>
        </FormGroup>
        <FormGroup row>
          <Label for="password" sm={2}>
            Password
          </Label>
          <Col sm={10}>
            <Input
              type="password"
              name="password"
              id="password"
              placeholder="password placeholder"
            />
          </Col>
        </FormGroup>
        <FormGroup row>
          <Label for="select" sm={2}>
            Select
          </Label>
          <Col sm={10}>
            <Input type="select" name="select" id="select">
              <option>1</option>
              <option>2</option>
              <option>3</option>
            </Input>
          </Col>
        </FormGroup>
        <FormGroup row>
          <Label for="multiple" sm={2}>
            Select Multiple
          </Label>
          <Col sm={10}>
            <Input type="select" name="selectMulti" id="multiple" multiple>
              <option>1</option>
              <option>2</option>
              <option>3</option>
            </Input>
          </Col>
        </FormGroup>
        <FormGroup row>
          <Label for="text" sm={2}>
            Text Area
          </Label>
          <Col sm={10}>
            <Input type="textarea" name="text" id="text" />
          </Col>
        </FormGroup>
        <FormGroup row>
          <Label for="file" sm={2}>
            File
          </Label>
          <Col sm={10}>
            <Input type="file" name="file" id="file" />
            <FormText color="muted">placeholder</FormText>
          </Col>
        </FormGroup>
        <FormGroup tag="fieldset" row>
          <legend className="col-form-label col-sm-2">Radio Buttons</legend>
          <Col sm={10}>
            <FormGroup check>
              <Label check>
                <Input type="radio" name="fruit" /> apple
              </Label>
            </FormGroup>
            <FormGroup check>
              <Label check>
                <Input type="radio" name="fruit" /> orange
              </Label>
            </FormGroup>
            <FormGroup check disabled>
              <Label check>
                <Input type="radio" name="fruit" disabled /> grape disabled
              </Label>
            </FormGroup>
          </Col>
        </FormGroup>
        <FormGroup row>
          <Label for="checkbox2" sm={2}>
            Checkbox
          </Label>
          <Col sm={{ size: 12 }}>
            <FormGroup check>
              <Label check>
                <Input type="checkbox" id="checkbox2" /> Check me out
              </Label>
            </FormGroup>
          </Col>
        </FormGroup>
        <FormGroup check row>
          <Col sm={{ size: 12, offset: 2 }}>
            <Button>Submit</Button>
          </Col>
        </FormGroup>
      </Form>
    </div>
  );
}

We put the Col in a FormGroup so that we can place the form elements in a grid.

The row prop in the FormGroup makes the form group display as a row.

The col can have optional breakpoint props to set the size.

sm sets the column size to 10 if the screen is sm or wider.

Form Grid with Form Row

We can also use the Row component with the form prop to add a form row.

For example, we can write:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import { Col, Row, Button, Form, FormGroup, Label, Input } from "reactstrap";

export default function App() {
  return (
    <div>
      <Form>
        <Row form>
          <Col md={6}>
            <FormGroup>
              <Label for="email">Email</Label>
              <Input
                type="email"
                name="email"
                id="email"
                placeholder="placeholder"
              />
            </FormGroup>
          </Col>
          <Col md={6}>
            <FormGroup>
              <Label for="password">Password</Label>
              <Input
                type="password"
                name="password"
                id="password"
                placeholder="password"
              />
            </FormGroup>
          </Col>
        </Row>
        <FormGroup>
          <Label for="address">Address</Label>
          <Input
            type="text"
            name="address"
            id="address"
            placeholder="address"
          />
        </FormGroup>
        <FormGroup>
          <Label for="address2">Address 2</Label>
          <Input
            type="text"
            name="address2"
            id="address2"
            placeholder="address 2"
          />
        </FormGroup>
        <Row form>
          <Col md={6}>
            <FormGroup>
              <Label for="city">City</Label>
              <Input type="text" name="city" id="city" />
            </FormGroup>
          </Col>
          <Col md={4}>
            <FormGroup>
              <Label for="region">Region</Label>
              <Input type="text" name="region" id="region" />
            </FormGroup>
          </Col>
          <Col md={2}>
            <FormGroup>
              <Label for="postal">Postal Code</Label>
              <Input type="text" name="postal" id="postal" />
            </FormGroup>
          </Col>
        </Row>
        <FormGroup check>
          <Input type="checkbox" name="check" id="checkbox" />
          <Label for="checkbox" check>
            I agree
          </Label>
        </FormGroup>
        <Button>Sign up</Button>
      </Form>
    </div>
  );
}

We have the Row component instead of a FormGroup in some places.

The form prop makes it a form row.

Inline Form

We can make an inline form with the inline prop on the Form .

For example, we can write:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import { Button, Form, FormGroup, Label, Input } from "reactstrap";

export default function App() {
  return (
    <div>
      <Form inline>
        <FormGroup className="mb-2 mr-sm-2 mb-sm-0">
          <Label for="email" className="mr-sm-2">
            Email
          </Label>
          <Input
            type="email"
            name="email"
            id="email"
            placeholder="email@email.cool"
          />
        </FormGroup>
        <FormGroup className="mb-2 mr-sm-2 mb-sm-0">
          <Label for="password" className="mr-sm-2">
            Password
          </Label>
          <Input
            type="password"
            name="password"
            id="password"
            placeholder="password"
          />
        </FormGroup>
        <Button>Submit</Button>
      </Form>
    </div>
  );
}

to make form fields inline with the inline prop.

We should add some margin classes to space out the labels and input boxes.

Conclusion

We can add forms with the content displayed our way.

The layout and spacing can change.

Categories
Reactstrap

Reactstrap — Dropdowns and Forms

Reactstrap is a version Bootstrap made for React.

It’s a set of React components that have Boostrap styles.

In this article, we’ll look at how to add dropdowns and forms with Reactstrap.

Modifiers

We can add modifiers to style our dropdown.

To do that, we use the modifiers prop to change the dropdown:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import {
  Dropdown,
  DropdownToggle,
  DropdownMenu,
  DropdownItem
} from "reactstrap";

export default function App() {
  const [isOpen, setIsOpen] = React.useState(false);

const toggle = () => setIsOpen(prevState => !prevState);

return (
    <>
      <Dropdown isOpen={isOpen} toggle={toggle}>
        <DropdownToggle>Dropdown</DropdownToggle>
        <DropdownMenu
          modifiers={{
            setMaxHeight: {
              enabled: true,
              order: 890,
              fn: data => {
                return {
                  ...data,
                  styles: {
                    ...data.styles,
                    overflow: "auto",
                    maxHeight: "100px"
                  }
                };
              }
            }
          }}
        >
          <DropdownItem>Action</DropdownItem>
          <DropdownItem>Action</DropdownItem>
          <DropdownItem>Action</DropdownItem>
        </DropdownMenu>
      </Dropdown>
    </>
  );
}

We have the modifiers prop to change the height.

We have the setMaxHeight property that has an object with the enabled property to enable the modifier.

fn returns the style we want for styling the dropdown.

setActiveFromChild

We can use the setActiveFromChild prop to set the dropdown to active when any dropdown menu items are active.

For example, we can write:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import {
  Navbar,
  Nav,
  NavItem,
  NavLink,
  UncontrolledDropdown,
  DropdownToggle,
  DropdownMenu,
  DropdownItem
} from "reactstrap";

export default function App() {
  return (
    <>
      <Navbar color="light" light expand="md">
        <Nav className="ml-auto" navbar>
          <NavItem>
            <NavLink href="/home/">Inactive Link</NavLink>
          </NavItem>
          <UncontrolledDropdown setActiveFromChild>
            <DropdownToggle tag="a" className="nav-link" caret>
              Dropdown
            </DropdownToggle>
            <DropdownMenu>
              <DropdownItem tag="a" href="/foo" active>
                Link
              </DropdownItem>
            </DropdownMenu>
          </UncontrolledDropdown>
        </Nav>
      </Navbar>
    </>
  );
}

We have the setActiveFromChild prop to make it active.

Fade

We can use the Fade component to show a fade effect for transitions.

For example, we can write:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import { Button, Fade } from "reactstrap";

export default function App() {
  const [fadeIn, setFadeIn] = React.useState(true);

  const toggle = () => setFadeIn(!fadeIn);

  return (
    <div>
      <Button color="primary" onClick={toggle}>
        Toggle Fade
      </Button>
      <Fade in={fadeIn} tag="h5" className="mt-3">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis imperdiet,
        lacus id rutrum euismod, nunc enim fringilla tortor, a suscipit turpis
        lacus dignissim mauris.
      </Fade>
    </div>
  );
}

We have the button to toggle the item in the Fade component.

This is done with the in prop.

It controls whether the item is displayed or not.

Form

Reactstrap comes with form components.

For example, we can write:

import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import { Button, Form, FormGroup, Label, Input, FormText } from "reactstrap";

export default function App() {
  return (
    <div>
      <Form>
        <FormGroup>
          <Label for="email">Email</Label>
          <Input
            type="email"
            name="email"
            id="email"
            placeholder="placeholder"
          />
        </FormGroup>
        <FormGroup>
          <Label for="password">Password</Label>
          <Input
            type="password"
            name="password"
            id="password"
            placeholder="password placeholder"
          />
        </FormGroup>
        <FormGroup>
          <Label for="select">Select</Label>
          <Input type="select" name="select" id="select">
            <option>1</option>
            <option>2</option>
            <option>3</option>
          </Input>
        </FormGroup>
        <FormGroup>
          <Label for="selectMulti">Select Multiple</Label>
          <Input type="select" name="selectMulti" id="selectMulti" multiple>
            <option>1</option>
            <option>2</option>
            <option>3</option>
          </Input>
        </FormGroup>
        <FormGroup>
          <Label for="textarea">Text Area</Label>
          <Input type="textarea" name="text" id="textarea" />
        </FormGroup>
        <FormGroup>
          <Label for="file-input">File</Label>
          <Input type="file" name="file" id="file-input" />
          <FormText color="muted">placeholder</FormText>
        </FormGroup>
        <FormGroup tag="fieldset">
          <legend>Radio Buttons</legend>
          <FormGroup check>
            <Label check>
              <Input type="radio" name="fruit" /> apple
            </Label>
          </FormGroup>
          <FormGroup check>
            <Label check>
              <Input type="radio" name="fruit" /> orange
            </Label>
          </FormGroup>
          <FormGroup check disabled>
            <Label check>
              <Input type="radio" name="fruit" disabled /> grape
            </Label>
          </FormGroup>
        </FormGroup>
        <FormGroup check>
          <Label check>
            <Input type="checkbox" /> I agree
          </Label>
        </FormGroup>
        <Button>Submit</Button>
      </Form>
    </div>
  );
}

We have the form with the form groups.

Inside the form groups, we have the labels and inputs.

They’re all added with the Reactstrap components.

The tag prop lets us render form groups with a different tag.

The check prop lets us place checkboxes in the form group.

The Input component is used for adding all kinds of inputs.

The type of input is specified by the type prop.

Conclusion

We can set the modifiers prop to modify dropdowns.

Reactstrap comes with components for building forms.