Skip to content

Commit

Permalink
feat(react): ✨ use NutUI-React Taro
Browse files Browse the repository at this point in the history
  • Loading branch information
anyesu committed Feb 20, 2024
1 parent 0652ab7 commit a3a05be
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 11 deletions.
12 changes: 12 additions & 0 deletions packages/taro-demo-react/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,16 @@ module.exports = {
},
],
],
plugins: [
[
'import',
{
libraryName: '@nutui/nutui-react-taro',
libraryDirectory: 'dist/esm',
style: false, // 样式不需要按需引入,避免优先级问题
camel2DashComponentName: false,
},
'nutui-react-taro',
],
],
};
16 changes: 13 additions & 3 deletions packages/taro-demo-react/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import prodConfig from './prod';
export default defineConfig(async (merge) => {
const baseConfig: UserConfigExport = {
projectName: 'taro-demo-react',
designWidth: 750,
designWidth: 375, // 全局使用 NutUI 的 375 尺寸
deviceRatio: {
640: 2.34 / 2,
750: 1,
Expand All @@ -16,14 +16,24 @@ export default defineConfig(async (merge) => {
},
sourceRoot: 'src',
outputRoot: 'dist',
plugins: [],
plugins: [
'@tarojs/plugin-html', // 支持 HTML 标签
],
defineConstants: {},
copy: {
patterns: [],
options: {},
},
framework: 'react',
compiler: 'webpack5',
compiler: {
type: 'webpack5',
// 仅 webpack5 支持依赖预编译配置
prebundle: {
// PreBundle 与 @nutui/nutui-react-taro 不兼容
// - https://nutui.jd.com/taro/react/2x/#/zh-CN/guide/start-react#3%E3%80%81%E5%B0%8F%E7%A8%8B%E5%BA%8F%E9%A1%B9%E7%9B%AE%E8%BF%90%E8%A1%8C%E6%97%B6%E5%87%BA%E7%8E%B0%E3%80%8C%E6%89%BE%E4%B8%8D%E5%88%B0%E6%A8%A1%E6%9D%BF%E3%80%8D%E7%9A%84%E9%94%99%E8%AF%AF%E6%8F%90%E7%A4%BA
exclude: ['@nutui/nutui-react-taro', '@nutui/icons-react-taro'],
},
},
cache: {
enable: false, // Webpack 持久化缓存配置,建议开启。默认配置请参考:https://docs.taro.zone/docs/config-detail#cache
},
Expand Down
4 changes: 4 additions & 0 deletions packages/taro-demo-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@
],
"dependencies": {
"@babel/runtime": "7.24.4",
"@nutui/icons-react-taro": "1.0.4",
"@nutui/nutui-react-taro": "2.6.0",
"@tarojs/components": "3.6.25",
"@tarojs/helper": "3.6.25",
"@tarojs/plugin-framework-react": "3.6.25",
"@tarojs/plugin-html": "3.6.25",
"@tarojs/plugin-platform-alipay": "3.6.25",
"@tarojs/plugin-platform-h5": "3.6.25",
"@tarojs/plugin-platform-harmony-hybrid": "3.6.25",
Expand All @@ -64,6 +67,7 @@
"@types/jest": "29.5.12",
"@types/react": "18.2.74",
"@types/webpack-env": "1.18.4",
"babel-plugin-import": "1.13.8",
"babel-preset-taro": "3.6.25",
"eslint-config-taro": "3.6.25",
"eslint-plugin-import": "2.12.0",
Expand Down
1 change: 1 addition & 0 deletions packages/taro-demo-react/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useLaunch } from '@tarojs/taro';
import type { PropsWithChildren } from 'react';
import '@nutui/nutui-react-taro/dist/style.css';
import './app.scss';

function App({ children }: PropsWithChildren<any>) {
Expand Down
3 changes: 3 additions & 0 deletions packages/taro-demo-react/src/pages/index/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ export interface NavGroupConfig extends NavConfig {
children: NavConfig[];
}

export const NUT_UI_LOGO =
'https://img14.360buyimg.com/imagetools/jfs/t1/117879/25/28831/6279/6329723bE66715a2f/5f099b8feca9e8cc.png';

export const navs: NavGroupConfig[] = [];
40 changes: 40 additions & 0 deletions packages/taro-demo-react/src/pages/index/index.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
.index {
height: 100%;
width: 100%;

&__header {
display: flex;
align-items: center;
padding: 0 34px;
height: 117px;
> image {
width: 67px;
height: 67px;
margin-right: 18px;
flex-shrink: 0;
}
&__info {
display: flex;
flex-direction: column;
h1 {
height: 48px;
line-height: 48px;
font-size: 34px;
color: rgb(51 51 51 / 100%);
font-weight: 500;
}
p {
height: 18px;
line-height: 18px;
font-size: 12px;
color: rgb(154 155 157 / 100%);
}
}
}
&__navs {
background: #f7f8fa;
border-radius: 30px 30px 0 0;
overflow: hidden;
padding: 30px 25px;
}
}
36 changes: 28 additions & 8 deletions packages/taro-demo-react/src/pages/index/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
import { Text, View } from '@tarojs/components';
import { useLoad } from '@tarojs/taro';
import { Image, View } from '@tarojs/components';
import Taro from '@tarojs/taro';
import { ArrowRight } from '@nutui/icons-react-taro';
import { Cell, CellGroup } from '@nutui/nutui-react-taro';
import { navs, NUT_UI_LOGO } from './constants';
import styles from './index.module.scss';

export default function Index() {
useLoad(() => {
console.log('Page loaded.');
});
async function navigateTo(group: string, path: string) {
await Taro.navigateTo({ url: `/pages/${group}/${path}` });
}

export default function Index() {
return (
<View className="index">
<Text>Hello world!</Text>
<View className={styles.index}>
<View className={styles.index__header}>
<Image src={NUT_UI_LOGO} />
<View className={styles.index__header__info}>
<h1>NutUI React</h1>
<p>京东风格的轻量级小程序组件库 React 版</p>
</View>
</View>
<View className={styles.index__navs}>
{navs.map((group) => (
<CellGroup key={group.path} title={group.name}>
{group.children.map(({ name, path }) => {
const onClick = () => navigateTo(group.path, path);
return <Cell key={path} title={name} extra={<ArrowRight />} onClick={onClick} />;
})}
</CellGroup>
))}
</View>
</View>
);
}

0 comments on commit a3a05be

Please sign in to comment.