代碼總結(jié)-1

代碼規(guī)范

案例

多選組件的實(shí)現(xiàn)

UI樣式

傳參

CommonSelector.propTypes = {
  // 標(biāo)題
  headerTitle: PropTypes.string,
  // 是否展示全選
  showSelectAll: PropTypes.bool,
  // 是否需要展開收起
  showExpandAndCollapse: PropTypes.bool,
  sliceIndex: PropTypes.number,
  // 選項(xiàng)列表
  dataList: PropTypes.array,
  // 已選列表
  selectedList: PropTypes.array,
  // 選項(xiàng)更改方法
  onSelectChange: PropTypes.func
}

樣式

<View className="filter-item-wrap">
  {showSelectAll && (
    <Text
      className={`filter-item ${selectedList.length === dataList.length ? 'chosen' : ''}`}
      onClick={toggleSelectAll}>
      全部
    </Text>
  )}
  {!showExpandAndCollapse && (
    <Block>
      {dataList.map(item => {
        return (
          <Text
            className={`filter-item ${selectedList.includes(item.value) ? 'chosen' : ''}`}
            key={item.value}
            onClick={(e) => {handleSelectChange(e)}}
            data-value={item.value}
          >
            {item.label}
          </Text>
        )
      })}
    </Block>
  )}
</View>

邏輯處理

// 點(diǎn)擊全部
function toggleSelectAll() {
  let list = []
  if (selectedList.length === dataList.length) {
    list = []
  } else {
    list = dataList.map(item => item.value)
  }
  onSelectChange(list)
}
// 點(diǎn)擊子項(xiàng)
function handleSelectChange(e) {
  const {value} = e.currentTarget.dataset
  if (selectedList.includes(value)) {
    selectedList = selectedList.filter(item => item !== value)
  } else {
    selectedList.push(value)
  }
  onSelectChange(selectedList)
}

外部使用

// 變量
purchaseIntentionList: [  // 潛力客戶選項(xiàng)
  { value: 'A1', label: 'A1' },
  { value: 'A2', label: 'A2' },
  { value: 'A3', label: 'A3' },
  { value: 'B1', label: 'B1' },
  { value: 'B2', label: 'B2' },
  { value: 'C', label: 'C' },
  { value: '', label: '未分類' }
],
chooseList: [] // 選擇的潛力標(biāo)簽

// change function
handlePotentialLabelSelect(selectedList) {
  this.setState({
    chooseList: selectedList
  })
  this.restartRequest()
}

// 組件傳值
{/* 潛力客戶篩選 */}
<CommonSelector
  headerTitle="潛力客戶"
  showExpandAndCollapse={true}
  sliceIndex="3"
  dataList={this.state.purchaseIntentionList}
  selectedList={this.state.chooseList}
  onSelectChange={this.handlePotentialLabelSelect.bind(this)}>
</CommonSelector>

思考:“全部”這個(gè)選項(xiàng)是否應(yīng)該作為選項(xiàng)由父組件傳入旅赢,還是直接放入子組件中钞楼,作為特殊處理蜈亩?

purchaseIntentionList: [
  { label: '全部', value: null },
  ...
]

樣式切換方式

常見場景:選中狀態(tài)與非選中狀態(tài)

1谅辣、使用模板字符串

<Text
  className={`filter-item ${selectedList.length === dataList.length ? 'chosen' : ''}`}
  onClick={toggleSelectAll}>
  全部
</Text>

2修赞、使用classnames

<Button
  className={classNames(
    'save-button-item',
    currentSelect ? '' : 'opacity-save-button'
  )}
  onClick={this.confirmTransferDealerOfEnquiry.bind(this)}
>
  確定
</Button>

思考:是否可以使用模板字符串,或者有什么其他更好地方法?

插件的使用的取舍:自己封裝or直接用相關(guān)插件

案例:時(shí)間日期處理
日期方法

插件:moment.js

公共方法的拆分的維度

// 根據(jù)不同的版本設(shè)置不同的顏色
addSaasVersionColor() {
  const { garageList } = this.state
  if (garageList.length > 0) {
    garageList.forEach(item => {
      const color = saasVersionColorMap[item.packageType]
      if (color) {
        item.textColor = color.textColor
        item.bgColor = color.bgColor
      }
    })
  }
  this.setState({
    garageList
  })
}

節(jié)點(diǎn)

<View
  className="version-show"
  style={{color: garage.textColor, background: garage.bgColor}}
>
  {versionTypeMap[`${garage.buyFlag}-${garage.packageType}`]}
</View>
UI樣式

組件封裝的標(biāo)準(zhǔn)柏副?復(fù)用性的判定

state 的粒度問題

class components extends Components {
  constructor(props) {
    super(props)
    this.state = {
      // 省市區(qū)相關(guān)
      areaState: {
        districtCodes: [],
        pageAreaData: [],
        firstIndex: 0,
        secondIndex: 0,
        showArea: false
      }
    }
  }
}
class components extends Components {
  constructor(props) {
    super(props)
    this.state = {
      // 省市區(qū)相關(guān)
      districtCodes: [],
      pageAreaData: [],
      firstIndex: 0,
      secondIndex: 0,
      showArea: false
    }
  }
}

// 接收后端接口數(shù)據(jù)

class components extends Components {
  constructor(props) {
    super(props)
    this.state = {
      // 省市區(qū)相關(guān)
      garageTotalCount: 0,  // 數(shù)量
      purchaseTotalAmount: 0, // 成交金額合計(jì)
      purchaseCommissionTotalAmount: 0, // 抽傭合計(jì)
      purchaseCommissionGrossTotalAmount: 0 // 抽傭毛利合計(jì)
    }
  }
}
async getGarageRanking() {
  const {
    pageNum,
    pageSize
  } = this.state
  const requestData = this.formatRequestData()
  const rankRes = await IncomeApi.getGaragePage(requestData, {pageNum, pageSize})
  if (rankRes.success && rankRes.data) {
    this.setState({
      garageTotalCount: rankRes.data.totalCount || 0,
      purchaseTotalAmount: rankRes.data.purchaseTotalAmount || 0,
      purchaseCommissionTotalAmount: rankRes.data.purchaseCommissionTotalAmount || 0,
      purchaseCommissionGrossTotalAmount: rankRes.data.purchaseCommissionGrossTotalAmount || 0
    })
  }
}

思考:是直接用一個(gè)data對(duì)象保存所有的數(shù)據(jù)勾邦,還是單獨(dú)保存每一項(xiàng)目

頁面數(shù)據(jù)默認(rèn)展示值的處理

<View className="purchase-info">
  <View className="info-item">{ (dealer.purchaseCount || 0) + '單'}</View>
  <View className="line"></View>
  <View className="info-item">¥{dealer.purchaseTotalAmount || 0}</View>
  <View className="line"></View>
  <View className="info-item">¥{(dealer.purchaseCommissionTotalAmount || 0 )}</View>
</View>

長函數(shù)

// 處理配件展示數(shù)據(jù)
handleQuotationDetail(quotationDetail) {
  if(quotationDetail.oldApp==='1'){
    //oldApp=1證明是老版本app,需要移除掉serviceQuotationProductGroupList
      delete quotationDetail["serviceQuotationProductGroupList"]
  }
  if (
    quotationDetail.serviceQuotationProductGroupList &&
    quotationDetail.serviceQuotationProductGroupList.length > 0
  ) {
    let partListFormat = [];
    for (
      let index = 0;
      index < quotationDetail.serviceQuotationProductGroupList.length;
      index++
    ) {
      const element = quotationDetail.serviceQuotationProductGroupList[index];
      if (element.productList.length == 1) {
        let item = element.productList[0];
        partListFormat.push({
          ...item,
          productList: element.productList
        });
      } else {
        let recommendPart =null
      if(element.showTips){
        recommendPart=element.productList.find(ele => ele.selectType) || null;
      }else{
        recommendPart=element.productList.find(ele => ele.recommendType) || null;
      }
      if(element.showTips){
        recommendPart.showTips=element.showTips 
      }
      partListFormat.push({
        ...recommendPart,
        productList: element.productList
      });
      }
    }
    quotationDetail.serviceQuotationProductList = partListFormat;
    //是否擁有不同品質(zhì)配件
    quotationDetail.haveDifferentQualitiesPart = quotationDetail.serviceQuotationProductList.some(
      ele => ele.productList.length > 1
    );
  }
  //設(shè)置了滿減優(yōu)惠 滿足門檻將discountsUseAmount復(fù)制為discountsAmount
  quotationDetail.discountsAmount = this.calculateDiscount(quotationDetail);
  return quotationDetail;
  }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市割择,隨后出現(xiàn)的幾起案子眷篇,更是在濱河造成了極大的恐慌,老刑警劉巖荔泳,帶你破解...
    沈念sama閱讀 211,194評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蕉饼,死亡現(xiàn)場離奇詭異,居然都是意外死亡玛歌,警方通過查閱死者的電腦和手機(jī)昧港,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,058評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來支子,“玉大人创肥,你說我怎么就攤上這事≈蹬螅” “怎么了叹侄?”我有些...
    開封第一講書人閱讀 156,780評(píng)論 0 346
  • 文/不壞的土叔 我叫張陵,是天一觀的道長昨登。 經(jīng)常有香客問我趾代,道長,這世上最難降的妖魔是什么篙骡? 我笑而不...
    開封第一講書人閱讀 56,388評(píng)論 1 283
  • 正文 為了忘掉前任稽坤,我火速辦了婚禮,結(jié)果婚禮上糯俗,老公的妹妹穿的比我還像新娘尿褪。我一直安慰自己,他們只是感情好得湘,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,430評(píng)論 5 384
  • 文/花漫 我一把揭開白布杖玲。 她就那樣靜靜地躺著,像睡著了一般淘正。 火紅的嫁衣襯著肌膚如雪摆马。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,764評(píng)論 1 290
  • 那天鸿吆,我揣著相機(jī)與錄音囤采,去河邊找鬼。 笑死惩淳,一個(gè)胖子當(dāng)著我的面吹牛蕉毯,可吹牛的內(nèi)容都是我干的乓搬。 我是一名探鬼主播,決...
    沈念sama閱讀 38,907評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼代虾,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼进肯!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起棉磨,我...
    開封第一講書人閱讀 37,679評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤江掩,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后乘瓤,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體环形,經(jīng)...
    沈念sama閱讀 44,122評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,459評(píng)論 2 325
  • 正文 我和宋清朗相戀三年馅扣,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了斟赚。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,605評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡差油,死狀恐怖拗军,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蓄喇,我是刑警寧澤发侵,帶...
    沈念sama閱讀 34,270評(píng)論 4 329
  • 正文 年R本政府宣布,位于F島的核電站妆偏,受9級(jí)特大地震影響刃鳄,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜钱骂,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,867評(píng)論 3 312
  • 文/蒙蒙 一叔锐、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧见秽,春花似錦愉烙、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,734評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至禀苦,卻和暖如春蔓肯,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背振乏。 一陣腳步聲響...
    開封第一講書人閱讀 31,961評(píng)論 1 265
  • 我被黑心中介騙來泰國打工蔗包, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人慧邮。 一個(gè)月前我還...
    沈念sama閱讀 46,297評(píng)論 2 360
  • 正文 我出身青樓调限,卻偏偏與公主長得像邻储,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子旧噪,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,472評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容