Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
refactor(lodash): has (#2434)
Browse files Browse the repository at this point in the history
* refactor(lodash): has

replace mostly by item[key] === undefined, which isn't completely the same, but close enough in our case, since rendering undefined would be quite weird.

BREAKING CHANGE:
translation will render default value if passed undefined as value

* chore(lodash): remove imports

* fix(translation): allow undefined value to be passed on purpose
  • Loading branch information
Haroenv committed Jun 27, 2019
1 parent 2f276a1 commit 75a4a15
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ describe('translatable', () => {
const defaultTranslations = {
sup: 'hey',
thing: n => `${n} things`,
fallbackThing: 'hi',
};
const translations = {
sup: 'hoy',
fallbackThing: undefined,
};
const Translated = translatable(defaultTranslations)(Dummy);
const { translate } = shallow(<Translated translations={translations} />)
.find(Dummy)
.props();
expect(translate('sup')).toBe('hoy');
expect(translate('thing', 20)).toBe('20 things');
expect(translate('fallbackThing')).toBe(undefined);
});
});
3 changes: 1 addition & 2 deletions packages/react-instantsearch-core/src/core/translatable.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { Component } from 'react';
import { has } from 'lodash';

const withKeysPropType = keys => (props, propName, componentName) => {
const prop = props[propName];
Expand All @@ -23,7 +22,7 @@ export default function translatable(defaultTranslations) {
const { translations } = this.props;

const translation =
translations && has(translations, key)
translations && translations.hasOwnProperty(key)
? translations[key]
: defaultTranslations[key];

Expand Down
7 changes: 3 additions & 4 deletions packages/react-instantsearch-dom/src/components/LinkList.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { has } from 'lodash';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Link from './Link';
Expand Down Expand Up @@ -35,7 +34,7 @@ export default class LinkList extends Component {
<ul className={cx('list', !canRefine && 'list--noRefinement')}>
{items.map(item => (
<li
key={has(item, 'key') ? item.key : item.value}
key={item.key === undefined ? item.value : item.key}
className={cx(
'item',
item.selected && !item.disabled && 'item--selected',
Expand All @@ -45,7 +44,7 @@ export default class LinkList extends Component {
>
{item.disabled ? (
<span className={cx('link')}>
{has(item, 'label') ? item.label : item.value}
{item.label === undefined ? item.value : item.label}
</span>
) : (
<Link
Expand All @@ -54,7 +53,7 @@ export default class LinkList extends Component {
href={createURL(item.value)}
onClick={() => onSelect(item.value)}
>
{has(item, 'label') ? item.label : item.value}
{item.label === undefined ? item.value : item.label}
</Link>
)}
</li>
Expand Down
5 changes: 2 additions & 3 deletions packages/react-instantsearch-dom/src/components/Select.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { has } from 'lodash';
import React, { Component } from 'react';
import PropTypes from 'prop-types';

Expand Down Expand Up @@ -36,11 +35,11 @@ export default class Select extends Component {
{items.map(item => (
<option
className={cx('option')}
key={has(item, 'key') ? item.key : item.value}
key={item.key === undefined ? item.value : item.key}
disabled={item.disabled}
value={item.value}
>
{has(item, 'label') ? item.label : item.value}
{item.label === undefined ? item.value : item.label}
</option>
))}
</select>
Expand Down

0 comments on commit 75a4a15

Please sign in to comment.