基于React + dumi + typescript 搭建自己的React组件库

# react+dumi+typescript搭建组件库开发环境


## 参考链接
- [concis从0到1搭建环境](正在上传…重新上传取消https://juejin.cn/post/7115414761646342152)
- [基于duim搭建组件库文档](https://juejin.cn/post/7057365237472690213)
- [dumi官网](https://d.umijs.org/zh-CN/config#menus)



## 搭建环境

### 安装
```
$ npx @umijs/create-dumi-lib     # 初始化一个文档模式的组件库开发脚手架

# or

$ yarn create @umijs/dumi-lib

$ npx @umijs/create-dumi-lib --site # 初始化一个站点模式的组件库开发脚手架

# or

$ yarn create @umijs/dumi-lib --site
```

### 运行

```
npm i 
npm run start
```


## 目录介绍
[请看链接](https://d.umijs.org/zh-CN/guide/basic)

## 修改默认配置

>你可以直接复制然后感受一下

.umirc.ts
```js
import { defineConfig } from 'dumi';
export default defineConfig({
  title: 'react-view-design', // 你的组件库名字
  // 你的网站 tabs 图片
  favicon: 'https://pro.upload.logomaker.com.cn/temp24h/logo/e4ed29e0-0890-479b-aba3-06f77f990361.svg',
  // 你的官网logo  
  logo: 'https://pro.upload.logomaker.com.cn/temp24h/logo/e4ed29e0-0890-479b-aba3-06f77f990361.svg',
  outputPath: 'docs-dist',
  mode: 'site',
  apiParser: {
    // 自定义属性过滤配置,也可以是一个函数,用法参考:https://github.com/styleguidist/react-docgen-typescript/#propfilter
    propFilter: {
      // 是否忽略从 node_modules 继承的属性,默认值为 false
      skipNodeModules: true,
      // 需要忽略的属性名列表,默认为空数组
      skipPropsWithName: ['autoFocus', 'form', 'formAction', 'formEncType', 'title'],
      // 是否忽略没有文档说明的属性,默认值为 false
      skipPropsWithoutDoc: true,
    },
  },
  history: {
    type: 'hash',
  },
  // 配置你想配置一级导航
  navs: [
    null,
    {
      title: '作者',
      children: [
        {
          title: 'CSDN',
          path: 'https://blog.csdn.net/CarrreyYan_979292?type=blog',
        },
        {
          title: '掘金',
          path: 'https://juejin.cn/user/1785262616087192',
        },
      ],
    },
    {
      title: 'GitHub',
      path: 'https://github.com/yq979292/Fluent-design0',
    },
  ],
  // styles: [style],
  themeConfig: {
    carrier: 'dumi', // 设备状态栏左侧的文本内容
    hd: true,
  },
});

```

>注意:这里如果启动页面失败,一定是运行环境问题,检查方式如下:
>1:看看node版本
>2:看看npm i 安装依赖是否成功。

>如果页面启动成功,如下图: 恭喜你搭建成功:
> - 请看链接 : https://d.umijs.org/zh-CN/guide#%E4%BB%80%E4%B9%88%E6%98%AF-dumi



## 第一个组件注意事项

> /src/Foo  为你的第一个组件
>    index.tsx    为组件的源代码
>    index.md    组件的说明文档


参考并复制一下代码,一起感受一下:

%%index.tsx%%
```tsx
import React from 'react';
export default ({ title }: { title: string }) => <h1>{title}</h1>;
```
index.md
```md
---
title: Foo
nav:
  title: 组件
  path: /commponnets
group:
  title: 通用
  mobile: false
---

## FOO
```

>注意事项:每次**创建新组件**应该修改`/src/index.ts`
>  例如:
>   export { default as Foo } from './Foo';



> 我的建议,大家可以阅读官网的基础使用,然后找一个参考说明文档格式,练习编写文档。
> 推荐参考文档如下:
> 1:Concis : http://concis.org.cn/#/common/modal
>       这是一个由个人完成的组件库,适合第一次写组件库的学习


下一篇文章:如何在dumi环境下支持scss语法。
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,针对您的问题,我可以给出以下的解答: 首先,在使用Ant Design的Tree组件时,我们可以通过`filterTreeNode`属性对树节点进行筛选,该属性接收一个函数作为参数,该函数的返回值为布尔类型,用于决定该节点是否被筛选出来。 接下来,我们可以使用React的函数组件来创建可搜索的树形控件。在函数组件中,我们可以通过useState钩子函数来维护搜索框的值,以及使用useMemo钩子函数来缓存筛选后的树节点数据。在筛选函数中,我们可以使用字符串的includes方法来进行模糊匹配,如果搜索框的值含有节点的名称,则该节点被筛选出来。 最后,我们可以使用TypeScript来为组件添加类型检查,从而提高代码的可维护性和可读性。 下面是一份示例代码,仅供参考: ```typescript import React, { useState, useMemo } from 'react'; import { Tree } from 'antd'; interface TreeNode { title: string; key: string; children?: TreeNode[]; } const data: TreeNode[] = [ { title: 'Parent 1', key: '0-0', children: [ { title: 'Child 1', key: '0-0-0', }, { title: 'Child 2', key: '0-0-1', }, ], }, { title: 'Parent 2', key: '0-1', children: [ { title: 'Child 3', key: '0-1-0', }, { title: 'Child 4', key: '0-1-1', }, ], }, ]; const SearchableTree: React.FC = () => { const [searchValue, setSearchValue] = useState<string>(''); const filteredData = useMemo(() => filterTreeData(data, searchValue), [searchValue]); const handleSearch = (e: React.ChangeEvent<HTMLInputElement>): void => { setSearchValue(e.target.value); }; return ( <div> <input type="text" value={searchValue} onChange={handleSearch} /> <Tree filterTreeNode={filterTreeNode}> {renderTreeNodes(filteredData)} </Tree> </div> ); }; const filterTreeData = (treeData: TreeNode[], searchValue: string): TreeNode[] => { if (!searchValue) { return treeData; } return treeData.reduce<TreeNode[]>((acc, node) => { const children = node.children && filterTreeData(node.children, searchValue); if (node.title.toLowerCase().includes(searchValue.toLowerCase()) || children?.length) { acc.push({ ...node, children, }); } return acc; }, []); }; const filterTreeNode = (node: TreeNode): boolean => { return !!node.children || node.title.toLowerCase().includes(searchValue.toLowerCase()); }; const renderTreeNodes = (treeData: TreeNode[]): React.ReactNode => { return treeData.map((node) => { if (node.children) { return ( <Tree.TreeNode key={node.key} title={node.title}> {renderTreeNodes(node.children)} </Tree.TreeNode> ); } return <Tree.TreeNode key={node.key} title={node.title} />; }); }; export default SearchableTree; ``` 希望能够对您有所帮助,如有疑问请随时追问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不羁的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值