From 36da7b4978bc219b93825eb52e7f152e8f2dceeb Mon Sep 17 00:00:00 2001 From: awinogradov Date: Sat, 18 Nov 2017 01:09:08 +0300 Subject: [PATCH] Bem: get rid of attrs prop and proxy all props to --- src/Bem.js | 31 ++++++++++++++++++++++++++----- tests/htmlMethods.spec.js | 17 +++++++++++++++-- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/Bem.js b/src/Bem.js index e98b7299..ec4da9d8 100644 --- a/src/Bem.js +++ b/src/Bem.js @@ -1,8 +1,32 @@ import inherit from 'inherit'; import stringify from '@bem/sdk.naming.entity.stringify'; +const bemModes = { + block : 1, + elem : 1, + addBemClassName : 1, + tag : 1, + attrs : 1, + mods : 1, + mix : 1, + cls : 1 +}; + export default function({ preset, naming }) { const { Base, classAttribute, Render, PropTypes } = preset; + const getRenderProps = function(instance, node) { + const mergedProps = { + ...node.attrs, + ...node, + [classAttribute] : instance.__cnb(node) + }; + + return Object.keys(mergedProps).reduce((props, p) => { + if(!bemModes[p]) props[p] = mergedProps[p]; + return props; + }, Object.create(null));; + }; + return inherit(Base, { __constructor() { this.__base(...arguments); @@ -39,11 +63,8 @@ export default function({ preset, naming }) { return this.__render(node); }, - __render(node) { - return Render(node.tag || 'div', { - [classAttribute] : this.__cnb(node), - ...node.attrs - }, node.children); + __render(props) { + return Render(props.tag || 'div', getRenderProps(this, props)); } }, { displayName : 'Bem', diff --git a/tests/htmlMethods.spec.js b/tests/htmlMethods.spec.js index 78fe1b84..9a694a13 100644 --- a/tests/htmlMethods.spec.js +++ b/tests/htmlMethods.spec.js @@ -14,8 +14,21 @@ describe('Entity without declaration', () => { expect(shallow().type()).toBe('b'); }); - it('Should proper attrs', () => { - expect(shallow().prop('id')) + it('Should proper inline attrs', () => { + expect(shallow().prop('id')) + .toBe('the-id'); + }); + + it('Should merge inline attrs and attrs mode', () => { + expect(shallow().props()) + .toMatchObject({ + id : 'the-id', + action : '/' + }); + }); + + it('Should have props priority', () => { + expect(shallow().prop('id')) .toBe('the-id'); }); });