天天看點

少寫css, 早下班! Antd完成todo-list樣式布局

  • Antd是一個UI元件庫, 與React非常搭
  • 善用UI庫, 可以節省寫css樣式的時間
  • 如果我們把寫css的時間壓縮一大半, 或許就可以早點下班了~

關于Antd

antd怎麼用?

  • 安裝antd
npm install antd -S
           
  • 在react元件中引入antd及其部分元件
import 'antd/dist/antd.css';
import { Input, List, Tag, Switch} from 'antd';
           
  • 在react元件中使用antd(以标簽為例)
{/*這裡的item是一個數組*/}
<Tag color="orange" style={{position: "absolute", right: 0}}>建立日期: {item[2]}</Tag>
           

用antd寫一個待辦清單

  • 自動添加建立日期
  • 支援任務的開啟關閉(點選switch元件或點選文字)
  • 支援删除任務(在關閉狀态下點選任務文字)

核心元件源碼

import React, { Component } from 'react';
import './App.css';
import 'antd/dist/antd.css';
import { Input, List, Tag, Switch} from 'antd';


class App extends Component {

  constructor(props){
    super(props);
    this.state = {
      list: [],
      inputValue: ''
    }
    this.addContent = this.addContent.bind(this);
    this.handleInputValue = this.handleInputValue.bind(this);
    this.removeItem = this.removeItem.bind(this);
    this.handleNewTodoKeyDown = this.handleNewTodoKeyDown.bind(this);
    this.getCurrentDate = this.getCurrentDate.bind(this);
  }

  componentDidMount(){
    // 頁面加載後自動聚焦
    this.nameInput.focus();
  }

  // 将輸入框内容添加到清單
  addContent() {
    let tmpValue =  this.state.inputValue
    // 内容為空無法添加
    if (tmpValue === "") {
      return
    }
    // 擷取目前時間
    let tmpDate = this.getCurrentDate()

    let tmpList = JSON.parse(JSON.stringify(this.state.list));
    tmpList.unshift([tmpValue, 0, tmpDate])
    // 清空輸入框内容
    this.setState({inputValue: ''});
    // 重新渲染清單
    this.setState({list: tmpList})
    // 重新聚焦
    this.nameInput.focus();

  }

  // 将實時輸入的内容進行綁定
  handleInputValue(event) {
    let tmpInputValue = event.target.value;
    this.setState({inputValue: tmpInputValue});
  }

  // 擷取目前時間的dom
  getCurrentDate(){
    // 添加建立日期
    let dt = new Date()
    let year = dt.getFullYear();
    let month = dt.getMonth();
    let day = dt.getDate();
    let hour = dt.getHours();
    let minute = dt.getMinutes();
    let second= dt.getSeconds();
    // 将月份加1
    month = month + 1;
    if (month <= 9){month = "0" + month;}
    if (day <= 9){day = "0" + day;}
    if (hour <= 9){hour = "0" + hour;}
    if (minute <= 9){minute = "0" + minute;}
    if (second <= 9){second = "0" + second;}
    let creteData = year+ "年" + month+ "月" + day+ "日" + hour+ "時" + minute+ "分" + second+ "秒";
    return creteData

  }

  // 移除被點選的内容
  removeItem(index){
    let tmpListdata = JSON.parse(JSON.stringify(this.state.list))
    // 切割出一個元素的數組
    let tmpItem = tmpListdata.splice(index, 1);
    // 被點選的元素索引加1
    tmpItem[0][1] += 1;
    // 一次劃線
    if (tmpItem[0][1] === 1){
      // 将元素補回
      tmpListdata.splice(index, 0, ...tmpItem);
    }
      // 更新清單資料
    this.setState({list: tmpListdata});
  }

  // 捕捉回車動作
  handleNewTodoKeyDown(event) {
    let ENTER_KEY = 13;
    if (event.keyCode === ENTER_KEY) {
      this.addContent();
    }
  }
  
  // 處理開關按鈕
  handleSwitch(index){
    let tmpList = JSON.parse(JSON.stringify(this.state.list));
    tmpList[index][1] = tmpList[index][1] ? 0 : 1;
    this.setState({list: tmpList});
  } 

  render() {
  return (
    <div className="App">
      <Input 
      style = {{marginTop: 20, marginBottom: 20}}
      className="app-input" size="large" placeholder="請輸入待辦事項(回車添加)" 
      onChange={this.handleInputValue.bind(this)} 
      value={this.state.inputValue}
      onKeyDown={this.handleNewTodoKeyDown}
      ref={(input) => { this.nameInput = input; }} />


      <List
      size="large"
      bordered
      dataSource={this.state.list}
      style={{color: "#4091F7", fontWeight: "bold"}}
      renderItem={
        (item, index) => (
          <List.Item>
          <Switch style={{marginRight: 20}} onChange={this.handleSwitch.bind(this, index)} checked = {item[1] ? false : true} />
          <Tag color="orange" style={{position: "absolute", right: 0}}>建立日期: {item[2]}</Tag>
          <div onClick={this.removeItem.bind(this, index)} style={{cursor:"pointer"}}>
          {item[1] ? <div style={{textDecoration:"line-through"}}>{item[0]}</div> : <div>{item[0]}</div>}
          </div>
          </List.Item>)
      }
      />

    </div>
  );
  }
}

export default App;
           
為便于管理, 相關資源整合到一張獨立的文章,連結如下: http://www.jianshu.com/p/4f28e1ae08b1

繼續閱讀