MCPcopy Create free account
hub / github.com/Snapchat/Valdi / generateComponentJsx

Function generateComponentJsx

npm_modules/cli/src/preview/previewGenerator.ts:135–171  ·  view source on GitHub ↗
(
  componentName: string,
  props: ParsedProp[],
  interactiveState: InteractiveState[],
)

Source from the content-addressed store, hash-verified

133}
134
135function generateComponentJsx(
136 componentName: string,
137 props: ParsedProp[],
138 interactiveState: InteractiveState[],
139): string {
140 const stateMap = new Map<string, InteractiveState>();
141 for (const s of interactiveState) {
142 stateMap.set(s.boolPropName, s);
143 stateMap.set(s.callbackPropName, s);
144 }
145
146 const requiredProps = props.filter(p => !p.optional);
147 if (requiredProps.length === 0) {
148 return `<${componentName} />`;
149 }
150
151 const propAssignments: string[] = [];
152 for (const prop of requiredProps) {
153 const state = stateMap.get(prop.name);
154 if (state && prop.isBoolean) {
155 propAssignments.push(`${prop.name}={this.state.${state.stateName}}`);
156 } else if (state && prop.isCallback) {
157 propAssignments.push(
158 `${prop.name}={this.${state.callbackPropName}Handler}`,
159 );
160 } else {
161 propAssignments.push(`${prop.name}={${sampleValueForType(prop.typeString)}}`);
162 }
163 }
164
165 if (propAssignments.length <= 2) {
166 return `<${componentName} ${propAssignments.join(' ')} />`;
167 }
168
169 const indentedProps = propAssignments.map(p => ` ${p}`).join('\n');
170 return `<${componentName}\n${indentedProps}\n />`;
171}
172
173// ---------------------------------------------------------------------------
174// BUILD.bazel

Callers 1

generatePreviewTsxFunction · 0.85

Calls 7

sampleValueForTypeFunction · 0.90
filterMethod · 0.80
setMethod · 0.65
getMethod · 0.65
pushMethod · 0.45
joinMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected