mirror of https://github.com/grafana/grafana
Fixes bug #12972 with a new type of input that escapes and unescapes special regexp characters
parent
1407691de2
commit
5388541fd7
@ -0,0 +1,48 @@ |
||||
import React, { ChangeEvent, forwardRef } from 'react'; |
||||
|
||||
const specialChars = ['(', '[', '{', '}', ']', ')', '|', '*', '+', '-', '.', '?', '<', '>', '#', '&', '^', '$']; |
||||
|
||||
export const escapeStringForRegex = (event: ChangeEvent<HTMLInputElement>) => { |
||||
const value = event.target.value; |
||||
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; |
||||
className?: string; |
||||
onChange: (value: string) => void; |
||||
} |
||||
|
||||
export const RegExpSafeInput = forwardRef<HTMLInputElement, Props>((props, ref) => ( |
||||
<input |
||||
ref={ref} |
||||
type="text" |
||||
className={props.className} |
||||
value={unEscapeStringFromRegex(props.value)} |
||||
onChange={event => props.onChange(escapeStringForRegex(event))} |
||||
placeholder={props.placeholder ? props.placeholder : null} |
||||
/> |
||||
)); |
Loading…
Reference in new issue