Giter Club home page Giter Club logo

workshop-ws-4-26-reactcomps's Introduction

CS52 Workshops: React Components

screen shots are helpful

Just like bootstrap or semantic-ui, but for React! Prebuilt components & styling. No re-inventing the wheel.

Overview

Ant Design (https://ant.design/docs/react/introduce) is a React UI library with components you can use in your React app. We will be using Ant Design to make a landing page.

We will implement:

  • a Ant Design's basic layout
    • Header
    • Sider (responsive navigation)
    • Content
    • Footer
  • Search bar with autocomplete

Setup

Pull from our directory. This contains a starterpack and some starting code.

$ git init
$ git remote add starter https://github.com/Flashing3/workshopstarter
$ git pull starter master --allow-unrelated-histories

Install antd from yarn.

$ yarn add antd

Overview of Layout Components

Here are the basic components we are using today!

Layout : The layout wrapper, in which Header Sider Content Footer or Layout itself can be nested, and can be placed in any parent container.

Header: The top layout with default style, in which any element can be nested, and must be placed in Layout.

Sider : The sidebar with default style and basic functions, in which any element can be nested, and must be placed in Layout.

Content : The content layout with default style, in which any element can be nested, and must be placed in Layout.

Footer : The bottom layout with default style, in which any element can be nested, and must be placed in Layout. Layout handles the overall layout of a page.

(More info: https://ant.design/components/layout/).

1. Add Layout

Import Layout, Menu and Icon at the top of your index.js file.

import { Layout, Menu, Icon } from 'antd';

Next, let's add Ant Design's css! Add antd/dist/antd.css manually into your index.js. Your imports should look like this:

import React from 'react';
import ReactDOM from 'react-dom';
import { Layout, Menu, Icon } from 'antd';
import 'antd/dist/antd.css';
import './style.scss';

Sidenote on importing css: don't actually do this.

Normally if you were using your starterpack, you can do the following. This allows you to import components without having to manually import the corresponding stylesheet. It imports it for you.

{
  "plugins": [
    ["import", { "libraryName": "antd", "libraryDirectory": "es", "style": "css" }] // `style: true` for less
  ]
}

Now do run it!

$ yarn start

Now you should see this. There's a Sider but no menu items.

Look at your index.js. In between the Menu tags, add the following code to create menu items.

<Menu.Item key="1">
  <Icon type="user" />
  <span className="nav-text">nav 1</span>
</Menu.Item>
<Menu.Item key="2">
  <Icon type="video-camera" />
  <span className="nav-text">nav 2</span>
</Menu.Item>
<Menu.Item key="3">
  <Icon type="upload" />
  <span className="nav-text">nav 3</span>
</Menu.Item>
<Menu.Item key="4">
  <Icon type="user" />
  <span className="nav-text">nav 4</span>
</Menu.Item>

You should see some menu items in the Sider.

2. Add an autocomplete search bar ๐Ÿ”

Our search bar will live in a new component! You need to add this in. ๐Ÿ˜ˆ Make a new components directory in your src directory.

Now make a file called Complete.js and add the following code:

import React, { Component } from 'react';
import { AutoComplete } from 'antd';

function onSelect(value) {
  console.log('onSelect', value);
}

class Complete extends Component {
  state = {
    dataSource: [],
  }

  handleSearch = (value) => {
    this.setState({
      dataSource: !value ? [] : [
        value,
        value + value,
        value + value + value,
      ],
    });
  }

  render() {
    const { dataSource } = this.state;
    return (
      <AutoComplete
        dataSource={dataSource}
        style={{ width: 200 }}
        onSelect={onSelect}
        onSearch={this.handleSearch}
        placeholder="input here"
      />
    );
  }
}

// ReactDOM.render(<Complete />, mountNode);

export default Complete;

Now import this into your index.js!

import Complete from './components/Complete';

You want to render this search bar. Make a closing Header tag, then add the search bar between the Header tags.

Now you should see the autocomplete search bar!!!

3. Carousel ๐ŸŒˆ

Add a carousel the same way you added the search bar. https://ant.design/components/carousel/

Make another component to hold your carousel.

import React from 'react';
import { Carousel } from 'antd';


function onChange(a, b, c) {
 console.log(a, b, c);
}

const CarouselComp = (props) => {
 return (
   <Carousel afterChange={onChange}>
     <div><h3>1</h3></div>
     <div><h3>2</h3></div>
     <div><h3>3</h3></div>
     <div><h3>4</h3></div>
   </Carousel>
 );
 // }
};

export default CarouselComp;

You also need the Carousel style. In your style.scss file:

.logo {
 height: 32px;
 background: rgba(255,255,255,.2);
 margin: 16px;
}
.ant-carousel .slick-slide {
 text-align: center;
 height: 160px;
 line-height: 160px;
 background: #364d79;
 overflow: hidden;
}
.ant-carousel .slick-slide h3 {
 color: #fff;
}

Import your carousel into index.js

import CarouselComp from './components/carousel';;

Make it render inside your content!

<Content style={{ margin: '24px 16px 0' }}>
      <div style={{ padding: 24, background: '#fff', minHeight: 360 }}>
        <CarouselComp />
      </div>
</Content>

4. Extra Credit! โœจ

Add pictures to your carousel!

Add in a third component of your choice! https://ant.design/docs/react/introduce Here are some ideas:

  • Timeline ๐Ÿ•
  • Calendar ๐Ÿ“…

Summary / What you Learned

You are done!! Amazing!!

  • How to add Ant Design Components
  • Make a basic layout
  • Made a autocomplete search bar

Resources

workshop-ws-4-26-reactcomps's People

Contributors

savannahliu avatar timofei7 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.