Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions src/tag/tag.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @flow
import React from 'react';
import type { DOMComponent } from '../bem-helper-types';
import { pick } from '../utils';
import { pick, stringifyBooleanProps } from '../utils';

const KNOWN_KEYS = ['key', 'className', 'children'];

Expand All @@ -17,7 +17,7 @@ export function tag(tagName: string): <Attrs: {}>(attrs?: Attrs) => DOMComponent
const Tag = props =>
React.createElement(tagName, {
...attrs,
...prune(props),
...prune(stringifyBooleanProps(props)),
});
Tag.displayName = `tag(${tagName})`;
return Tag;
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export { decapitalize } from './decapitalize';
export { kebabCase } from './kebab-case';
export { kebabToCamelCase } from './kebab-to-camel-case';
export { classNamesList } from './class-names-list';
export { stringifyBooleanProps } from './stringify-boolean-props';
13 changes: 13 additions & 0 deletions src/utils/stringify-boolean-props.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @flow

const BOOLEAN_DOM_ATTRIBUTES = ['checked', 'selected', 'disabled', 'readonly', 'multiple', 'ismap'];

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Деградация к React 15.
Лучше идти от attrs.


export function stringifyBooleanProps(obj?: {} = {}): { [string]: mixed } {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

нужна сериализация в строку как для булевых так и для других типов атрибутов

return Object.entries(obj).reduce(
(acc, [key, value]) =>
(typeof obj[key] === 'boolean' && !BOOLEAN_DOM_ATTRIBUTES.includes(key)
? { ...acc, [key]: String(value) }

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

для false в новой спеке лучше вообще выбрасывать ключ

: { ...acc, [key]: value }),
{},
);
}
25 changes: 25 additions & 0 deletions src/utils/stringify-boolean-props.spec.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// @flow
import { stringifyBooleanProps } from './stringify-boolean-props';

describe('stringifyBooleanProps', () => {
it('should transform boolean properties to string', () => {
expect(stringifyBooleanProps({ foo: true })).toEqual({ foo: 'true' });
expect(stringifyBooleanProps({ foo: true, bar: 1 })).toEqual({
foo: 'true',
bar: 1,
});
});

it('should NOT transform standard boolean DOM attributes', () => {
expect(stringifyBooleanProps({ foo: true, checked: true, disabled: false })).toEqual({
foo: 'true',
checked: true,
disabled: false,
});
});

it('should return object literal if null provided', () => {
expect(stringifyBooleanProps(undefined)).toEqual({});
expect(stringifyBooleanProps({})).toEqual({});
});
});