自定义通讯录界面 UI

更新时间: 2024/05/10 14:03:40

如果默认通讯录界面(contact-kit)无法满足您的业务需求,您可通过本节介绍的参数进行界面自定义。

自定义参数概览

contact-kit提供的自定义参数如下:

参数
类型 说明 子组件
renderCustomContact ((contactType: ContactType) => Element) 自定义渲染通讯录内容,具体示例参见下文的自定义渲染通讯录内容 关系导航组件
prefix string 样式前缀,默认值为"contact"
onItemClick ((contactType: ContactType) => void) 通讯录导航点击事件
renderFriendListEmpty (() => Element) 自定义渲染好友列表为空时的内容 关系详情组件
renderFriendListHeader (() => Element) 自定义渲染好友列表头部内容,具体示例参见下文的自定义好友列表头部内容
renderBlackListEmpty (() => Element) 自定义渲染黑名单列表为空时的内容
renderBlackListHeader (() => Element) 自定义渲染黑名单列表头部内容
renderGroupListEmpty (() => Element) 自定义渲染群组列表为空时的内容
renderGroupListHeader (() => Element) 自定义渲染群组列表头部内容
renderMsgListEmpty (() => Element) 自定义渲染消息中心为空时的内容
renderMsgListHeader (() => Element) 自定义渲染消息中心头部内容
renderEmpty (() => Element) 自定义渲染通讯录为空时的内容
afterSendMsgClick (() => void) 点击发送消息后的事件,例如在同意好友申请后,可自定义渲染打招呼消息
onFriendItemClick ((account: string) => void) 点击好友的事件
onGroupItemClick ((team: Team) => void) 群组点击事件
onBlackItemClick ((account: string) => void) 黑名单点击事件
prefix string 样式前缀,默认值:"contact"
commonPrefix string 公共样式前缀,默认值:"common"

自定义示例

自定义通讯录内容

  • 查看在线示例

    IMAppProps 传入正确的 appkeyaccounttoken 即可在线预览效果。

  • 示例代码

    React
    const handleItemClick = (contactType) => {
      store.uiStore.selectContactType(contactType)
      if (contactType === 'msgList') {
        store.sysMsgStore.resetSystemMsgUnread()
      }
    }
    
    const renderCustomContact = (contactType) => {
      return contactType == 'msgList' ? (
        <div className='msg-list-wrapper' onClick={() => handleItemClick(contactType)}>
          <img className='msg-list' src="https://yx-web-nosdn.netease.im/common/67be494eeca64db51d2013cc682b0f93/75573bd7b485f23.jpeg" alt="" />
          <div className='msg-list-text'>消息中心</div>
        </div>
      ) : null
    }
    
    <ContactListContainer renderCustomContact={renderCustomContact}/>
    
    Vue
    <template>
        <div ref="contactList" />
    </template>
    
    <script>
        ...
          mounted() {
              this.$uikit.render(
              ContactListContainer,
              {
              renderCustomContact:(contactType) => {
                    console.log('==========renderCustomContact===========', contactType);
                    const handleItemClick = (contactType) => {
                      const { store, nim } = window.__xkit_store__
                      store.uiStore.selectContactType(contactType)
                      if (contactType === 'msgList') {
                        store.sysMsgStore.resetSystemMsgUnread()
                      }
                    }
                    if( contactType === 'msgList'){
                      return compile(`<div className='msg-list-wrapper' onClick={() => context.handleItemClick(context.contactType)}>
                        <div className='msg-list-text'>消息中心</div>
                      </div>`, { handleItemClick, contactType })
                    }else{
                      return null
                    }
                  }
              },
              this.$refs.contactList
            );
          }
    </script>
    
  • 示例代码解读

    上述示例中的 contactType 可获取到四种类型,分别为 'blackList'(黑名单) | 'groupList'(我的群组)| 'friendList'(我的好友) | 'msgList'(消息中心), 可根据不同 contactType 自定义渲染逻辑,若 renderCustomContact 返回为 null,则使用组件默认渲染逻辑。

  • 效果比对

    默认
    自定义后
    通讯录内容.png 通讯录内容自定义1.png

在集成通讯录组件时,如果不需要展示群组,您可在 contactType 为 'groupList' 时,return 一个空 div 即可,其余 contactType return null, 使用默认渲染逻辑。

React
<ContactListContainer
      renderCustomContact={(contactType) => {
        console.log(
          "==========renderCustomContact===========",
          contactType
        );
        if (contactType === "groupList") {
          return <div></div>;
        } else {
          return null;
        }
      }}
  />
Vue
<template>
     <div ref="contactList" />
</template>

<script>
     ...
      mounted() {
          this.$uikit.render(
          ContactListContainer,
          {
            renderCustomContact: (contactType) => {
                console.log('==========renderCustomContact===========', contactType);
                if( contactType === 'groupList'){
                  return compile(`<div></div>`)
                }else{
                  return null
                }
              }
          },
          this.$refs.contactList
        );
      }
</script>

自定义好友列表头部内容

  • 查看在线示例

    IMAppProps 传入正确的 appkeyaccounttoken 即可在线预览效果。

  • 示例代码

    React
    const renderFriendListHeader = () => {
      return (
        <div className='friend-header'>
          <img className='friend' src="https://yx-web-nosdn.netease.im/common/71a3ab830b7c8171580dad6ac2885fd6/邀请好友.png" alt="" />
          <div className='friend-text'>我的好友</div>
        </div>
      )
    }
    
    <ContactInfoContainer
        renderFriendListHeader={renderFriendListHeader}
      />
    
    Vue
    <template>
        <div ref="contactInfo" />
    </template>
    
    <script>
        ...
          mounted() {
              this.$uikit.render(
              ContactInfoContainer,
              {
                renderFriendListHeader: () => {
                  return compile(
                    `<div style={{ display: 'flex' }}>
                      <img src="https://yx-web-nosdn.netease.im/common/71a3ab830b7c8171580dad6ac2885fd6/邀请好友.png" alt="" />
                      <div>我的好友</div>
                    </div>`
                  )
                }
              },
              this.$refs.contactInfo
            );
          }
    </script>
    

    renderBlackListHeaderrenderGroupListHeaderrenderMsgListHeader 同理。

  • 效果比对

    默认
    自定义后
    好友列表头部.png 好友列表头部内容自定义后1.png

自定义打招呼消息

  • 查看在线示例

    IMAppProps 传入正确的 appkeyaccounttoken 即可在线预览效果。

  • 示例代码

    React
    <ContactInfoContainer
          //...
          afterAcceptApplyFriend={(account) => {
            store.msgStore
              .sendTextMsgActive({
                scene: 'p2p',
                to: account,
                body: t('passFriendAskText'),
              })
              .then(() => {
                setModel('chat')
              })
          }}
        />
    
    Vue
    <template>
        <div ref="contactInfo" />
    </template>
    
    <script>
        ...
          mounted() {
              this.$uikit.render(
              ContactInfoContainer,
              {
                afterAcceptApplyFriend: (account) => {
                  const { store, nim } = window.__xkit_store__
                  store.msgStore
                    .sendTextMsgActive({
                      scene: 'p2p',
                      to: account,
                      body: '我已经同意了你的申请,现在开始聊天吧~',
                    })
                    .then(() => {
                      this.model = "chat";
                })
              },
              this.$refs.contactInfo
            );
          }
    </script>
    
  • 效果比对

    默认
    自定义后
    web无打招呼.png web打招呼.png

相关图例

本节介绍通讯录界面的部分默认展示内容。

好友列表为空时的内容

黑名单列表为空时的内容

黑名单列表头部内容

群组列表为空时的内容

群组列表头部内容

消息中心为空时的内容

消息中心头部内容

通讯录为空时的内容

此文档是否对你有帮助?
有帮助
去反馈
  • 自定义参数概览
  • 自定义示例
  • 自定义通讯录内容
  • 自定义好友列表头部内容
  • 自定义打招呼消息
  • 相关图例
  • 好友列表为空时的内容
  • 黑名单列表为空时的内容
  • 黑名单列表头部内容
  • 群组列表为空时的内容
  • 群组列表头部内容
  • 消息中心为空时的内容
  • 消息中心头部内容
  • 通讯录为空时的内容