@@ -29,9 +29,10 @@ const buttonGroupStyle = { width: '60px' }
29
29
* Extract just the subset of fields to build the state variable.
30
30
*/
31
31
function extractStateFields ( props : Props ) : Substitution {
32
- const { description, normalizeSpace, pattern, replacement } = props . substitution
32
+ const { description, invalid , normalizeSpace, pattern, replacement } = props . substitution
33
33
return {
34
34
description : description || '' ,
35
+ invalid,
35
36
normalizeSpace,
36
37
pattern,
37
38
replacement
@@ -41,10 +42,17 @@ function extractStateFields (props: Props): Substitution {
41
42
/**
42
43
* @returns true if the given substitution pattern is null or empty, false otherwise.
43
44
*/
44
- export function isSubstitutionInvalid ( substitution : Substitution ) {
45
+ export function isSubstitutionBlank ( substitution : Substitution ) {
45
46
return ! substitution . pattern || substitution . pattern === ''
46
47
}
47
48
49
+ /**
50
+ * @returns true if the given substitution is marked invalid by the backend, false otherwise.
51
+ */
52
+ export function isSubstitutionInvalid ( substitution : Substitution ) {
53
+ return substitution . invalid
54
+ }
55
+
48
56
/**
49
57
* Renders and lets user edit a substitution.
50
58
*/
@@ -137,18 +145,29 @@ export default class SubstitutionRow extends Component<Props, Substitution> {
137
145
}
138
146
139
147
render ( ) {
140
- const { activeEditingIndex } = this . props
141
- const { description , normalizeSpace, pattern, replacement } = this . state
148
+ const { activeEditingIndex, substitution : originalSubstitution } = this . props
149
+ const { description , invalid , normalizeSpace, pattern, replacement } = this . state
142
150
const regexTestLink = `https://regexr.com/?expression=${ encodeURIComponent ( pattern ) } &text=${ STRING_FOR_REGEXR_TEST } `
143
151
const isEditing = this . _isEditing ( )
144
152
const allowEdit = activeEditingIndex === - 1
145
- const isEditingInvalid = isSubstitutionInvalid ( this . state )
153
+ const isPatternBlank = isSubstitutionBlank ( this . state )
154
+ const isPatternInvalid = invalid && pattern === originalSubstitution . pattern
155
+ const isEditingInvalid = isPatternBlank || isPatternInvalid
156
+ let validationMessage = null
157
+ if ( isPatternBlank ) {
158
+ validationMessage = 'The substitution search pattern cannot be empty'
159
+ } else if ( isPatternInvalid ) {
160
+ validationMessage = `The substitution search pattern '${ originalSubstitution . pattern } ' is invalid`
161
+ }
146
162
147
163
// Construct CSS class for row
148
164
const editingClassName = isEditing ? '' : 'inactive'
149
165
const allowEditClassName = allowEdit ? 'allow-edit' : ''
150
166
const rowClassName = `substitution-row ${ editingClassName } ${ allowEditClassName } `
151
167
168
+ // Override style for inputs with invalid search patterns.
169
+ const patternStyleOverride = isPatternInvalid ? { borderColor : 'inherit' } : null
170
+
152
171
return (
153
172
< tr className = { rowClassName } >
154
173
< td >
@@ -163,10 +182,11 @@ export default class SubstitutionRow extends Component<Props, Substitution> {
163
182
onKeyDown = { this . _onKeyDown }
164
183
placeholder = 'Text (regex) to find'
165
184
readOnly = { ! isEditing }
166
- title = { isEditingInvalid ? 'Substitution search pattern cannot be empty' : null }
185
+ style = { patternStyleOverride }
186
+ title = { validationMessage }
167
187
value = { pattern }
168
188
/>
169
- < InputGroup . Addon >
189
+ < InputGroup . Addon style = { patternStyleOverride } >
170
190
< a
171
191
href = { regexTestLink }
172
192
target = '_blank'
@@ -217,9 +237,9 @@ export default class SubstitutionRow extends Component<Props, Substitution> {
217
237
< ButtonGroup style = { buttonGroupStyle } >
218
238
< Button
219
239
bsSize = 'xsmall'
220
- disabled = { isEditingInvalid }
240
+ disabled = { isPatternInvalid }
221
241
onClick = { this . _onClickSave }
222
- title = { isEditingInvalid ? 'Unable to save, there are errors in this row.' : 'Save changes in this row' }
242
+ title = { isPatternInvalid ? 'Unable to save, there are errors in this row.' : 'Save changes in this row' }
223
243
>
224
244
< Icon type = 'check' />
225
245
</ Button >
0 commit comments