成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

《精通react/vue組件設計》之實現一個健壯的警告提示(Alert)組件

開發 前端
對于react選手來說,如果沒用typescript,建議大家都用PropTypes, 它是react內置的類型檢測工具,我們可以直接在項目中導入. vue有自帶的屬性檢測方式,這里就不一一介紹了。

前言

本文是筆者寫組件設計的第七篇文章, 今天帶大家實現一個自帶主題且可關閉的Alert組件, 該組件在諸如Antd或者elementUI等第三方組件庫中都會出現,主要用來提供系統的用戶反饋.

之所以會寫組件設計相關的文章,是因為作為一名前端優秀的前端工程師,面對各種繁瑣而重復的工作,我們不應該按部就班的去"辛勤勞動",而是要根據已有前端的開發經驗,總結出一套自己的高效開發的方法.

前端組件一般會劃分為如下幾種類型:

  • 通用型組件: 比如Button, Icon等.
  • 布局型組件: 比如Grid, Layout布局等.
  • 導航型組件: 比如面包屑Breadcrumb, 下拉菜單Dropdown, 菜單Menu等.
  • 數據錄入型組件: 比如form表單, Switch開關, Upload文件上傳等.
  • 數據展示型組件: 比如Avator頭像, Table表格, List列表等.
  • 反饋型組件: 比如Progress進度條, Drawer抽屜, Modal對話框等.
  • 其他業務類型

所以我們在設計組件系統的時候可以參考如上分類去設計,該分類也是antd, element, zend等主流UI庫的分類方式.

正文

在開始組件設計之前希望大家對css3和js有一定的基礎,并了解基本的react/vue語法.我們先看看實現后的組件效果:

圖片圖片

1. 組件設計思路

按照之前筆者總結的組件設計原則,我們第一步是要確認需求. 一個警告提示(Alert)組件會有如下需求點:

  • 能控制Alert組件的樣式
  • 能控制Alert組件的關閉按鈕是否顯示
  • 用戶可以自己輸入提示內容
  • 能控制關閉按鈕的文本,或者自定義關閉按鈕
  • 支持顯示提示內容的輔助文本
  • 內置提供不同類型的警告提示樣式,比如成功, 錯誤, 警告等
  • 關閉提示時能提供自定義事件

需求收集好之后,作為一個有追求的程序員, 會得出如下線框圖:

圖片圖片

對于react選手來說,如果沒用typescript,建議大家都用PropTypes, 它是react內置的類型檢測工具,我們可以直接在項目中導入. vue有自帶的屬性檢測方式,這里就不一一介紹了.

通過以上需求分析, 我們發現實現一個Alert非常簡單, 它屬于反饋型組件,所以不會涉及到太多功能.接下來我們就來看看具體實現.

2. 基于react實現一個Alert組件

2.1. Alert組件框架設計

首先我們先根據需求將組件框架寫好,這樣后面寫業務邏輯會更清晰:

import classnames from 'classnames'
import styles from './index.less'


/**
 * 警告提示組件
 * @param {style} object 更改Alert樣式
 * @param {closable} bool 是否顯示關閉按鈕, 默認不顯示
 * @param {closeText} string|reactNode 自定義關閉按鈕
 * @param {message} string 警告提示內容
 * @param {description} string 警告提示的輔助性文字
 * @param {type} string 警告的類型
 * @param {onClose} func 關閉時觸發的事件
 */
function Alert(props) {
  const {
    style,
    closable,
    closeText,
    message,
    description,
    type,
    onClose
  } = props


  return <div className={styles.xAlertWrap}>
          <div className={styles.alertMes}>{ message }</div>
          <div className={styles.alertDesc}>{ description }</div>
          <span className={styles.closeBtn}>{ closeText ? closeText : 'x' }</span>
         </div>
}


export default Alert

有了這個框架,我們就來往里面實現內容吧.

2.2 實現style,closeText,message, description,type

這幾個功能在框架搭建好之后已經部分實現了,是因為他們都比較簡單,不會牽扯到其他復雜邏輯.只需要對外暴露屬性并使用屬性即可. 具體實現如下:

function Alert(props) {
  const {
    style,
    closable,
    closeText,
    message,
    description,
    type,
    onClose
  } = props


  return <div 
      className={classnames(styles.xAlertWrap, styles[type] || styles.warning)}
      style={{
        ...style
      }}
    >
      <div className={styles.alertMes}>{ message }</div>
      <div className={styles.alertDesc}>{ description }</div>
      <span className={styles.closeBtn}>{ closeText ? closeText : 'x' }</span>
    </div>
}

以上代碼可以發現筆者采用了classnames這個第三方工具, 他可以組合我們的class以實現更靈活的配置. 對于type的實現,我的思路是提前預制好幾種類型樣式, 通過用戶手動配置來匹配到對應的樣式:

.xAlertWrap {
  box-sizing: border-box;
  position: relative;
  padding: 5px 12px;
  margin-bottom: 16px;
  border-radius: 3px;
  &.success {
    background-color: #f6ffed;
    border: 1px solid #b7eb8f;
  }
  &.info {
    background-color: #e6f7ff;
    border: 1px solid #91d5ff;
  }
  &.error {
    background-color: #fffbe6;
    border: 1px solid #ffe58f;
  }
  &.warning {
    background-color: #fff1f0;
    border: 1px solid #ffa39e;
  }
}

2.3 實現closable和onClose

closable主要是用來讓用戶能手動關閉Alert,onClose是對外暴露的關閉時的方法, 因為沒必要也不需要向外暴露屬性來讓Alert關閉, 所以最好的方式是在組件內部實現, 我們會通過useState這個鉤子來處理,代碼如下:

function Alert(props) {
  const {
    style,
    closable,
    closeText,
    message,
    description,
    type,
    onClose
  } = props
  let [visible, setVisible] = useState(true)


  const handleColse = () => {
    setVisible(false)
    onClose && onClose()
  }
  return visible ? 
    <div 
      className={classnames(styles.xAlertWrap, styles[type] || styles.warning)}
      style={{
        opacity: visible ? '1' : '0',
        ...style
      }}
    >
      <div className={styles.alertMes}>{ message }</div>
      <div className={styles.alertDesc}>{ description }</div>
      {
        !!closable && <span className={styles.closeBtn} notallow={handleColse}>{ closeText ? closeText : 'x' }</span>
      }
    </div> : null
}

通過控制visible來控制Alert的出現和消失, 并且當點擊關閉按鈕時能調用外部暴露的onClose方法.

2.4 健壯性支持, 我們采用react提供的propTypes工具:

import PropTypes from 'prop-types'
// ...
Alert.propTypes = {
  style: PropTypes.object,
  closable: PropTypes.bool,
  closeText: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.element
  ]),
  message: PropTypes.string,
  description: PropTypes.string,
  type: PropTypes.string,
  onClose: PropTypes.func
}

關于prop-types的使用官網上有很詳細的案例,這里說一點就是oneOfType的用法, 它用來支持一個組件可能是多種類型中的一個.  組件完整css代碼如下:

.xAlertWrap {
  box-sizing: border-box;
  position: relative;
  padding: 5px 12px;
  margin-bottom: 16px;
  border-radius: 3px;
  &.success {
    background-color: #f6ffed;
    border: 1px solid #b7eb8f;
  }
  &.info {
    background-color: #e6f7ff;
    border: 1px solid #91d5ff;
  }
  &.error {
    background-color: #fffbe6;
    border: 1px solid #ffe58f;
  }
  &.warning {
    background-color: #fff1f0;
    border: 1px solid #ffa39e;
  }


  .alertMes {
    margin-bottom:5px;
    color: rgba(0, 0, 0, 0.85);
    font-size: 14px;
    line-height: 1.5em;
  }
  .alertDesc {
    color: rgba(0, 0, 0, 0.65);
    font-size: 14px;
    line-height: 1.5em;
    word-break: break-all;
  }
  .closeBtn {
    position: absolute;
    right: 8px;
    top: 5px;
    color: rgba(0, 0, 0, 0.4);
    cursor: pointer;
  }
}

通過以上步驟, 一個健壯的的Alert組件就完成了,關于代碼中的css module和classnames的使用大家可以自己去官網學習,非常簡單.如果不懂的可以在趣談前端技術群里提問,筆者看到后會第一時間解答.

2.5 使用Alert組件

我們可以通過如下方式使用它:

<Alert message="溫馨提示,你忘帶口罩了" />
<Alert message="溫馨提示,你注冊成功" type="success" />
<Alert message="錯誤提示,你沒洗手了" type="error" />
<Alert message="提示: 我們開始吧" type="info" />
<Alert message="提示: 我可以關閉了" type="info" closable notallow={() => { alert(111) }} /><Alert message="注冊成功" descriptinotallow="你在本網站已經注冊成功,謝謝您的支持和反饋,多交流真正的技術吧" closable type="success" />

筆者已經將實現過的組件發布到npm上了,大家如果感興趣可以直接用npm安裝后使用,方式如下:

npm i @alex_xu/xui
// 導入xui
import {
  Button,
  Skeleton,
  Empty,
  Progress,
  Tag,
  Switch,
  Drawer,
  Badge,
  Alert
} from '@alex_xu/xui'

該組件庫支持按需導入,我們只需要在項目里配置babel-plugin-import即可,具體配置如下:

// .babelrc
"plugins": [
  ["import", { "libraryName": "@alex_xu/xui", "style": true }]
]

npm庫截圖如下:

圖片圖片

最后

之前筆者已經實現了:

  • modal(模態窗),
  • badge(徽標),
  • table(表格),
  • tooltip(工具提示條),
  • Skeleton(骨架屏),
  • Message(全局提示),
  • form(form表單),
  • switch(開關),
  • 日期/日歷,
  • 二維碼識別器組件

等組件, 來復盤筆者多年的組件化之旅.


責任編輯:武曉燕 來源: 趣談前端
相關推薦

2023-04-28 09:30:40

vuereact

2021-03-31 08:01:24

React Portareactcss3

2023-05-17 10:05:35

組件設計(Modal)組件

2018-09-18 10:11:21

前端vue.jsjavascript

2021-06-21 15:49:39

React動效組件

2021-10-17 20:37:44

組件DrawerReact

2009-08-20 10:12:59

Flex Alert組

2022-09-20 11:00:14

Vue3滾動組件

2023-12-21 10:26:30

??Prettier

2022-05-13 08:48:50

React組件TypeScrip

2021-01-28 06:11:40

導航組件Sidenav Javascript

2024-04-01 11:52:46

2022-04-26 05:55:06

Vue.js異步組件

2024-03-20 09:31:00

圖片懶加載性能優化React

2022-04-25 07:36:21

組件數據函數

2020-12-15 08:58:07

Vue編輯器vue-cli

2022-09-07 16:07:17

前端React

2024-07-29 00:02:00

DemoVue開發

2018-01-31 15:45:07

前端Vue.js組件

2022-03-10 09:00:37

提醒框ReactVue
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 久久久精彩视频 | 午夜精品一区二区三区在线观看 | 精品国产青草久久久久96 | 一区二区精品视频 | 在线亚洲一区二区 | 99精品亚洲国产精品久久不卡 | 欧美激情综合 | 亚洲国产精品一区二区久久 | 香蕉视频在线播放 | 亚洲风情在线观看 | 日韩毛片 | 亚洲人人 | 国产a级毛片 | 午夜欧美a级理论片915影院 | 国产乱码久久久久久一区二区 | 欧洲妇女成人淫片aaa视频 | 国产视频二区在线观看 | 青青久久av北条麻妃海外网 | 久久国产精品一区二区 | 人人看人人干 | 日日爱夜夜操 | 一区二区三区精品视频 | 国产视频不卡一区 | 国产日韩久久久久69影院 | 91精品国产综合久久婷婷香蕉 | 一本一道久久a久久精品综合蜜臀 | 黄色欧美大片 | 成人久久久 | 久久久久久综合 | 一级毛片视频 | 欧美影院 | 日日干夜夜操 | 亚洲男女视频在线观看 | 成人亚洲性情网站www在线观看 | 亚洲高清在线 | 国产乱一区二区三区视频 | 成人在线免费观看 | 日韩二| 国产97在线视频 | 婷婷免费在线 | 综合欧美亚洲 |