mirror of https://github.com/grafana/grafana
Merge pull request #15382 from grafana/hugoh/bug-pressing-brackets-key-in-input-fields
Bug pressing special regexp chars in input fieldspull/15409/head
commit
960fa56033
@ -0,0 +1,51 @@ |
|||||||
|
import React, { forwardRef } from 'react'; |
||||||
|
|
||||||
|
const specialChars = ['(', '[', '{', '}', ']', ')', '|', '*', '+', '-', '.', '?', '<', '>', '#', '&', '^', '$']; |
||||||
|
|
||||||
|
export const escapeStringForRegex = (value: string) => { |
||||||
|
if (!value) { |
||||||
|
return value; |
||||||
|
} |
||||||
|
|
||||||
|
const newValue = specialChars.reduce( |
||||||
|
(escaped, currentChar) => escaped.replace(currentChar, '\\' + currentChar), |
||||||
|
value |
||||||
|
); |
||||||
|
|
||||||
|
return newValue; |
||||||
|
}; |
||||||
|
|
||||||
|
export const unEscapeStringFromRegex = (value: string) => { |
||||||
|
if (!value) { |
||||||
|
return value; |
||||||
|
} |
||||||
|
|
||||||
|
const newValue = specialChars.reduce( |
||||||
|
(escaped, currentChar) => escaped.replace('\\' + currentChar, currentChar), |
||||||
|
value |
||||||
|
); |
||||||
|
|
||||||
|
return newValue; |
||||||
|
}; |
||||||
|
|
||||||
|
export interface Props { |
||||||
|
value: string | undefined; |
||||||
|
placeholder?: string; |
||||||
|
labelClassName?: string; |
||||||
|
inputClassName?: string; |
||||||
|
onChange: (value: string) => void; |
||||||
|
} |
||||||
|
|
||||||
|
export const FilterInput = forwardRef<HTMLInputElement, Props>((props, ref) => ( |
||||||
|
<label className={props.labelClassName}> |
||||||
|
<input |
||||||
|
ref={ref} |
||||||
|
type="text" |
||||||
|
className={props.inputClassName} |
||||||
|
value={unEscapeStringFromRegex(props.value)} |
||||||
|
onChange={event => props.onChange(escapeStringForRegex(event.target.value))} |
||||||
|
placeholder={props.placeholder ? props.placeholder : null} |
||||||
|
/> |
||||||
|
<i className="gf-form-input-icon fa fa-search" /> |
||||||
|
</label> |
||||||
|
)); |
||||||
Loading…
Reference in new issue