-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponentFileContent.js
More file actions
198 lines (182 loc) · 5.28 KB
/
ComponentFileContent.js
File metadata and controls
198 lines (182 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* @class ComponentFileContent
* @description ComponentFileContent is a class that represents the content of a file.
*/
class ComponentFileContent {
// private properties
#content = "";
#hasStyles = false;
#fileExtension = "js";
#hasProps = false;
#hasImportReact = false;
#componentName = "";
#componentType = "functional";
constructor(
componentName,
hasStyles = this.#hasStyles,
fileExtension = this.#fileExtension,
hasProps = this.#hasProps,
hasImportReact = this.#hasImportReact,
componentType = this.#componentType
) {
if (!componentName) {
throw new Error("Component name is required");
}
this.#hasStyles = hasStyles;
this.#fileExtension = fileExtension;
this.#hasProps = hasProps;
this.#hasImportReact = hasImportReact;
this.#componentName = componentName;
this.#componentType = componentType;
}
get componentName() {
return this.#componentName;
}
get hasStyles() {
return this.#hasStyles;
}
get fileExtension() {
return this.#fileExtension;
}
get hasProps() {
return this.#hasProps;
}
get hasImportReact() {
return this.#hasImportReact;
}
get componentType() {
return this.#componentType;
}
get content() {
return this.#content;
}
// generate the part of "import React from 'react'";
#generateImportReact() {
return this.#hasImportReact ? `import React from 'react';\n` : "";
}
// generate additional React imports based on component type
#generateAdditionalImports() {
let imports = "";
if (this.#componentType === "memoized") {
imports += `import { memo } from 'react';\n`;
} else if (this.#componentType === "forwardRef") {
imports += `import { forwardRef } from 'react';\n`;
}
return imports;
}
// generate the part of props, considering if it is a ts file and if it is with props
#generateProps() {
if (this.fileExtension === "ts" && this.#hasProps) {
return `interface Props {}\n`;
}
return "";
}
// generate functional component
#generateFunctionalComponent() {
let propsParamContent = "";
if (this.#hasProps) {
propsParamContent = this.fileExtension === "ts" ? "props: Props" : "props";
}
return `function ${this.componentName}(${propsParamContent}) {
return (
<>
{/* Add your component content here */}
</>
);
}`;
}
// generate arrow function component
#generateArrowFunctionComponent() {
let propsParamContent = "";
if (this.#hasProps) {
propsParamContent = this.fileExtension === "ts" ? "props: Props" : "props";
}
return `const ${this.componentName} = (${propsParamContent}) => {
return (
<>
{/* Add your component content here */}
</>
);
}`;
}
// generate class component
#generateClassComponent() {
let propsParamContent = "";
if (this.#hasProps) {
propsParamContent = this.fileExtension === "ts" ? "Props" : "";
}
return `class ${this.componentName} extends React.Component${propsParamContent ? `<${propsParamContent}>` : ''} {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (
<>
{/* Add your component content here */}
</>
);
}
}`;
}
// generate memoized component
#generateMemoizedComponent() {
let propsParamContent = "";
if (this.#hasProps) {
propsParamContent = this.fileExtension === "ts" ? "props: Props" : "props";
}
return `const ${this.componentName} = memo((${propsParamContent}) => {
return (
<>
{/* Add your component content here */}
</>
);
});`;
}
// generate forwardRef component
#generateForwardRefComponent() {
let propsParamContent = "";
if (this.#hasProps) {
propsParamContent = this.fileExtension === "ts" ? "props: Props" : "props";
}
return `const ${this.componentName} = forwardRef<HTMLDivElement, ${this.fileExtension === "ts" ? "Props" : "any"}>((${propsParamContent}, ref) => {
return (
<div ref={ref}>
{/* Add your component content here */}
</div>
);
});`;
}
// generate the component content based on type
generateComponentContent() {
const importReactContent = this.#generateImportReact();
const additionalImports = this.#generateAdditionalImports();
const propsObjectContent = this.#generateProps();
let componentContent = "";
switch (this.#componentType) {
case "functional":
componentContent = this.#generateFunctionalComponent();
break;
case "arrow":
componentContent = this.#generateArrowFunctionComponent();
break;
case "class":
componentContent = this.#generateClassComponent();
break;
case "memoized":
componentContent = this.#generateMemoizedComponent();
break;
case "forwardRef":
componentContent = this.#generateForwardRefComponent();
break;
default:
componentContent = this.#generateFunctionalComponent();
}
this.#content = `${importReactContent}${additionalImports}
${propsObjectContent}
${componentContent}
export default ${this.componentName};`;
return this.#content;
}
}
module.exports = ComponentFileContent;