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

木子昭發表於2018-07-12
  • 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