-
Notifications
You must be signed in to change notification settings - Fork 429
/
Copy pathhas-children-without-display-name-of.js
51 lines (40 loc) · 1.76 KB
/
has-children-without-display-name-of.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* Copyright (c) 2015-present, salesforce.com, inc. All rights reserved */
/* Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license */
/* eslint-disable import/no-mutable-exports */
import React from 'react';
// This function will deliver an error message to the browser console when all of the props passed in are undefined (falsey).
import warning from 'warning';
let hasChildrenWithoutDisplayNameOf = function hasChildrenWithoutDisplayNameOfFunction() {};
if (process.env.NODE_ENV !== 'production') {
const hasWarned = {};
// TODO: allow `displayName` to be an array of displayNames
hasChildrenWithoutDisplayNameOf = function hasChildrenWithoutDisplayNameOfFunction(
control,
children,
displayName,
comment
) {
if (!hasWarned[control]) {
const additionalComment = comment ? ` ${comment}` : '';
const childrenWithoutSelectedDisplayName = [];
React.Children.forEach(children, (child) => {
if (child && child.type && child.type.displayName !== displayName) {
// eslint-disable-next-line fp/no-mutating-methods
childrenWithoutSelectedDisplayName.push(child);
}
});
const hasChildrenWithoutSelectedDisplayName =
childrenWithoutSelectedDisplayName.length > 0;
if (hasChildrenWithoutSelectedDisplayName) {
/* eslint-disable max-len */
warning(
false,
`[Design System React] Unable to use child components specified within ${control}. Please use a child component with a \`displayName\` class property value of ${displayName}. Children without that class property are ignored. Please review \`children\` prop documentation.${additionalComment}`
);
/* eslint-enable max-len */
}
hasWarned[control] = !!hasChildrenWithoutSelectedDisplayName;
}
};
}
export default hasChildrenWithoutDisplayNameOf;