Skip to content

Commit

Permalink
feat: 添加基于weui的组件库
Browse files Browse the repository at this point in the history
  • Loading branch information
三少 committed Aug 11, 2021
1 parent d5e921e commit a387d3f
Show file tree
Hide file tree
Showing 91 changed files with 6,270 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/weui/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
*.log
*.lock
*.swp

.github
.cache
.temp
.idea
.rn_temp
.DS_Store

CHANGELOG.md
package-lock.json

src
node_modules
coverage
19 changes: 19 additions & 0 deletions packages/weui/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# @antmjs/weui

> 一套适用于Taro3.3的weui组件库
## 为什么需要

组件库,避免每次都要重新造一遍轮子

## 安装

```bash
yarn add @antmjs/weui
```

## 使用

```js
import { Button } from '@antmjs/weui'
```
64 changes: 64 additions & 0 deletions packages/weui/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"name": "@antmjs/weui",
"version": "1.0.1",
"main": "dist/index.js",
"module": "dist/index.esm.js",
"typings": "types/index.d.ts",
"author": "三少 <hi_sanshao@outlook.com>",
"description": "一套适用于Taro3.3的weui组件库",
"license": "MIT",
"publishConfig": {
"access": "public",
"registry": "http://registry.npmjs.org"
},
"keywords": [
"taro",
"weui"
],
"repository": {
"type": "https",
"url": "https://github.com/AntmJS/antm.git"
},
"bugs": {
"url": "https://github.com/AntmJS/antm/issues/new"
},
"engines": {
"node": ">=12",
"npm": ">=6.4",
"yarn": ">=1.22"
},
"browserslist": [
"Chrome >= 35",
"ChromeAndroid >= 35",
"iOS >= 8",
"Safari >= 8",
"Android >= 4.1",
"QQAndroid >= 4.1",
"UCAndroid >= 4.1"
],
"scripts": {
"_clean": "npx rimraf dist",
"_real": "npx rollup -c ./rollup.config.js",
"watch": "npx rollup -c ./rollup.watch.config.js -w",
"build": "npx run-s _clean _real",
"test:watch": "",
"test": ""
},
"peerDependencies": {
"@tarojs/components": ">=3.3.0",
"@tarojs/taro": ">=3.3.0",
"@types/react": ">=17.0.0",
"react": ">=17.0.0",
"react-dom": ">=17.0.0"
},
"dependencies": {
"@babel/runtime-corejs3": "^7.14.7"
},
"devDependencies": {
"@tarojs/components": "^3.3.2",
"@tarojs/taro": "^3.3.2",
"@types/react": "^17.0.17",
"react": "^17.0.2",
"react-dom": "^17.0.2"
}
}
58 changes: 58 additions & 0 deletions packages/weui/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const { resolve, join } = require('path')
const { getBabelOutputPlugin } = require('@rollup/plugin-babel')
const json = require('@rollup/plugin-json')
const image = require('@rollup/plugin-image')
const copy = require('rollup-plugin-copy')
const typescript = require('@rollup/plugin-typescript')
const commonjs = require('@rollup/plugin-commonjs')
const { nodeResolve } = require('@rollup/plugin-node-resolve')
const cwd = process.cwd()

const config = {
input: join(cwd, 'src/index.ts'),
output: [
{
file: join(cwd, 'dist/index.js'),
format: 'cjs',
sourcemap: true,
},
{
sourcemap: true,
format: 'esm',
file: join(cwd, 'dist/index.esm.js'),
},
],
external: [
'@babel/runtime-corejs3',
'@tarojs/taro',
'@tarojs/components',
'react',
'react-dom',
],
plugins: [
commonjs({
include: /\/node_modules\//,
}),
nodeResolve({
customResolveOptions: {
moduleDirectories: ['node_modules'],
},
}),
json(),
typescript({
tsconfig: resolve(__dirname, '../../tsconfig.json'),
module: 'esnext',
skipLibCheck: true,
}),
getBabelOutputPlugin({
configFile: resolve(__dirname, '../../babel.config.js'),
}),
image(),
copy({
targets: [{ src: 'src/style', dest: 'dist' }],
}),
],
}

module.exports = [config]
59 changes: 59 additions & 0 deletions packages/weui/rollup.watch.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const { resolve, join } = require('path')
const { getBabelOutputPlugin } = require('@rollup/plugin-babel')
const json = require('@rollup/plugin-json')
const image = require('@rollup/plugin-image')
const copy = require('rollup-plugin-copy-watch')
const typescript = require('@rollup/plugin-typescript')
const commonjs = require('@rollup/plugin-commonjs')
const { nodeResolve } = require('@rollup/plugin-node-resolve')
const cwd = process.cwd()

const config = {
input: join(cwd, 'src/index.ts'),
output: [
{
file: join(cwd, 'dist/index.js'),
format: 'cjs',
sourcemap: true,
},
{
sourcemap: true,
format: 'esm',
file: join(cwd, 'dist/index.esm.js'),
},
],
external: [
'@babel/runtime-corejs3',
'@tarojs/taro',
'@tarojs/components',
'react',
'react-dom',
],
plugins: [
commonjs({
include: /\/node_modules\//,
}),
nodeResolve({
customResolveOptions: {
moduleDirectories: ['node_modules'],
},
}),
json(),
typescript({
tsconfig: resolve(__dirname, '../../tsconfig.json'),
module: 'esnext',
skipLibCheck: true,
}),
getBabelOutputPlugin({
configFile: resolve(__dirname, '../../babel.config.js'),
}),
image(),
copy({
watch: 'src/style',
targets: [{ src: 'src/style', dest: 'dist' }],
}),
],
}

module.exports = [config]
57 changes: 57 additions & 0 deletions packages/weui/src/components/button/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Button } from '@tarojs/components'
import type { ButtonProps } from '../../../types/button.d'

export default function Index(props: ButtonProps = { children: '' }) {
const { size, type, className, loading, disabled, ...others } = props
let cls = ''
if (size === 'full') {
if (!type || type === 'primary') {
cls = 'weui-btn_cell weui-btn_cell-primary'
} else if (type === 'default') {
cls = 'weui-btn_cell weui-btn_cell-default'
} else if (type === 'warn') {
cls = 'weui-btn_cell weui-btn_cell-warn'
}
} else {
if (!type || type === 'primary') {
cls = 'weui-btn weui-btn_primary'
} else if (type === 'default') {
cls = 'weui-btn weui-btn_default'
} else if (type === 'warn') {
cls = 'weui-btn weui-btn_warn'
}

if (size === 'small') {
cls = cls + ' weui-btn_mini'
} else if (size === 'around') {
cls = cls + ' weui-btn_around'
}
}

if (loading) {
cls = cls + ' weui-btn_loading'
}

if (disabled) {
cls = cls + ' weui-btn_disabled'
}

return (
<Button className={`${cls} ${className || ''}`} {...others}>
<>
{loading && (
<span
className={`weui-primary-loading ${
!type || type === 'primary'
? 'weui-primary-loading_transparent'
: ''
}`}
>
<i className="weui-primary-loading__dot"></i>
</span>
)}
{props.children}
</>
</Button>
)
}
94 changes: 94 additions & 0 deletions packages/weui/src/components/button/miniPhoneButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import {
forwardRef,
useEffect,
useState,
useImperativeHandle,
ForwardedRef,
} from 'react'
import Taro from '@tarojs/taro'
import { View, ButtonProps, CommonEventFunction } from '@tarojs/components'
import type {
MiniPhoneButtonProps,
MiniPhoneButtonRef,
} from '../../../types/button.d'
import Button from './index'

function Index(
props: MiniPhoneButtonProps,
ref: ForwardedRef<MiniPhoneButtonRef>,
): JSX.Element {
const { onGetPhone, children, ...others } = props
const [code, setCode] = useState('')

const onUpdateCode = () => {
setCode('')
Taro.login({
success(res) {
setCode(res.code)
},
})
}

useImperativeHandle(ref, () => ({
onUpdateCode,
}))

useEffect(function () {
Taro.login({
success(res) {
setCode(res.code)
},
})
}, [])

const getPhone: CommonEventFunction<ButtonProps.onGetPhoneNumberEventDetail> =
function (e) {
if (e.detail.encryptedData) {
onGetPhone({
code: code,
iv: e.detail.iv,
encryptedData: e.detail.encryptedData,
})
} else {
Taro.showToast({
title: '授权失败,请重试',
})
}
}

const callLogin = function () {
Taro.login({
success(res) {
setCode(res.code)
Taro.showToast({
title: '授权失败,请重试',
})
},
fail() {
Taro.showToast({
title: '授权失败,请重试',
})
},
})
}

return (
<>
{code ? (
<Button
{...others}
openType="getPhoneNumber"
onGetPhoneNumber={getPhone}
>
{children}
</Button>
) : (
<View {...others} onClick={callLogin}>
{children}
</View>
)}
</>
)
}

export default forwardRef(Index)
Loading

0 comments on commit a387d3f

Please # to comment.