Skip to content

Commit da5efbd

Browse files
author
Ben Grynhaus
committed
fixed ng classes being forbidden
1 parent 27ae18f commit da5efbd

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

libs/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "../../node_modules/ng-packagr/package.schema.json",
33
"name": "@angular-react/core",
4-
"version": "0.2.0-5",
4+
"version": "0.2.0-6",
55
"ngPackage": {
66
"lib": {
77
"languageLevel": [

libs/core/src/components/wrapper-component.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const ignoredAttributeMatchers = [
1818
/^_?ng-?.*/
1919
];
2020

21+
const ngClassRegExp = /^ng-/;
22+
2123
export interface RenderComponentOptions<TContext extends object> {
2224
readonly componentType: Type<TContext>;
2325
readonly factoryResolver: ComponentFactoryResolver;
@@ -142,10 +144,10 @@ export abstract class ReactWrapperComponent<TProps extends {}> implements AfterV
142144

143145
// Ensure there are no blacklisted props. Suggest alternative as error if there is any
144146
hostAttributes.forEach(attr => {
145-
const altAttrMapping = forbiddenAttributesAsProps.find(([originalAttrName, altAttrName]) => originalAttrName === attr.name);
146-
if (altAttrMapping) {
147-
throw new Error(`[${(this.elementRef.nativeElement as HTMLElement).tagName.toLowerCase()}] React wrapper components cannot have the '${attr.name}' attribute set. Use the following alternative: ${altAttrMapping && altAttrMapping[1] || ''}`);
148-
}
147+
const [forbidden, alternativeAttrName] = this._isForbiddenAttribute(attr);
148+
if (forbidden) {
149+
throw new Error(`[${(this.elementRef.nativeElement as HTMLElement).tagName.toLowerCase()}] React wrapper components cannot have the '${attr.name}' attribute set. Use the following alternative: ${alternativeAttrName || ''}`);
150+
}
149151
});
150152

151153
const whitelistedHostAttributes = hostAttributes.filter(attr => !this._isIgnoredAttribute(attr));
@@ -172,4 +174,20 @@ export abstract class ReactWrapperComponent<TProps extends {}> implements AfterV
172174
private _isIgnoredAttribute(attr: Attr) {
173175
return ignoredAttributeMatchers.some(regExp => regExp.test(attr.name));
174176
}
177+
178+
private _isForbiddenAttribute(attr: Attr): [boolean, string | undefined] {
179+
const { name, value } = attr;
180+
181+
if (name === 'key') return [true, undefined];
182+
if (name === 'class' && value.split(' ').some(className => !ngClassRegExp.test(className))) return [true, 'rClass'];
183+
if (name === 'style') {
184+
const style = toStyle(value);
185+
// Only allowing style if it's something that changes the display - setting anything else should be done on the child component directly (via the `styles` attribute in fabric for example)
186+
if (Object.entries(style).filter(([key, value]) => value || key !== 'display').length > 0) {
187+
return [true, 'rStyle'];
188+
}
189+
}
190+
191+
return [false, undefined];
192+
}
175193
}

0 commit comments

Comments
 (0)