Categories
Shards React

React Development with the Shards React Library — Form Input and Radio Buttons

Shards React is a useful UI library that lets us add many components easily into our React app.

In this article, we’ll look at how to use it to add components to our React app.

Form Input

We can add a text input into our React app with Shards React’s FormInput component.

For instance, we can write:

import React from "react";
import { FormInput } from "shards-react";
import "bootstrap/dist/css/bootstrap.min.css";
import "shards-ui/dist/css/shards.min.css";

export default function App() {
  return (
    <div className="App">
      <FormInput placeholder="form input" />;
    </div>
  );
}

to add it into our app.

placeholder has the input placeholder.

We can change the size with:

import React from "react";
import { FormInput } from "shards-react";
import "bootstrap/dist/css/bootstrap.min.css";
import "shards-ui/dist/css/shards.min.css";

export default function App() {
  return (
    <div className="App">
      <FormInput size="lg" placeholder="form input" />;
    </div>
  );
}

size has the form input size.

lg makes it big.

sm makes it small.

We can set valid and invalid to display a green border if it’s valid and a red border if it’s not.

For instance, we can write:

import React from "react";
import { FormInput } from "shards-react";
import "bootstrap/dist/css/bootstrap.min.css";
import "shards-ui/dist/css/shards.min.css";

export default function App() {
  return (
    <div className="App">
      <FormInput valid placeholder="form input" />;
    </div>
  );
}

to add it.

Radio Buttons

We can add radio buttons with the FormRadio component.

For instance, we can write:

import React, { useState } from "react";
import { FormRadio } from "shards-react";
import "bootstrap/dist/css/bootstrap.min.css";
import "shards-ui/dist/css/shards.min.css";

export default function App() {
  const [selectedFruit, setSelectedFruit] = useState();
  const changeFruit = (fruit) => {
    setSelectedFruit(fruit);
  };

  return (
    <div className="App">
      <FormRadio
        name="fruit"
        checked={selectedFruit === "orange"}
        onChange={() => {
          changeFruit("orange");
        }}
      >
        Orange
      </FormRadio>
      <FormRadio
        name="fruit"
        checked={selectedFruit === "lemon"}
        onChange={() => {
          changeFruit("lemon");
        }}
      >
        Lemon
      </FormRadio>
    </div>
  );
}

We have the changeFruit function which calls setSelectedFruit to change the selectedFruit state.

Then we add the boolean expression to set the radio button value to the checked prop.

If it returns true , then the radio button is selected.

We should set their name props to be the same so they’re identified as being in the same group.

We can make it inline with the inline prop:

import React, { useState } from "react";
import { FormRadio } from "shards-react";
import "bootstrap/dist/css/bootstrap.min.css";
import "shards-ui/dist/css/shards.min.css";

export default function App() {
  const [selectedFruit, setSelectedFruit] = useState();
  const changeFruit = (fruit) => {
    setSelectedFruit(fruit);
  };

  return (
    <div className="App">
      <FormRadio
        inline
        name="fruit"
        checked={selectedFruit === "orange"}
        onChange={() => {
          changeFruit("orange");
        }}
      >
        Orange
      </FormRadio>
      <FormRadio
        inline
        name="fruit"
        checked={selectedFruit === "lemon"}
        onChange={() => {
          changeFruit("lemon");
        }}
      >
        Lemon
      </FormRadio>
    </div>
  );
}

Conclusion

We can add form inputs and radio buttons into our React app with Shards React.

Categories
Shards React

React Development with the Shards React Library — Forms and Checkboxes

Shards React is a useful UI library that lets us add many components easily into our React app.

In this article, we’ll look at how to use it to add components to our React app.

Form

Shards React comes with the Form component to let us add forms into our React app.

For instance, we can write:

import React from "react";
import { Form, FormInput, FormGroup } from "shards-react";
import "bootstrap/dist/css/bootstrap.min.css";
import "shards-ui/dist/css/shards.min.css";

export default function App() {
  return (
    <div className="App">
      <Form className="p-1">
        <FormGroup>
          <label htmlFor="#username">Username</label>
          <FormInput id="#username" placeholder="Username" />
        </FormGroup>
        <FormGroup>
          <label htmlFor="#password">Password</label>
          <FormInput type="password" id="#password" placeholder="Password" />
        </FormGroup>
      </Form>
    </div>
  );
}

to add the Form component with FormGroup s and FormInput s to add input fields into our app.

Checkbox

We can add a checkbox with the FormCheckox component.

For example, we can write:

import React, { useState } from "react";
import { FormCheckbox } from "shards-react";
import "bootstrap/dist/css/bootstrap.min.css";
import "shards-ui/dist/css/shards.min.css";

export default function App() {
  const [selected, setSelected] = useState({});
  const { orange, lemon } = selected;

  const handleChange = (e, fruit) => {
    setSelected((fruits) => ({ ...fruits, [fruit]: !selected[fruit] }));
  };

  return (
    <div className="App">
      <FormCheckbox
        checked={orange}
        onChange={(e) => handleChange(e, "orange")}
      >
        Orange
      </FormCheckbox>
      <FormCheckbox checked={lemon} onChange={(e) => handleChange(e, "lemon")}>
        Lemon
      </FormCheckbox>
    </div>
  );
}

We have the selected state which is an object.

Then we destructured the object and pass those properties into the checked prop of FormCheckbox to set the checked state of each checkbox.

Then we add the onChange handler to each checkbox to call handleChange to update the checked value.

We can turn them to toggles with the toggle prop:

import React, { useState } from "react";
import { FormCheckbox } from "shards-react";
import "bootstrap/dist/css/bootstrap.min.css";
import "shards-ui/dist/css/shards.min.css";

export default function App() {
  const [selected, setSelected] = useState({});
  const { orange, lemon } = selected;

  const handleChange = (e, fruit) => {
    setSelected((fruits) => ({ ...fruits, [fruit]: !selected[fruit] }));
  };

  return (
    <div className="App">
      <FormCheckbox
        toggle
        checked={orange}
        onChange={(e) => handleChange(e, "orange")}
      >
        Orange
      </FormCheckbox>
      <FormCheckbox
        toggle
        checked={lemon}
        onChange={(e) => handleChange(e, "lemon")}
      >
        Lemon
      </FormCheckbox>
    </div>
  );
}

We can add the small and toggle props to add a small toggle:

import React, { useState } from "react";
import { FormCheckbox } from "shards-react";
import "bootstrap/dist/css/bootstrap.min.css";
import "shards-ui/dist/css/shards.min.css";

export default function App() {
  const [selected, setSelected] = useState({});
  const { orange, lemon } = selected;

  const handleChange = (e, fruit) => {
    setSelected((fruits) => ({ ...fruits, [fruit]: !selected[fruit] }));
  };

  return (
    <div className="App">
      <FormCheckbox
        toggle
        small
        checked={orange}
        onChange={(e) => handleChange(e, "orange")}
      >
        Orange
      </FormCheckbox>
      <FormCheckbox
        toggle
        small
        checked={lemon}
        onChange={(e) => handleChange(e, "lemon")}
      >
        Lemon
      </FormCheckbox>
    </div>
  );
}

We can make them inline with the inline prop:

import React, { useState } from "react";
import { FormCheckbox } from "shards-react";
import "bootstrap/dist/css/bootstrap.min.css";
import "shards-ui/dist/css/shards.min.css";

export default function App() {
  const [selected, setSelected] = useState({});
  const { orange, lemon } = selected;

  const handleChange = (e, fruit) => {
    setSelected((fruits) => ({ ...fruits, [fruit]: !selected[fruit] }));
  };

  return (
    <div className="App">
      <FormCheckbox
        inline
        checked={orange}
        onChange={(e) => handleChange(e, "orange")}
      >
        Orange
      </FormCheckbox>
      <FormCheckbox
        inline
        checked={lemon}
        onChange={(e) => handleChange(e, "lemon")}
      >
        Lemon
      </FormCheckbox>
    </div>
  );
}

Conclusion

We can add forms and checkbox into our React app with Shards React.

Categories
Shards React

React Development with the Shards React Library — Button

Shards React is a useful UI library that lets us add many components easily into our React app.

In this article, we’ll look at how to use it to add components to our React app.

Button

We can add a button with React Shard’s Button component.

For instance, we can write:

import React from "react";
import { Button } from "shards-react";
import "shards-ui/dist/css/shards.min.css";

export default function App() {
  return (
    <div className="App">
      <Button theme="secondary">Secondary</Button>
    </div>
  );
}

We set the theme to set the background color.

We can also set it to success , danger , info , light , and dark .

If we leave it out, we see a blue background color.

We can add an outlined style with the outline prop:

import React from "react";
import { Button } from "shards-react";
import "shards-ui/dist/css/shards.min.css";

export default function App() {
  return (
    <div className="App">
      <Button outline theme="secondary">
        Secondary
      </Button>
    </div>
  );
}

And we can make it pill-shaped with the pill prop:

import React from "react";
import { Button } from "shards-react";
import "shards-ui/dist/css/shards.min.css";

export default function App() {
  return (
    <div className="App">
      <Button pill theme="secondary">
        Secondary
      </Button>
    </div>
  );
}

We can add all the props together.

Also, we can make it square with the squared prop:

import React from "react";
import { Button } from "shards-react";
import "shards-ui/dist/css/shards.min.css";

export default function App() {
  return (
    <div className="App">
      <Button squared theme="secondary">
        Secondary
      </Button>
    </div>
  );
}

And we can change the size with the size prop:

import React from "react";
import { Button } from "shards-react";
import "shards-ui/dist/css/shards.min.css";

export default function App() {
  return (
    <div className="App">
      <Button size="lg" theme="secondary">
        Secondary
      </Button>
    </div>
  );
}

The active state can be added with the active prop:

import React from "react";
import { Button } from "shards-react";
import "shards-ui/dist/css/shards.min.css";

export default function App() {
  return (
    <div className="App">
      <Button active>Secondary</Button>
    </div>
  );
}

The disabled prop lets us make the button look disabled:

import React from "react";
import { Button } from "shards-react";
import "shards-ui/dist/css/shards.min.css";

export default function App() {
  return (
    <div className="App">
      <Button disabled>Secondary</Button>
    </div>
  );
}

The block prop makes the button display as a block-level element:

import React from "react";
import { Button } from "shards-react";
import "shards-ui/dist/css/shards.min.css";

export default function App() {
  return (
    <div className="App">
      <Button block>Secondary</Button>
    </div>
  );
}

Conclusion

We can add buttons with various styles into our React app with Shards React.

Categories
Shards React

React Development with the Shards React Library — Button Groups and Containers

Shards React is a useful UI library that lets us add many components easily into our React app.

In this article, we’ll look at how to use it to add components to our React app.

Button Group

We can add button groups into our React app with Shard Reacr’s ButtonGroip component:

import React from "react";
import { Button, ButtonGroup } from "shards-react";
import "shards-ui/dist/css/shards.min.css";

export default function App() {
  return (
    <div className="App">
      <ButtonGroup>
        <Button>Left</Button>
        <Button>Middle</Button>
        <Button>Right</Button>
      </ButtonGroup>
    </div>
  );
}

We can change the group size with the size prop:

import React from "react";
import { Button, ButtonGroup } from "shards-react";
import "shards-ui/dist/css/shards.min.css";

export default function App() {
  return (
    <div className="App">
      <ButtonGroup size="lg">
        <Button>Left</Button>
        <Button>Middle</Button>
        <Button>Right</Button>
      </ButtonGroup>
    </div>
  );
}

We can also set it to sm or leave it out.

And we can make the group vertical with the vertical prop:

import React from "react";
import { Button, ButtonGroup } from "shards-react";
import "shards-ui/dist/css/shards.min.css";

export default function App() {
  return (
    <div className="App">
      <ButtonGroup vertical>
        <Button>Left</Button>
        <Button>Middle</Button>
        <Button>Right</Button>
      </ButtonGroup>
    </div>
  );
}

Button Toolbar

We can add a button toolbar with the ButtonToolbar component:

import React from "react";
import {
  Button,
  ButtonGroup,
  ButtonToolbar,
  FormInput,
  InputGroup
} from "shards-react";
import "shards-ui/dist/css/shards.min.css";

export default function App() {
  return (
    <div className="App">
      <ButtonToolbar>
        <ButtonGroup size="sm">
          <Button>Create</Button>
          <Button>Edit</Button>
        </ButtonGroup>

        <InputGroup size="sm" className="ml-auto">
          <FormInput placeholder="Search..." />
        </InputGroup>
      </ButtonToolbar>
    </div>
  );
}

We can add ButtonGroup and InputGroup inside a ButtonToolbar .

Card

Cards let us add containers for content into our React app.

To add it, we write:

import React from "react";
import {
  Card,
  CardHeader,
  CardTitle,
  CardImg,
  CardBody,
  CardFooter,
  Button
} from "shards-react";
import "shards-ui/dist/css/shards.min.css";

export default function App() {
  return (
    <div className="App">
      <Card style={{ maxWidth: "300px" }}>
        <CardHeader>Card header</CardHeader>
        <CardImg src="https://place-hold.it/300x200" />
        <CardBody>
          <CardTitle>Lorem Ipsum</CardTitle>
          <p>Lorem ipsum.</p>
          <Button>Read more</Button>
        </CardBody>
        <CardFooter>Card footer</CardFooter>
      </Card>
    </div>
  );
}

We add a card with various parts.

Card is the card container.

CardHeader has the card header.

CardImg has the card image.

CardBody has the card body.

CardTitle has the card title.

CardFooter has the card footer.

Container

We can use the Collapse component to toggle the visibility of our content.

For instance, we can write:

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

export default function App() {
  return (
    <div className="App">
      <Container>
        <Row>
          <Col sm="12" lg="6">
            1 / 2
          </Col>
          <Col sm="12" lg="6">
            2 / 2
          </Col>
        </Row>
      </Container>
    </div>
  );
}

to add the Container com0ponent to add a flexbox container.

Row has the rows. And Col has the columns.

We set the sm and lg props to set the column sizes for those breakpoints.

Conclusion

We can add button groups and containers into our React app with Shards React.

Categories
Shards React

React Development with the Shards React Library — Collapse, Dropdowns, and Fade Effects

Shards React is a useful UI library that lets us add many components easily into our React app.

In this article, we’ll look at how to use it to add components to our React app.

Column Order and Offset

We can set the order and offset of the columns.

For instance, we can write:

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

export default function App() {
  return (
    <div className="App">
      <Container>
        <Row>
          <Col sm={{ size: 6, order: 2, offset: 2 }}>2</Col>
          <Col sm={{ size: 2, order: 1, offset: 2 }}>1</Col>
        </Row>
      </Container>
    </div>
  );
}

to set the order property to change the order.

And we can change the offset to change the offset size.

Collapse

We can add a collapse component into our React app with Shards React.

For instance, we can write:

import React, { useState } from "react";
import { Button, Collapse } from "shards-react";
import "bootstrap/dist/css/bootstrap.min.css";
import "shards-ui/dist/css/shards.min.css";

export default function App() {
  const [collapse, setCollapse] = useState(false);

  const toggle = () => {
    setCollapse((c) => !c);
  };
  return (
    <div className="App">
      <Button onClick={toggle}>Toggle</Button>
      <Collapse open={collapse}>
        <div className="p-3 mt-3 border rounded">
          <h5>Lorem ipsum</h5>
          <span>Lorem ipsum</span>
        </div>
      </Collapse>
    </div>
  );
}

We have the collapse state which we pass into the open prop to control whether the Collapse component is displayed or not.

The Bootstrap styles are required for it to work properly.

Dropdown

We can add dropdowns with the Dropdown , DropdownToggle , DropdownMenu and DropdownItem components.

For instance, we can write:

import React, { useState } from "react";
import {
  Dropdown,
  DropdownToggle,
  DropdownMenu,
  DropdownItem
} from "shards-react";
import "bootstrap/dist/css/bootstrap.min.css";
import "shards-ui/dist/css/shards.min.css";

export default function App() {
  const [open, setOpen] = useState(false);

  const toggle = () => {
    setOpen((c) => !c);
  };
  return (
    <div className="App">
      <Dropdown open={open} toggle={toggle}>
        <DropdownToggle>Dropdown</DropdownToggle>
        <DropdownMenu>
          <DropdownItem>Action</DropdownItem>
          <DropdownItem>Another action</DropdownItem>
          <DropdownItem>Something else here</DropdownItem>
        </DropdownMenu>
      </Dropdown>
    </div>
  );
}

We add the Dropdown component to add the dropdown container.

DropdownToggle has the dropdown rtoggle.

DropdownMenu has the dropdown menu.

DropdownItem has the dropdown items.

We set the open prop to control when it’s open.

The toggle prop has the toggle function to control the open state.

Fade

The Fade component lets us add a fade effect to our React app.

For instance, we write:

import React, { useState } from "react";
import { Fade, Button } from "shards-react";

import "bootstrap/dist/css/bootstrap.min.css";
import "shards-ui/dist/css/shards.min.css";

export default function App() {
  const [visible, setVisible] = useState(false);

  const toggle = () => {
    setVisible((c) => !c);
  };
  return (
    <div className="App">
      <Button onClick={toggle} className="mb-2">
        Toggle
      </Button>
      <Fade in={visible} className="p-1">
        lorem ipsum
      </Fade>
    </div>
  );
}

to add a Button to toggle the Fade component.

The in prop lets us set the visibility of the Fade component.

And the toggle function toggles the visible state to control Fade ‘s visibility.

Conclusion

We can add collapse, dropdowns, and fade effects into our React app with Shard React.