Skip to main content

File Editing Practice - Frontend Development

Qoder CN file editing provides multi-file editing and tool-use capabilities. It helps developers complete coding tasks, such as implementing requirements, solving problems, generating unit test cases, and performing batch code modifications. This topic focuses on practical use cases in typical frontend development scenarios, such as text-to-code, image-to-code, importing self-developed frontend components, and refactoring self-developed frontend components. Qoder CN File Editing has the capabilities of multi-file code modification (Multi-file Edit) and tool use (Tool-use), and can collaborate with developers to complete coding tasks such as requirement implementation, problem solving, unit test case generation, and batch code modification. This article focuses on typical scenarios in front-end development, providing usage practices for core scenarios such as text-to-code, image-to-code, the introduction of self-developed front-end components, and code refactoring of self-developed front-end components.

Scenario 1: Front-End Component Introduction

In modern front-end development, developers need to repeatedly write or integrate UI components that comply with enterprise design specifications, such as buttons, forms, navigation bars, and modals. Traditional approaches are time-consuming and laborious, while the prompt-based approach can quickly generate or introduce the required component code through a simple description, greatly improving development efficiency.

1. Enterprise Knowledge Base Preparation

To make the knowledge base retrieval-augmentation technology work best, there are the following requirements for front-end component documentation in terms of knowledge base document preparation.

Principles for Preparing Front-End Component Documentation

  1. Clear structure: Organize in logical order, divided into the component title/name, basic usage and code examples, attributes, events, and so on, with each module having a clear title.
  2. Title and function overview: Use a clear title (such as ColorPicker Color Picker) to describe the component name, and briefly explain the component's function and purpose (such as "Used for color selection, supports multiple formats").
  3. Core usage:
    • For basic usage, provide an explanation and a code example that uses the component, demonstrating the component's core functionality.
    • If there are advanced features or complex usages, additionally provide code examples for those scenarios.
      • You can use a sub-section format to introduce the component's different advanced features separately, with each feature module providing a code example and a brief explanation showing how to enable a specific feature or its specific usage.
  4. Attributes: List all the component's configurable attributes in a table, including the parameter name, function description, data type, range of possible values, and default value.
  5. Events: List all events triggered by the component in a table, including the event name, trigger condition or timing, callback parameters, and their meanings.
  6. Code example: Providing a code implementation example is sufficient. For details, refer to the following "ColorPicker" example.

Documentation Reference Example

ColorPicker.md
## ColorPicker 颜色选择器

用于颜色选择,支持多种格式。

### 基础用法

使用 v-model 与 Vue 实例中的一个变量进行双向绑定,绑定的变量需要是字符串类型。

```html
<div class="block">
  <span class="demonstration">有默认值</span>
  <el-color-picker v-model="color1"></el-color-picker>
</div>
<div class="block">
  <span class="demonstration">无默认值</span>
  <el-color-picker v-model="color2"></el-color-picker>
</div>

<script>
  export default {
    data {
      return {
        color1: '#409EFF',
        color2: null
      }
    }
  };
</script>
```

### 选择透明度

ColorPicker 支持普通颜色,也支持带 Alpha 通道的颜色,通过 `show-alpha` 属性即可控制是否支持透明度的选择。

```html
<el-color-picker v-model="color" show-alpha></el-color-picker>
```

### 预定义颜色

ColorPicker 支持预定义颜色。

```html
<el-color-picker
  v-model="color"
  show-alpha
  :predefine="predefineColors">
</el-color-picker>
```

### Attributes
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|---|---|---|---|---|
| value / v-model | 绑定值 | string | — | — |
| disabled | 是否禁用 | boolean | — | false |
| size | 尺寸 | string | medium / small / mini | — |
| show-alpha | 是否支持透明度选择 | boolean | — | false |
| color-format | 写入 v-model 的颜色的格式 | string | hsl / hsv / hex / rgb | hex / rgb |
| popper-class | ColorPicker 下拉框的类名 | string | — | — |
| predefine | 预定义颜色 | array | — | — |

### Events
| 事件名称 | 说明 | 回调参数 |
|---|---|---|
| change | 当绑定值变化时触发 | 当前值 |
| active-change | 面板中当前显示的颜色发生改变时触发 | 当前显示的颜色值 |

2. Scenario Demonstration: "Code Generation for the Self-Developed ColorPicker Component"

This demonstration uses the open-source project mall-admin-web. We would like to thank the project developers for their contribution to open source. Video

Scenario 2: Front-End Component Replacement and Refactoring

In modern front-end development, developers often need to refactor front-end components, such as replacing an old version with a new one. Traditional methods require finding and manually replacing components, which is time-consuming and laborious. With Qoder CN, you can quickly complete component replacement through prompts and a component documentation library, improving refactoring efficiency.

1. Enterprise Knowledge Base Preparation

To make the knowledge base retrieval-augmentation technology work best, there are the following requirements for front-end component documentation in terms of knowledge base document preparation. Prompt example: This Table component is @alicloud/console-components; help me change it to the CndTable of @ali/cnd. The following "@ali-cnd CndTable.md" is an example after the change.

Documentation Reference Example

@ali-cnd CndTable.md
#  @ali/cnd CndTable 表格组件

> 如果使用 refreshIndex 属性初始值应设置为 0设置为非 0, 第一次加载时可能造成 fetchData 函数执行两次

## 基本使用

```tsx preview
import React from 'react';
import { CndTable } from '@ali/cnd';
import axios from 'axios';
import { get } from 'lodash';

const Demo =  => {
  const columns = [
    { key: 'InstanceName', title: '实例名称', dataIndex: 'InstanceName', width: 300 },
    { key: 'Address', title: 'IP地址', dataIndex: 'Address', width: 300 },
    { key: 'CreationTime', title: '创建时间', width: 300, dataIndex: 'CreationTime', sortable: true },
    { key: 'Status', title: '状态', dataIndex: 'Status', width: 300 },
  ];
  const fetchData = async params => {
    const res = await axios.get('https://oneapi.alibaba-inc.com/mock/cloud-native/request/DescribeInstances', { params });
    return {
      data: get(res, 'data.data.Instances.Instance'),
      total: get(res, 'data.data.TotalCount'),
    };
  };
  return (
    <CndTable
      columns={columns}
      fetchData={fetchData}
      pagination={{ current: 1, pageSize: 10, total: 40 }}
    />
  );
};

export default Demo;
```

## operation && secondaryOperation

```tsx preview
import React from 'react';
import Table from '@ali/cnd-table';
import { Button } from '@alicloud/console-components';

const Demo =  => {
  // ...columns 与 fetchData 同上
  return (
    <Table
      columns={columns}
      fetchData={fetchData}
      pagination={{ current: 1, pageSize: 10, total: 40 }}
      showRefreshButton
      operation={<Button type="primary">自定义左上角按钮</Button>}
      secondaryOperation={<Button>自定义右上角按钮</Button>}
    />
  );
};

export default Demo;
```

## refreshIndex / showRefreshButton / search / selection

> 以上属性可组合使用用于手动刷新搜索栏行选择轮询等场景具体用法参见组件源码示例

## API

> 继承 @alicloud/console-components-table 组件的 API

| 名称 | 类型 | 说明 | 默认值 |
|---|---|---|---|
| fetchData | (data: IParams) => Promise<IResult> | 请求数据源的方法, 入参包含搜索条件和分页信息等 | - |
| refreshIndex | number | 触发刷新操作重新请求 fetchData | 0 |
| recordCurrent | boolean | refreshIndex 更新后是否停留在当前页 | false |
| showRefreshButton | boolean | 是否显示刷新按钮 | false |
| search | IRcSearchProps | 搜索栏配置 | - |
| columns | ColumnProps[] | 列描述数据对象 | - |
| operation | ReactNode | 自定义左上角内容 | - |
| secondaryOperation | ReactNode | 自定义右上角内容 | - |
| isShowLoading | boolean | 数据刷新时是否需要展示 loading | true |
| loop | Object | 接口是否轮询以及轮询时间设置 | { enable: false, time: 10000, showLoading: false } |

### IParams

```
interface IParams {
  current?: number;
  pageSize?: number;
  [key: string]: any;
}
```

### IResult

```
interface IResult {
  data: any[];
  total?: number;
}
```

2. Scenario Demonstration: "Rapid Refactoring and Replacement of a Self-Developed Table Component"

Video

Scenario 3: Text-to-Code

Experience how to use Qoder CN File Editing to quickly build a sales dashboard front-end page from scratch. Simply enter your requirements or design ideas as a prompt, and Qoder CN File Editing can automatically generate front-end code, which you can run directly and preview in the browser, achieving a seamless connection from concept to presentation.

1. Scenario Demonstration: "Sales Dashboard Front-End Page Generation"

Video