home / skills / einverne / dotfiles / react-component-generator

react-component-generator skill

/skills/react-component-generator

This skill generates React components that adhere to project conventions: TypeScript, functional components, CSS Modules, JSDoc, and tests.

npx playbooks add skill einverne/dotfiles --skill react-component-generator

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
1.7 KB
---
name: react-component-generator
description: 生成符合项目规范的 React 组件。当用户要求创建组件、新建 React 组件或生成组件文件时使用
---

# React 组件生成器

## 组件规范

项目使用以下约定:
- TypeScript + React
- 函数式组件 + Hooks
- CSS Modules 样式
- 完整的 JSDoc 注释
- 配套的测试文件

## 生成流程

1. 确认组件信息
   - 组件名称(PascalCase)
   - 组件类型(基础组件、容器组件、页面组件)
   - 所需 props

2. 创建文件结构
   ```
   src/components/{ComponentName}/
   ├── index.tsx
   ├── {ComponentName}.module.css
   └── {ComponentName}.test.tsx
   ```

3. 生成组件文件

index.tsx 模板:
```typescript
import React from 'react';
import styles from './{ComponentName}.module.css';

interface {ComponentName}Props {
  // 定义 props 类型
}

/**
 * {组件描述}
 * @param props - 组件属性
 */
export const {ComponentName}: React.FC<{ComponentName}Props> = (props) => {
  return (
    <div className={styles.container}>
      {/* 组件内容 */}
    </div>
  );
};
```

4. 生成样式文件

{ComponentName}.module.css 模板:
```css
.container {
  /* 组件样式 */
}
```

5. 生成测试文件

{ComponentName}.test.tsx 模板:
```typescript
import { render, screen } from '@testing-library/react';
import { {ComponentName} } from './index';

describe('{ComponentName}', () => {
  it('renders correctly', () => {
    render(<{ComponentName} />);
    // 添加断言
  });
});
```

## 质量检查

生成后自动运行:
1. TypeScript 类型检查:`npm run type-check`
2. ESLint 检查:`npm run lint`
3. 测试:`npm test -- {ComponentName}`

如有错误,显示错误信息并提供修复建议。

Overview

This skill generates React components that follow a strict TypeScript + React project convention. It creates a component folder with a typed functional component, CSS Modules stylesheet, and a corresponding test file. The generator also runs TypeScript, ESLint, and unit test checks and reports problems with suggested fixes.

How this skill works

Provide the component name (PascalCase), component type (base, container, or page), and required props. The tool scaffolds src/components/{ComponentName}/ with index.tsx, {ComponentName}.module.css, and {ComponentName}.test.tsx using templates that include JSDoc, typed props, Hooks-ready structure, and CSS Modules import. After generation it triggers type checking, linting, and the component test and returns diagnostics and repair suggestions if any step fails.

When to use it

  • When adding a new UI component that must match project conventions.
  • When you want consistent file structure and TypeScript typing for components.
  • When onboarding contributors to ensure uniform component patterns.
  • When you need a quick starter for a component with tests and styles included.

Best practices

  • Name components in PascalCase and choose the right type (base/container/page) before generating.
  • Define props explicitly in the generated interface and add JSDoc comments for public props.
  • Keep styles scoped with CSS Modules and place component-specific rules in .module.css.
  • Run the generator early in development to avoid manual wiring and lint issues.
  • Address reported TypeScript and ESLint errors immediately and re-run the checks.

Example use cases

  • Generate a simple Button base component with onClick and label props.
  • Create a Form container component that composes multiple base components and handles local state with Hooks.
  • Scaffold a Page component with placeholder layout and initial test to assert render.
  • Quickly add a small reusable input component complete with CSS Modules and a unit test.

FAQ

Does the generator enforce TypeScript and lint rules automatically?

Yes. After scaffolding it runs the project's TypeScript check and ESLint, then reports any errors with suggestions.

Can I customize the templates for different component types?

Templates are designed for base, container, and page types; you can extend or modify them to match additional conventions before generating.