Skip to content

Commit f6bded7

Browse files
author
Ben Grynhaus
committed
[Core] Allow more syntax input options in contentClass input for ReactWrapperComponent
Allows all [ngClass] options (and more)
1 parent 6a8dae5 commit f6bded7

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import {
1515
TemplateRef,
1616
Type,
1717
} from '@angular/core';
18+
import classnames from 'classnames';
1819
import toStyle from 'css-to-style';
20+
import { Many } from '../declarations/many';
1921
import { ReactContentProps } from '../renderer/react-content';
2022
import { isReactNode } from '../renderer/react-node';
2123
import { isReactRendererData } from '../renderer/renderer';
@@ -42,33 +44,34 @@ export type InputRendererOptions<TContext extends object> =
4244

4345
export type JsxRenderFunc<TContext> = (context: TContext) => JSX.Element;
4446

47+
export type ContentClassValue = string[] | Set<string> | { [klass: string]: any };
4548
/**
4649
* Base class for Angular @Components wrapping React Components.
4750
* Simplifies some of the handling around passing down props and setting CSS on the host component.
4851
*/
4952
// NOTE: TProps is not used at the moment, but a preparation for a potential future change.
5053
export abstract class ReactWrapperComponent<TProps extends {}> implements AfterViewInit, OnChanges {
51-
private _contentClass: string;
52-
private _contentStyle: string;
54+
private _contentClass: Many<ContentClassValue>;
55+
private _contentStyle: ContentStyleValue;
5356

5457
protected abstract reactNodeRef: ElementRef<HTMLElement>;
5558

5659
/**
57-
* Alternative to `class` using the same syntax.
60+
* Alternative to `class` and `[ngClass]` using the same syntax.
5861
*
59-
* @description Since this is a wrapper component, sticking to the virtual DOM concept, this should have any styling of its own.
60-
* Any value passes to `contentClass` will be passed to the root component's class.
62+
* @description Since this is a wrapper component, sticking to the virtual DOM concept, its DOM element shouldn't have any styling of its own.
63+
* Instead, any value passes to `contentClass` will be passed to the root component's class as `className`.
6164
*/
6265
@Input()
63-
set contentClass(value: string) {
66+
set contentClass(value: Many<ContentClassValue>) {
6467
this._contentClass = value;
6568
if (isReactNode(this.reactNodeRef.nativeElement)) {
66-
this.reactNodeRef.nativeElement.setProperty('className', value);
69+
this.reactNodeRef.nativeElement.setProperty('className', classnames(value));
6770
this.changeDetectorRef.detectChanges();
6871
}
6972
}
7073

71-
get contentClass(): string {
74+
get contentClass(): Many<ContentClassValue> {
7275
return this._contentClass;
7376
}
7477

0 commit comments

Comments
 (0)