>;
10 | /** children节点 */
11 | children?: React.ReactNode
12 | }
13 |
14 | export function withNativeProps(props: P, element: ReactElement) {
15 | const p = {
16 | ...element.props,
17 | };
18 | if (props.className) {
19 | p.className = classNames(element.props.className, props.className);
20 | }
21 | if (props.style) {
22 | p.style = {
23 | ...p.style,
24 | ...props.style,
25 | };
26 | }
27 | return React.cloneElement(element, p);
28 | }
--------------------------------------------------------------------------------
/src/utils/omit.ts:
--------------------------------------------------------------------------------
1 | /** 删除一个对象中的key */
2 | export default function omit(
3 | obj: T,
4 | keys: Array // string 为了某些没有声明的属性被omit
5 | ): Omit {
6 | const clone = {
7 | ...obj,
8 | };
9 | keys.forEach((key) => {
10 | if ((key as K) in clone) {
11 | delete clone[key as K];
12 | }
13 | });
14 | return clone;
15 | }
16 |
--------------------------------------------------------------------------------
/src/utils/random.ts:
--------------------------------------------------------------------------------
1 | /** 生成随机字符串 */
2 | export const randomStr = (v?: string | number) =>
3 | `${v !== void 0 ? v + "-" : ""}${Math.ceil(Math.random() * 10e5).toString(
4 | 36
5 | )}-${Date.now().toString(36)}`;
6 |
7 | /** 打乱数组 Fisher Yates shuffle 算法 */
8 | export function shuffleArray(array: any[]) {
9 | for (let i = array.length - 1; i > 0; i--) {
10 | const j = Math.floor(Math.random() * (i + 1));
11 | [array[i], array[j]] = [array[j], array[i]];
12 | }
13 | }
14 |
15 | /** 生成随机字母 isUppercase 是否大写 */
16 | export function randomLetter(isUppercase = true) {
17 | return String.fromCharCode(
18 | Math.floor(Math.random() * 26) + (isUppercase ? 65 : 97)
19 | );
20 | }
21 |
22 | /** 随机生成十六进制颜色 */
23 | export function getRandomHexColor() {
24 | const letters = "0123456789ABCDEF";
25 | let color = "#";
26 | for (let i = 0; i < 6; i++) {
27 | color += letters[Math.floor(Math.random() * 16)];
28 | }
29 | return color;
30 | }
31 |
--------------------------------------------------------------------------------
/src/utils/replace.ts:
--------------------------------------------------------------------------------
1 | /** 将横线替换成空格 */
2 | export function replaceLineToSpace(str: string) {
3 | return str.replace(/\-/, ' ')
4 | }
5 |
6 | /** 替换掉类名中的特殊字符 */
7 | export function replaceClassName(title: string) {
8 | return title.replace(/[.#]/g, '')
9 | }
10 |
11 | /** 转化颜色 例:将 #fff 转化为 rgb(255, 255, 255) */
12 | export const replaceHexToRgb = (hex: string) => {
13 | if(!hex.includes('#')) return hex
14 | const rgb: number[] = [];
15 | //去除前缀 # 号
16 | hex = hex.substring(1);
17 | if (hex.length === 3) {
18 | // 处理 '#abc' 成 '#aabbcc'
19 | hex = hex.replace(/(.)/g, '$1$1');
20 | }
21 | hex.replace(/../g, (color: string) => {
22 | // 按16进制将字符串转换为数字
23 | rgb.push(parseInt(color, 0x10));
24 | return color;
25 | });
26 | return 'rgb(' + rgb.join(',') + ')';
27 | };
--------------------------------------------------------------------------------
/src/utils/sleep.ts:
--------------------------------------------------------------------------------
1 | export default function sleep(time = 1000) {
2 | return new Promise(resolve => setTimeout(resolve, time));
3 | }
--------------------------------------------------------------------------------
/src/utils/validate.ts:
--------------------------------------------------------------------------------
1 | /** 校验手机 */
2 | export const validatePhone = (rule: any, value: any, callback: any) => {
3 | if (value === '') {
4 | callback(new Error('请输入手机号'));
5 | } else {
6 | const reg = /^1(3|4|5|6|7|8|9)\d{9}$/;
7 | if (!reg.test(value)) {
8 | callback(new Error('请输入正确的手机号'));
9 | }
10 | callback();
11 | }
12 | };
13 |
14 | export function isFunction(val: any) {
15 | return typeof val === 'function';
16 | }
17 | export function isPlainObject(val: any) {
18 | return val !== null && typeof val === 'object' && !Array.isArray(val);
19 | }
20 | export function isPromise(val: any) {
21 | return isPlainObject(val) && isFunction(val.then) && isFunction(val.catch);
22 | }
23 | export function isDef(value: any) {
24 | return value !== undefined && value !== null;
25 | }
26 | export function isObj(x: any) {
27 | const type = typeof x;
28 | return x !== null && (type === 'object' || type === 'function');
29 | }
30 | export function isNumber(value: any) {
31 | return /^\d+(\.\d+)?$/.test(value);
32 | }
33 | export function isBoolean(value: any) {
34 | return typeof value === 'boolean';
35 | }
36 |
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "strict": true,
4 | "declaration": true,
5 | "skipLibCheck": true,
6 | "esModuleInterop": true,
7 | "jsx": "react",
8 | "baseUrl": "./",
9 | "paths": {
10 | "@@/*": [
11 | ".dumi/tmp/*"
12 | ],
13 | "lhh-ui": [
14 | "src"
15 | ],
16 | "lhh-ui/*": [
17 | "src/*",
18 | "*"
19 | ]
20 | }
21 | },
22 | "include": [
23 | ".dumirc.ts",
24 | "src/**/*",
25 | "type/index.ts"
26 | ]
27 | }
--------------------------------------------------------------------------------
/type/index.ts:
--------------------------------------------------------------------------------
1 | // 导入图片的后缀
2 | declare module '*.svg'
3 | declare module '*.png'
4 | declare module '*.jpg'
5 | declare module '*.jpeg'
6 | declare module '*.gif'
7 | declare module '*.bmp'
8 | declare module '*.tiff'
--------------------------------------------------------------------------------