Skip to content

Commit b82e3b4

Browse files
authored
feat: remove unnecessary lodash map values dep (#572)
1 parent 2861a1b commit b82e3b4

File tree

10 files changed

+447
-228
lines changed

10 files changed

+447
-228
lines changed

examples/form-inputs.example.story.js

+35-23
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { action } from '@storybook/addon-actions';
77
import omitEmpty from 'omit-empty';
88
import withReadme from 'storybook-readme/with-readme';
99
import { withKnobs, select } from '@storybook/addon-knobs';
10-
import mapValues from 'lodash.mapvalues';
1110
import { FormikBox, Section } from '../.storybook/decorators';
1211
import {
1312
Text,
@@ -182,11 +181,17 @@ const validate = (formValues, locale) => {
182181
errors.slug.missing = true;
183182
} else {
184183
const isValidSlug = value => /^[a-zA-Z0-9_-]{2,256}$/.test(value);
185-
const translationErrors = mapValues(
186-
LocalizedTextInput.omitEmptyTranslations(formValues.slug),
187-
// more validation errors could be added in theory
188-
slug => (isValidSlug(slug) ? {} : { hasForbiddenChars: true })
189-
);
184+
const translationErrors = Object.keys(
185+
LocalizedTextInput.omitEmptyTranslations(formValues.slug)
186+
).reduce((acc, language) => {
187+
const value = isValidSlug(formValues.slug[language])
188+
? {}
189+
: { hasForbiddenChars: true };
190+
return {
191+
...acc,
192+
[language]: value,
193+
};
194+
}, {});
190195

191196
errors.slug.translations = translationErrors;
192197
}
@@ -349,24 +354,31 @@ class ProductForm extends React.Component {
349354
// specific messages in code means we can make use of our existing
350355
// translation mechanism. We could easily render
351356
// <FormattedMessage /> instead of rendering the warning string.
352-
mapValues(
353-
// We only show errors once the field has been touched
354-
LocalizedTextInput.isTouched(this.props.formik.touched.slug) &&
355-
this.props.formik.errors.slug
356-
? // We map on the per-field errors which are present on
357-
// the "translations" field
358-
this.props.formik.errors.slug.translations
359-
: {},
360-
error => {
361-
if (error.hasForbiddenChars) {
362-
return <ErrorMessage>This slug is not valid.</ErrorMessage>;
363-
}
364-
// we could map other errors here as well
357+
(() => {
358+
const allLocales =
359+
LocalizedTextInput.isTouched(
360+
this.props.formik.touched.slug
361+
) && this.props.formik.errors.slug
362+
? // We map on the per-field errors which are present on
363+
// the "translations" field
364+
this.props.formik.errors.slug.translations
365+
: {};
366+
return Object.entries(allLocales).reduce(
367+
(acc, [key, error]) => {
368+
const value = error.hasForbiddenChars ? (
369+
<ErrorMessage>This slug is not valid.</ErrorMessage>
370+
) : (
371+
undefined
372+
);
365373

366-
// returning undefined results in no error for that field
367-
return undefined;
368-
}
369-
)
374+
return {
375+
[key]: value,
376+
...acc,
377+
};
378+
},
379+
{}
380+
);
381+
})()
370382
}
371383
/>
372384
{LocalizedTextInput.isTouched(this.props.formik.touched.slug) &&

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
"lodash.flowright": "3.5.0",
7272
"lodash.has": "4.5.2",
7373
"lodash.isnil": "4.0.0",
74-
"lodash.mapvalues": "4.6.0",
7574
"lodash.omit": "4.5.0",
7675
"lodash.sortby": "4.7.0",
7776
"lodash.uniq": "4.5.0",

src/components/avatar/avatar.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3-
import oneLineTrim from 'common-tags/lib/oneLineTrim';
3+
import { oneLineTrim } from 'common-tags';
44
import { css } from '@emotion/core';
55
import vars from '../../../materials/custom-properties';
66
import filterDataAttributes from '../../utils/filter-data-attributes';

src/components/buttons/secondary-button/secondary-button.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import PropTypes from 'prop-types';
22
import React from 'react';
3-
import oneLine from 'common-tags/lib/oneLine';
3+
import { oneLine } from 'common-tags';
44
import { Link } from 'react-router-dom';
55
import flowRight from 'lodash.flowright';
66
import isNil from 'lodash.isnil';

src/components/fields/localized-multiline-text-field/localized-multiline-text-field.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33
import requiredIf from 'react-required-if';
4-
import oneLine from 'common-tags/lib/oneLine';
4+
import { oneLine } from 'common-tags';
55
import Constraints from '../../constraints';
66
import Spacings from '../../spacings';
77
import FieldLabel from '../../field-label';

src/components/fields/localized-text-field/localized-text-field.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33
import requiredIf from 'react-required-if';
4-
import oneLine from 'common-tags/lib/oneLine';
4+
import { oneLine } from 'common-tags';
55
import Constraints from '../../constraints';
66
import Spacings from '../../spacings';
77
import FieldLabel from '../../field-label';

src/components/inputs/localized-money-input/localized-money-input.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33
import without from 'lodash.without';
4-
import oneLine from 'common-tags/lib/oneLine';
4+
import { oneLine } from 'common-tags';
55
import { injectIntl } from 'react-intl';
66
import { css } from '@emotion/core';
77
import Spacings from '../../spacings';

src/components/inputs/localized-multiline-text-input/localized-multiline-text-input.js

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33
import requiredIf from 'react-required-if';
4-
import mapValues from 'lodash.mapvalues';
5-
import oneLine from 'common-tags/lib/oneLine';
4+
import { oneLine } from 'common-tags';
65
import { injectIntl } from 'react-intl';
76
import Spacings from '../../spacings';
87
import Constraints from '../../constraints';
@@ -95,8 +94,14 @@ export class LocalizedMultilineTextInput extends React.Component {
9594
// This state is used to show/hide the remaining translations
9695
areLanguagesOpened: this.props.defaultExpandLanguages,
9796
// This state is to manage the expand/collapse of multiline text inputs
98-
expandedTranslations: mapValues(this.props.value, () =>
99-
Boolean(this.props.defaultExpandMultilineText)
97+
expandedTranslations: Object.keys(this.props.value).reduce(
98+
(translations, locale) => {
99+
return {
100+
[locale]: Boolean(this.props.defaultExpandMultilineText),
101+
...translations,
102+
};
103+
},
104+
{}
100105
),
101106
};
102107

@@ -115,9 +120,14 @@ export class LocalizedMultilineTextInput extends React.Component {
115120

116121
expandAllTranslations = () =>
117122
this.setState(prevState => ({
118-
expandedTranslations: mapValues(
119-
prevState.expandedTranslations,
120-
() => true
123+
expandedTranslations: Object.keys(prevState.expandedTranslations).reduce(
124+
(translations, locale) => {
125+
return {
126+
[locale]: true,
127+
...translations,
128+
};
129+
},
130+
{}
121131
),
122132
}));
123133

@@ -145,6 +155,7 @@ export class LocalizedMultilineTextInput extends React.Component {
145155
this.props.selectedLanguage,
146156
Object.keys(this.props.value)
147157
);
158+
148159
return (
149160
<Constraints.Horizontal constraint={this.props.horizontalConstraint}>
150161
<Spacings.Stack scale="s">

src/components/inputs/localized-text-input/localized-text-input.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33
import requiredIf from 'react-required-if';
4-
import oneLine from 'common-tags/lib/oneLine';
4+
import { oneLine } from 'common-tags';
55
import { FormattedMessage } from 'react-intl';
66
import { css } from '@emotion/core';
77
import ErrorMessage from '../../messages/error-message';

0 commit comments

Comments
 (0)