cfel-base-components 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.babelrc ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "presets": [
3
+ "@babel/preset-env",
4
+ "@babel/preset-react",
5
+ "@babel/preset-typescript"
6
+ ]
7
+ }
package/README.md ADDED
File without changes
@@ -0,0 +1,20 @@
1
+ module.exports = {
2
+ resolve: {
3
+ // 定义 import 引用时可省略的文件后缀名
4
+ extensions: ['.js', '.jsx', '.ts', '.tsx'],
5
+ },
6
+ module: {
7
+ rules: [
8
+ {
9
+ // 编译处理 js 和 jsx 文件
10
+ test: /(\.js(x?))|(\.ts(x?))$/,
11
+ use: [
12
+ {
13
+ loader: 'babel-loader'
14
+ }
15
+ ],
16
+ exclude: /node_modules/, // 只解析 src 目录下的文件
17
+ }
18
+ ]
19
+ },
20
+ };
@@ -0,0 +1,62 @@
1
+ const path = require('path');
2
+ const { merge } = require('webpack-merge');
3
+ const baseConfig = require('./webpack.base.js'); // 公共配置
4
+
5
+ const devConfig = {
6
+ mode: 'development', // 开发模式
7
+ entry: path.join(__dirname, "../demo/src/index.jsx"), // 入口,处理资源文件的依赖关系
8
+ output: {
9
+ path: path.join(__dirname, "../demo/src/"),
10
+ filename: "dev.js",
11
+ },
12
+ module: {
13
+ rules: [
14
+ {
15
+ test: /.s[ac]ss$/,
16
+ exclude: /.min.css$/,
17
+ use: [
18
+ { loader: 'style-loader' },
19
+ {
20
+ loader: 'css-loader',
21
+ options: {
22
+ modules: {
23
+ mode: "global"
24
+ }
25
+ }
26
+ },
27
+ {
28
+ loader: 'postcss-loader',
29
+ options: {
30
+ postcssOptions: {
31
+ plugins: [
32
+ [
33
+ 'postcss-preset-env',
34
+ {
35
+ // 其他选项
36
+ },
37
+ ],
38
+ ],
39
+ },
40
+ },
41
+ },
42
+ { loader: 'sass-loader' }
43
+ ]
44
+ },
45
+ {
46
+ test: /.min.css$/,
47
+ use: [
48
+ { loader: 'style-loader' },
49
+ { loader: 'css-loader' }
50
+ ]
51
+ }
52
+ ]
53
+ },
54
+ devServer: {
55
+ static: path.join(__dirname, '../demo/src/'),
56
+ compress: true,
57
+ host: '127.0.0.1',
58
+ port: 8686, // 启动端口
59
+ open: true // 打开浏览器
60
+ },
61
+ };
62
+ module.exports = merge(devConfig, baseConfig); // 合并配置
@@ -0,0 +1,70 @@
1
+ const path = require('path');
2
+ const { merge } = require('webpack-merge');
3
+ const baseConfig = require('./webpack.base.js'); // 引用公共的配置
4
+ const MiniCssExtractPlugin = require("mini-css-extract-plugin"); // 用于将组件的css打包成单独的文件输出到`lib`目录中
5
+
6
+ const prodConfig = {
7
+ mode: 'production', // 生产模式
8
+ entry: path.join(__dirname, "../src/index.tsx"),
9
+ output: {
10
+ path: path.join(__dirname, "../lib/"),
11
+ filename: "index.js",
12
+ libraryTarget: 'umd', // 采用通用模块定义
13
+ libraryExport: 'default', // 兼容 ES6 Module、CommonJS 和 AMD 模块规范
14
+ },
15
+ module: {
16
+ rules: [
17
+ {
18
+ test: /.s[ac]ss$/,
19
+ exclude: /.min.css$/,
20
+ use: [
21
+ { loader: MiniCssExtractPlugin.loader },
22
+ {
23
+ loader: 'css-loader',
24
+ options: {
25
+ modules: {
26
+ mode: "global"
27
+ }
28
+ }
29
+ },
30
+ {
31
+ loader: 'postcss-loader',
32
+ options: {
33
+ postcssOptions: {
34
+ plugins: [
35
+ [
36
+ 'postcss-preset-env',
37
+ {
38
+ // 其他选项
39
+ },
40
+ ],
41
+ ],
42
+ },
43
+ },
44
+ },
45
+ { loader: 'sass-loader' }
46
+ ]
47
+ }
48
+ ]
49
+ },
50
+ plugins: [
51
+ new MiniCssExtractPlugin({
52
+ filename: "index.min.css" // 提取后的css的文件名
53
+ })
54
+ ],
55
+ externals: { // 定义外部依赖,避免把react和react-dom打包进去
56
+ react: {
57
+ root: "React",
58
+ commonjs2: "react",
59
+ commonjs: "react",
60
+ amd: "react"
61
+ },
62
+ "react-dom": {
63
+ root: "ReactDOM",
64
+ commonjs2: "react-dom",
65
+ commonjs: "react-dom",
66
+ amd: "react-dom"
67
+ }
68
+ },
69
+ };
70
+ module.exports = merge(prodConfig, baseConfig); // 合并配置
@@ -0,0 +1,17 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <title>text-width-auto-label</title>
6
+ <style>
7
+ html, body, p {
8
+ margin: 0;
9
+ padding: 0;
10
+ }
11
+ </style>
12
+ </head>
13
+ <body>
14
+ <div id="root"></div>
15
+ <script src="dev.js"></script>
16
+ </body>
17
+ </html>
@@ -0,0 +1,30 @@
1
+ import React from "react"
2
+ import { createRoot } from 'react-dom/client';
3
+ import { WidthAutoLabel } from '../../src/index'; // 引入组件
4
+ import './index.scss';
5
+ // import WidthAutoLabel from 'text-width-auto-label';
6
+
7
+ const App = () => {
8
+ return (
9
+ <div className="container">
10
+ <div className="text">
11
+ <p>示例文本:</p><p>文本宽度自适应标签组件</p>
12
+ </div>
13
+ <div className="normal">
14
+ <p>宽度100px:</p><p>文本宽度自适应标签组件</p>
15
+ </div>
16
+ <div className="style1">
17
+ <p>宽度100px:</p><p><WidthAutoLabel>文本宽度自适应标签组件</WidthAutoLabel></p>
18
+ </div>
19
+ <div className="style2">
20
+ <p>宽度80px:</p><p><WidthAutoLabel>文本宽度自适应标签组件</WidthAutoLabel></p>
21
+ </div>
22
+ <div className="style3">
23
+ <p>宽度50px:</p><p><WidthAutoLabel>文本宽度自适应标签组件</WidthAutoLabel></p>
24
+ </div>
25
+ </div>
26
+ );
27
+ }
28
+ const container = document.getElementById('root');
29
+ const root = createRoot(container);
30
+ root.render(<App />);
@@ -0,0 +1,60 @@
1
+ .container {
2
+ display: flex;
3
+ flex-direction: column;
4
+ justify-content: center;
5
+ align-items: center;
6
+ margin-top: 30px;
7
+ font-size: 14px;
8
+ font-weight: bold;
9
+ color: #333;
10
+ .text {
11
+ display: flex;
12
+ align-items: center;
13
+ p:nth-child(2) {
14
+ color: #f00;
15
+ }
16
+ }
17
+ .normal {
18
+ display: flex;
19
+ align-items: center;
20
+ margin-top: 10px;
21
+ p:nth-child(2) {
22
+ width: 100px;
23
+ height: 32px;
24
+ overflow: hidden;
25
+ text-overflow: ellipsis;
26
+ white-space: nowrap;
27
+ border: 1px solid #ccc;
28
+ }
29
+ }
30
+ .style1 {
31
+ display: flex;
32
+ align-items: center;
33
+ margin-top: 10px;
34
+ p:nth-child(2) {
35
+ width: 100px;
36
+ height: 32px;
37
+ border: 1px solid #ccc;
38
+ }
39
+ }
40
+ .style2 {
41
+ display: flex;
42
+ align-items: center;
43
+ margin-top: 10px;
44
+ p:nth-child(2) {
45
+ width: 70px;
46
+ height: 32px;
47
+ border: 1px solid #ccc;
48
+ }
49
+ }
50
+ .style3 {
51
+ display: flex;
52
+ align-items: center;
53
+ margin-top: 10px;
54
+ p:nth-child(2) {
55
+ width: 40px;
56
+ height: 32px;
57
+ border: 1px solid #ccc;
58
+ }
59
+ }
60
+ }
package/lib/index.js ADDED
@@ -0,0 +1 @@
1
+ !function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t(require("react"));else if("function"==typeof define&&define.amd)define(["react"],t);else{var r="object"==typeof exports?t(require("react")):t(e.React);for(var o in r)("object"==typeof exports?exports:e)[o]=r[o]}}(self,(e=>(()=>{"use strict";var t={787:t=>{t.exports=e}},r={};var o={};return function e(o){var f=r[o];if(void 0!==f)return f.exports;var p=r[o]={exports:{}};return t[o](p,p.exports,e),p.exports}(787),o.default})()));
@@ -0,0 +1,2 @@
1
+ .aaa-button{color:red}
2
+
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "cfel-base-components",
3
+ "version": "0.0.1",
4
+ "description": "cfel-base-components",
5
+ "main": "/src/index.tsx",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1",
8
+ "dev": "webpack-dev-server --config config/webpack.dev.config.js",
9
+ "build": "webpack --config config/webpack.prod.config.js"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git@codeup.aliyun.com:644a10bf88aef0db4b8c1edc/front/base-components.git"
14
+ },
15
+ "author": "",
16
+ "license": "ISC",
17
+ "devDependencies": {
18
+ "@babel/cli": "^7.22.5",
19
+ "@babel/core": "^7.22.5",
20
+ "@babel/preset-env": "^7.22.5",
21
+ "@babel/preset-react": "^7.22.5",
22
+ "@babel/preset-typescript": "^7.22.5",
23
+ "@types/react": "^18.2.14",
24
+ "@types/react-dom": "^18.2.6",
25
+ "babel-loader": "^8.2.5",
26
+ "css-loader": "^6.8.1",
27
+ "mini-css-extract-plugin": "^2.7.6",
28
+ "node-sass": "^9.0.0",
29
+ "postcss": "^8.4.24",
30
+ "postcss-loader": "^7.3.3",
31
+ "postcss-preset-env": "^8.5.1",
32
+ "react": "^18.2.0",
33
+ "react-dom": "^18.2.0",
34
+ "sass-loader": "^13.3.2",
35
+ "style-loader": "^3.3.3",
36
+ "ts-loader": "^9.4.4",
37
+ "webpack": "^5.88.1",
38
+ "webpack-cli": "^5.1.4",
39
+ "webpack-dev-server": "^4.15.1",
40
+ "webpack-merge": "^5.9.0"
41
+ }
42
+ }
@@ -0,0 +1,3 @@
1
+ .aaa-button {
2
+ color: red;
3
+ }
@@ -0,0 +1,16 @@
1
+ import React, { useEffect, useRef } from 'react';
2
+ import "./index.scss"
3
+ interface WidthAutoLabelProps {
4
+ children: string; // 要绘制的文本
5
+ }
6
+
7
+ /**
8
+ * 文本宽度自适应标签组件
9
+ * @param props
10
+ * @constructor
11
+ */
12
+ const Button = (props: WidthAutoLabelProps) => {
13
+ return <div className='aaa-button'>123</div>
14
+ }
15
+
16
+ export default Button;
@@ -0,0 +1,54 @@
1
+ import React, { useEffect, useRef } from 'react';
2
+
3
+ interface WidthAutoLabelProps {
4
+ children: string; // 要绘制的文本
5
+ }
6
+
7
+ /**
8
+ * 文本宽度自适应标签组件
9
+ * @param props
10
+ * @constructor
11
+ */
12
+ const WidthAutoLabel = (props: WidthAutoLabelProps) => {
13
+ const { children = '' } = props;
14
+ const ref = useRef<any>(null);
15
+
16
+ useEffect(() => {
17
+ drawText();
18
+ }, [children]);
19
+
20
+
21
+ const drawText = () => {
22
+ let canvas = ref.current;
23
+ if (!canvas) {
24
+ return;
25
+ }
26
+ let parentNode = canvas.parentNode;
27
+ if (!parentNode) {
28
+ return;
29
+ }
30
+ const { fontSize, fontStyle, fontWeight, fontFamily } = getComputedStyle(parentNode, null);
31
+ const { clientWidth, clientHeight } = parentNode;
32
+ canvas.width = clientWidth;
33
+ canvas.height = clientHeight;
34
+ let ctx = canvas.getContext("2d");
35
+ // 设置文本样式
36
+ ctx.font = `${fontStyle} ${fontWeight} ${fontSize} ${fontFamily}`;
37
+ ctx.textBaseline = 'top';
38
+ let y = Math.ceil((clientHeight - Number(fontSize.replace('px', ''))) / 2);
39
+ let x = 0;
40
+ // 居中显示
41
+ const { width: textWidth } = ctx.measureText(children);
42
+ if (textWidth < clientWidth) {
43
+ x = (clientWidth - textWidth) / 2;
44
+ }
45
+ // 绘制文本
46
+ ctx.fillText(children, x, y, clientWidth);
47
+ }
48
+
49
+ return (
50
+ <canvas ref={ref}></canvas>
51
+ )
52
+ }
53
+
54
+ export default WidthAutoLabel;
package/src/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ declare module '*.scss' {
2
+ const content: any;
3
+ export default content;
4
+ }
package/src/index.tsx ADDED
@@ -0,0 +1,8 @@
1
+ import WidthAutoLabel from './components/widthAutoLabelProps'
2
+ import Button from './components/button'
3
+
4
+
5
+ export {
6
+ WidthAutoLabel,
7
+ Button
8
+ }