Skip to content

Commit 80a4b1c

Browse files
committed
chore(scripts): new-component生成的组件使用 attrs 收束 html attribute,并且支持输入中文名
1 parent 88a52f9 commit 80a4b1c

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

scripts/new-component.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const rootPath = Path.resolve(__dirname, '../');
1212
const config = {
1313
name: '',
1414
componentName: '',
15+
alias: '',
1516
componentsPath: Path.resolve(rootPath, 'packages/components/src'),
1617
};
1718
async function run() {
@@ -52,10 +53,19 @@ async function getConfig() {
5253
config.name = kebabCase(name);
5354
config.componentName = pascalCase(config.name);
5455

56+
const { alias } = await prompt<{ alias: string }>({
57+
type: 'input',
58+
name: 'alias',
59+
message: '输入组件别名(中文)',
60+
required: true,
61+
});
62+
63+
config.alias = alias;
64+
5565
const { confirm } = await prompt<{ confirm: boolean }>({
5666
type: 'confirm',
5767
name: 'confirm',
58-
message: `确认添加组件(${config.name})?`,
68+
message: `确认添加组件(${config.name} ${alias})?`,
5969
});
6070

6171
if (!confirm) throw new Error('放弃添加组件');
@@ -73,20 +83,20 @@ import type { RequiredPart } from '@tool-pack/types';
7383
import { getClassNames } from '@tool-pack/basic';
7484
7585
const rootName = getComponentClass('${config.name}');
86+
const defaultProps = {} satisfies Partial<${props}>;
7687
7788
export const ${componentName}: React.FC<${props}> = (props) => {
78-
const { children, className, ...rest } = props as RequiredPart<
89+
const { attrs, children } = props as RequiredPart<
7990
${props},
8091
keyof typeof defaultProps
8192
>;
8293
return (
83-
<div {...rest} className={getClassNames(rootName, className)}>
94+
<div {...attrs} className={getClassNames(rootName, attrs?.className)}>
8495
{children}
8596
</div>
8697
);
8798
};
8899
89-
const defaultProps = {} satisfies Partial<${props}>;
90100
${componentName}.defaultProps = defaultProps;
91101
${componentName}.displayName = '${componentName}';
92102
`;
@@ -99,9 +109,11 @@ function initTypes(): InitRes {
99109
const content = `
100110
import React from 'react';
101111
102-
export type ${props} = React.HTMLAttributes<HTMLElement> & {
103-
name?: string;
104-
};
112+
export interface ${props} {
113+
attrs?: Partial<React.HTMLAttributes<HTMLElement>>;
114+
children?: React.ReactNode;
115+
ref?: React.ForwardedRef<HTMLElement>;
116+
}
105117
`;
106118
return [filename, content];
107119
}
@@ -152,15 +164,15 @@ function initDoc(): InitRes {
152164
const content = `
153165
---
154166
category: Components
155-
title: ${config.componentName} ${config.componentName}
167+
title: ${config.componentName} ${config.alias}
156168
atomId: ${config.componentName}
157169
demo:
158170
cols: 2
159171
group:
160172
title: 通用
161173
---
162174
163-
${config.componentName} 说明
175+
${config.componentName} ${config.alias}
164176
165177
## 代码演示
166178
@@ -202,7 +214,7 @@ function appendPlayground() {
202214

203215
const insertTarget = '/*insert target*/';
204216
const insertContent = `{
205-
name: '${config.name} ${config.name}',
217+
name: '${config.name} ${config.alias}',
206218
path: '/${config.name}',
207219
element: getDemos(import.meta.glob('~/${config.name}/demo/*.tsx')),
208220
},

0 commit comments

Comments
 (0)