From e7e7185cb434190ad5cfc53dfcb5baa5235241bb Mon Sep 17 00:00:00 2001 From: Ash Date: Wed, 2 Oct 2024 14:15:48 +0100 Subject: [PATCH] fix(sanity): allow global search "contains" filter to match inside words (#7572) --- .../search/definitions/operators/stringOperators.test.ts | 7 ++++++- .../navbar/search/definitions/operators/stringOperators.ts | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/sanity/src/core/studio/components/navbar/search/definitions/operators/stringOperators.test.ts b/packages/sanity/src/core/studio/components/navbar/search/definitions/operators/stringOperators.test.ts index 3fcb6617107..1853d912e1e 100644 --- a/packages/sanity/src/core/studio/components/navbar/search/definitions/operators/stringOperators.test.ts +++ b/packages/sanity/src/core/studio/components/navbar/search/definitions/operators/stringOperators.test.ts @@ -23,7 +23,12 @@ describe('stringOperators', () => { it('should create a valid filter for stringMatches', () => { const filter = stringOperators.stringMatches.groqFilter({fieldPath, value}) - expect(filter).toEqual(`${fieldPath} match "${value}"`) + expect(filter).toEqual(`${fieldPath} match "*${value}*"`) + }) + + it('should create a valid filter for stringNotMatches', () => { + const filter = stringOperators.stringNotMatches.groqFilter({fieldPath, value}) + expect(filter).toEqual(`!(${fieldPath} match "*${value}*")`) }) it('should create a valid filter for stringNotEqual', () => { diff --git a/packages/sanity/src/core/studio/components/navbar/search/definitions/operators/stringOperators.ts b/packages/sanity/src/core/studio/components/navbar/search/definitions/operators/stringOperators.ts index 06188c70286..e51798b792d 100644 --- a/packages/sanity/src/core/studio/components/navbar/search/definitions/operators/stringOperators.ts +++ b/packages/sanity/src/core/studio/components/navbar/search/definitions/operators/stringOperators.ts @@ -37,7 +37,7 @@ export const stringOperators = { nameKey: 'search.operator.string-contains.name', descriptionKey: 'search.operator.string-contains.description', groqFilter: ({fieldPath, value}) => - value && fieldPath ? `${fieldPath} match ${toJSON(value)}` : null, + value && fieldPath ? `${fieldPath} match "*${value}*"` : null, initialValue: null, inputComponent: SearchFilterStringInput as SearchOperatorInput, type: 'stringMatches', @@ -55,7 +55,7 @@ export const stringOperators = { nameKey: 'search.operator.string-not-contains.name', descriptionKey: 'search.operator.string-not-contains.description', groqFilter: ({fieldPath, value}) => - value && fieldPath ? `!(${fieldPath} match ${toJSON(value)})` : null, + value && fieldPath ? `!(${fieldPath} match "*${value}*")` : null, initialValue: null, inputComponent: SearchFilterStringInput as SearchOperatorInput, type: 'stringNotMatches',