@@ -34,6 +34,77 @@ The supported options are:
34
34
- ` allowedStrings ` - An array of unique string values that would otherwise warn, but will be ignored.
35
35
- ` ignoreProps ` (default: ` false ` ) - When ` true ` the rule ignores literals used in props, wrapped or unwrapped.
36
36
- ` noAttributeStrings ` (default: ` false ` ) - Enforces no string literals used in attributes when set to ` true ` .
37
+ - ` elementOverrides ` - An object where the keys are the element names and the
38
+ values are objects with the same options as above. This allows you to specify
39
+ different options for different elements.
40
+
41
+ ### ` elementOverrides `
42
+
43
+ The ` elementOverrides ` option allows you to specify different options for
44
+ different elements. This is useful when you want to enforce different rules for
45
+ different elements. For example, you may want to allow string literals in
46
+ ` Button ` elements, but not in the rest of your application.
47
+
48
+ The element name only accepts component names. HTML element tag names are not supported.
49
+ Component names are case-sensitive and should exactly match the name of
50
+ the component as it is used in the JSX. It can also be the name of a compound
51
+ component (ie. ` Modal.Button ` ).
52
+
53
+ Specifying options creates a new context for the rule, so the rule will only
54
+ apply the new options to the specified element and its children (if
55
+ ` applyToNestedElements ` is ` true ` - see below). This means that the root rule options will
56
+ not apply to the specified element.
57
+
58
+ In addition to the options above (` noStrings ` , ` allowedStrings ` ,
59
+ ` noAttributeStrings ` and ` ignoreProps ` ), you can also specify the the following options
60
+ that are specific to ` elementOverrides ` :
61
+
62
+ - ` allowElement ` (default: ` false ` ) - When ` true ` the rule will allow the
63
+ specified element to have string literals as children, wrapped or unwrapped
64
+ without warning.
65
+ - ` applyToNestedElements ` (default: ` true ` ) - When ` false ` the rule will not
66
+ apply the current options set to nested elements. This is useful when you want
67
+ to apply the rule to a specific element, but not to its children.
68
+
69
+ ** Note** : As this rule has no way of differentiating
70
+ between different componets with the same name, it is recommended to use this
71
+ option with specific components that are unique to your application.
72
+
73
+ #### ` elementOverrides ` Examples
74
+
75
+ The following are ** correct** examples that demonstrate how to use the ` elementOverrides ` option:
76
+
77
+ ``` js
78
+ // "react/jsx-no-literals": [<enabled>, {"elementOverrides": { "Button": {"allowElement": true} }}]
79
+
80
+ var Hello = < div> {' test' }< / div> ;
81
+ var World = < Button> test< / Button> ;
82
+ ```
83
+
84
+ ``` js
85
+ // "react/jsx-no-literals": [<enabled>, {"elementOverrides": { "Text": {"allowElement": true} }}]
86
+
87
+ var World = < Text > Hello < a href= " a" > world< / a>< / Text > ;
88
+ ```
89
+
90
+ ``` js
91
+ // "react/jsx-no-literals": [<enabled>, {"elementOverrides": { "Text": {"allowElement": true, "applyToNestedElements": false} }}]
92
+
93
+ var linkText = ' world' ;
94
+ var World = < Text > Hello < a href= " a" > {linkText}< / a>< / Text > ;
95
+ ```
96
+
97
+ ``` js
98
+ // "react/jsx-no-literals": [<enabled>, {"noStrings": true, "elementOverrides": { "Button": {"noStrings": false} }}]
99
+ // OR
100
+ // "react/jsx-no-literals": [<enabled>, {"noStrings": true, "elementOverrides": { "Button": {} }}]
101
+
102
+ var test = ' test'
103
+ var Hello = < div> {test}< / div> ;
104
+ var World = < Button> {' test' }< / Button> ;
105
+ ```
106
+
107
+ ## Examples
37
108
38
109
To use, you can specify as follows:
39
110
0 commit comments