Merge branch '15766-copy-property-text' refs #15766
authorPeter Amstutz <pamstutz@veritasgenetics.com>
Thu, 31 Oct 2019 19:03:45 +0000 (15:03 -0400)
committerPeter Amstutz <pamstutz@veritasgenetics.com>
Thu, 31 Oct 2019 19:03:45 +0000 (15:03 -0400)
Arvados-DCO-1.1-Signed-off-by: Peter Amstutz <pamstutz@veritasgenetics.com>

src/common/regexp.ts [new file with mode: 0644]
src/views-components/resource-properties-form/property-key-field.tsx
src/views-components/resource-properties-form/property-value-field.tsx

diff --git a/src/common/regexp.ts b/src/common/regexp.ts
new file mode 100644 (file)
index 0000000..eca24c7
--- /dev/null
@@ -0,0 +1,6 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+export const escapeRegExp = (st: string) =>
+    st.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
index 3fb2d377aeff56574a06a919f2f33e3aea5f142e..686c858ec75ea8ba5c2104ac9f62383c3c37eb50 100644 (file)
@@ -9,6 +9,7 @@ import { Autocomplete } from '~/components/autocomplete/autocomplete';
 import { Vocabulary } from '~/models/vocabulary';
 import { connectVocabulary, VocabularyProp, buildProps } from '~/views-components/resource-properties-form/property-field-common';
 import { TAG_KEY_VALIDATION } from '~/validators/validators';
+import { escapeRegExp } from '~/common/regexp.ts';
 
 export const PROPERTY_KEY_FIELD_NAME = 'key';
 
@@ -39,8 +40,10 @@ const matchTags = (vocabulary: Vocabulary) =>
             ? undefined
             : 'Incorrect key';
 
-const getSuggestions = (value: string, vocabulary: Vocabulary) =>
-    getTagsList(vocabulary).filter(tag => tag.includes(value) && tag !== value);
+const getSuggestions = (value: string, vocabulary: Vocabulary) => {
+    const re = new RegExp(escapeRegExp(value), "i");
+    return getTagsList(vocabulary).filter(tag => re.test(tag) && tag !== value);
+};
 
 const getTagsList = ({ tags }: Vocabulary) =>
     Object.keys(tags);
index 13dcfeb544278acf62db0b41aca9c113333043d3..c8634acf4e386c071f684064197a53521e389c7f 100644 (file)
@@ -10,6 +10,7 @@ import { Vocabulary } from '~/models/vocabulary';
 import { PROPERTY_KEY_FIELD_NAME } from '~/views-components/resource-properties-form/property-key-field';
 import { VocabularyProp, connectVocabulary, buildProps } from '~/views-components/resource-properties-form/property-field-common';
 import { TAG_VALUE_VALIDATION } from '~/validators/validators';
+import { escapeRegExp } from '~/common/regexp.ts';
 
 interface PropertyKeyProp {
     propertyKey: string;
@@ -48,8 +49,10 @@ const matchTagValues = ({ vocabulary, propertyKey }: PropertyValueFieldProps) =>
             ? undefined
             : 'Incorrect value';
 
-const getSuggestions = (value: string, tagName: string, vocabulary: Vocabulary) =>
-    getTagValues(tagName, vocabulary).filter(v => v.includes(value) && v !== value);
+const getSuggestions = (value: string, tagName: string, vocabulary: Vocabulary) => {
+    const re = new RegExp(escapeRegExp(value), "i");
+    return getTagValues(tagName, vocabulary).filter(v => re.test(v) && v !== value);
+};
 
 const isStrictTag = (tagName: string, vocabulary: Vocabulary) => {
     const tag = vocabulary.tags[tagName];