Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): type checking for mods in withBemMod #463

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/core/core.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { ComponentType, StatelessComponent } from 'react';
import { cn, NoStrictEntityMods } from '@bem-react/classname';
import { cn } from '@bem-react/classname';
import { classnames } from '@bem-react/classnames';

export interface IClassNameProps {
Expand All @@ -10,15 +10,15 @@ export type Enhance<T extends IClassNameProps> = (WrappedComponent: ComponentTyp

type Dictionary<T = any> = { [key: string]: T };

export function withBemMod<T, U extends IClassNameProps = {}>(blockName: string, mod: NoStrictEntityMods, enhance?: Enhance<T & U>) {
export function withBemMod<T, U extends IClassNameProps = { className?: string }>(blockName: string, mod: T, enhance?: Enhance<T & U>) {
return function WithBemMod<K extends IClassNameProps = {}>(WrappedComponent: ComponentType<T & K>) {
// Use cache to prevent create new component when props are changed.
let ModifiedComponent: ComponentType<any>;

return function BemMod(props: T & K) {
const entity = cn(blockName);
const isMatched = (key: string) => (props as Dictionary)[key] === mod[key];
const isStarMatched = (key: string) => mod[key] === '*' && Boolean((props as Dictionary)[key]);
const isMatched = (key: string) => (props as Dictionary)[key] === (mod as Dictionary)[key];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Чёт мне не нравится постоянное дублирование props as Dictionary, mod as Dictionary
Это можно "профиксить" так

const _props = props as Dictionary;
const _mod = mod as Dictionary;

const isStarMatched = (key: string) => (mod as Dictionary)[key] === '*' && Boolean((props as Dictionary)[key]);

if (__DEV__) {
setDisplayName(BemMod, {
Expand All @@ -30,7 +30,7 @@ export function withBemMod<T, U extends IClassNameProps = {}>(blockName: string,

if (Object.keys(mod).every(key => isMatched(key) || isStarMatched(key))) {
const modifierClassName = entity(Object.keys(mod).reduce((acc: Dictionary, key) => {
if (mod[key] !== '*') acc[key] = mod[key];
if ((mod as Dictionary)[key] !== '*') acc[key] = (mod as Dictionary)[key];

return acc;
}, {}));
Expand Down
20 changes: 8 additions & 12 deletions packages/core/test/withBemMod.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ use(spies);
const getClassNameFromSelector = (Component: React.ReactElement<any>, selector: string = 'div') =>
mount(Component).find(selector).prop('className');

interface IPresenterProps extends IClassNameProps {
theme?: 'normal';
view?: 'default';
url?: string;
}
interface IPresenterProps extends IClassNameProps {}

const presenter = cn('Presenter');

Expand All @@ -25,30 +21,30 @@ const Presenter: React.FC<IPresenterProps> = ({ className }) =>

describe('withBemMod', () => {
it('should not affect CSS class with empty object', () => {
const WBCM = withBemMod<IPresenterProps>(presenter(), {})(Presenter);
const WBCM = withBemMod(presenter(), {})(Presenter);
expect(getClassNameFromSelector(<WBCM className="Additional" />))
.eq('Presenter Additional');
});

it('should add modifier class for matched prop', () => {
const Enhanced1 = withBemMod<IPresenterProps>(presenter(), { theme: 'normal' })(Presenter);
const Enhanced2 = withBemMod<IPresenterProps>(presenter(), { view: 'default' })(Enhanced1);
const Enhanced1 = withBemMod<{theme?: 'normal'}>(presenter(), { theme: 'normal' })(Presenter);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Здесь нужны пробелы? <{ theme?: 'normal' }>

const Enhanced2 = withBemMod<{view?: 'default'}>(presenter(), { view: 'default' })(Enhanced1);
const Component = <Enhanced2 className="Additional" theme="normal" view="default" />;

expect(getClassNameFromSelector(Component))
.eq('Presenter Presenter_theme_normal Presenter_view_default Additional');
});

it('should not add modifier class for star matched prop', () => {
const Enhanced1 = withBemMod<IPresenterProps>(presenter(), { url: '*' })(Presenter);
const Enhanced1 = withBemMod<{url?: string}>(presenter(), { url: '*' })(Presenter);
const Component = <Enhanced1 className="Additional" url="ya.ru" />;

expect(getClassNameFromSelector(Component))
.eq('Presenter Additional');
});

it('should match on star matched prop', () => {
const Enhanced1 = withBemMod<IPresenterProps>(presenter(), { url: '*' }, Base => props => (
const Enhanced1 = withBemMod<{url?: string}>(presenter(), { url: '*' }, Base => props => (
<Base {...props} className="Additional" />
))(Presenter);
const Component = <Enhanced1 url="ya.ru" />;
Expand All @@ -58,14 +54,14 @@ describe('withBemMod', () => {
});

it('should not add modifier class for unmatched prop', () => {
const WBCM = withBemMod<IPresenterProps>(presenter(), { theme: 'normal' })(Presenter);
const WBCM = withBemMod<{theme?: 'normal'}>(presenter(), { theme: 'normal' })(Presenter);
expect(getClassNameFromSelector(<WBCM className="Additional" />))
.eq('Presenter Additional');
});

it('should not initialized after change props', () => {
const init = spy();
const Enhanced = withBemMod<IPresenterProps>(presenter(), { theme: 'normal' }, WrapepdComponent => (
const Enhanced = withBemMod<{theme?: 'normal'}>(presenter(), { theme: 'normal' }, WrapepdComponent => (
class WithEnhanced extends React.PureComponent {
constructor(props: IPresenterProps) {
super(props);
Expand Down