Skip to content

Commit da39483

Browse files
committed
feat(classname-setter): 优化类名设置组件功能和代码结构
- 添加 IOption 接口,用于定义数据源项的结构 - 优化 getClassNameList 方法,使用解构赋值和类型注解 - 重构 handleChange 方法,添加类型注解和可选链操作 - 优化组件初始化逻辑,使用 map 和类型注解 - 为 Select 组件的 onChange 事件处理函数添加类型转换
1 parent 4c2d7e5 commit da39483

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/setter/classname-setter/index.tsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ export interface PluginProps {
99
onChange: any;
1010
}
1111

12+
interface IOption {
13+
label: string;
14+
value: string;
15+
}
16+
1217
export default class ClassNameView extends PureComponent<PluginProps> {
1318
static display = 'ClassName';
1419

@@ -22,10 +27,15 @@ export default class ClassNameView extends PureComponent<PluginProps> {
2227
value: '',
2328
};
2429

30+
state = {
31+
dataSource: [],
32+
selectValue: [],
33+
};
34+
2535
getClassNameList = () => {
2636
const schema = project.exportSchema();
27-
const css = schema.componentsTree[0].css;
28-
const classNameList = [];
37+
const { css } = schema.componentsTree[0];
38+
const classNameList: string[] = [];
2939
if (css) {
3040
const re = /\.?\w+[^{]+\{[^}]*\}/g;
3141
const list = css.match(re);
@@ -47,9 +57,9 @@ export default class ClassNameView extends PureComponent<PluginProps> {
4757
return classNameList;
4858
};
4959

50-
handleChange = (value) => {
60+
handleChange = (value?: string[]) => {
5161
const { onChange } = this.props;
52-
onChange(value.join(' '));
62+
onChange(value?.join(' '));
5363
this.setState({
5464
selectValue: value,
5565
});
@@ -59,7 +69,7 @@ export default class ClassNameView extends PureComponent<PluginProps> {
5969
componentWillMount() {
6070
const { value } = this.props;
6171
const classnameList = this.getClassNameList();
62-
const dataSource = [];
72+
const dataSource: IOption[] = [];
6373
classnameList.map((item) => {
6474
dataSource.push({
6575
value: item,
@@ -69,7 +79,7 @@ export default class ClassNameView extends PureComponent<PluginProps> {
6979
return item;
7080
});
7181

72-
let selectValue = [];
82+
let selectValue: string[] = [];
7383
if (value && value !== '') {
7484
selectValue = value.split(' ');
7585
}
@@ -98,7 +108,7 @@ export default class ClassNameView extends PureComponent<PluginProps> {
98108
aria-label="tag mode"
99109
mode="tag"
100110
dataSource={dataSource}
101-
onChange={this.handleChange}
111+
onChange={(value) => this.handleChange(value as string[])}
102112
value={selectValue}
103113
/>
104114
);

0 commit comments

Comments
 (0)