Skip to content

Commit 8940726

Browse files
authored
fix(ui): relationship filter clearing on blur (#11021)
When using the filter controls in the list view on a relationship field, the select options would clear after clicking outside of the component then never repopulate. This caused the component to remain in an unusable state, where no options would appear unless the filter is completely removed and re-added. The reason for this is that the `react-select` component fires an `onInputChange` event on blur, and the handler that is subscribed to this event was unknowingly clearing the options. This PR also renames the various filter components, i.e. `RelationshipField` -> `RelationshipFilter`. This improves semantics and dedupes their names from the actual field components. This bug was first introduced in this PR: #10553
1 parent ae32c55 commit 8940726

File tree

6 files changed

+21
-22
lines changed

6 files changed

+21
-22
lines changed

packages/ui/src/elements/WhereBuilder/Condition/Date/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { DatePickerField } from '../../../DatePicker/index.js'
77

88
const baseClass = 'condition-value-date'
99

10-
export const DateField: React.FC<Props> = ({ disabled, field: { admin }, onChange, value }) => {
10+
export const DateFilter: React.FC<Props> = ({ disabled, field: { admin }, onChange, value }) => {
1111
const { date } = admin || {}
1212

1313
return (

packages/ui/src/elements/WhereBuilder/Condition/DefaultFilter/index.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import React from 'react'
44

55
import type { FieldCondition } from '../../types.js'
66

7-
import { DateField } from '../Date/index.js'
8-
import { NumberField } from '../Number/index.js'
9-
import { RelationshipField } from '../Relationship/index.js'
7+
import { DateFilter } from '../Date/index.js'
8+
import { NumberFilter } from '../Number/index.js'
9+
import { RelationshipFilter } from '../Relationship/index.js'
1010
import { Select } from '../Select/index.js'
1111
import { Text } from '../Text/index.js'
1212

@@ -45,7 +45,7 @@ export const DefaultFilter: React.FC<Props> = ({
4545
switch (internalField?.field?.type) {
4646
case 'date': {
4747
return (
48-
<DateField
48+
<DateFilter
4949
disabled={disabled}
5050
field={internalField.field}
5151
onChange={onChange}
@@ -57,7 +57,7 @@ export const DefaultFilter: React.FC<Props> = ({
5757

5858
case 'number': {
5959
return (
60-
<NumberField
60+
<NumberFilter
6161
disabled={disabled}
6262
field={internalField.field}
6363
onChange={onChange}
@@ -69,7 +69,7 @@ export const DefaultFilter: React.FC<Props> = ({
6969

7070
case 'relationship': {
7171
return (
72-
<RelationshipField
72+
<RelationshipFilter
7373
disabled={disabled}
7474
field={internalField.field}
7575
onChange={onChange}

packages/ui/src/elements/WhereBuilder/Condition/Number/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import './index.scss'
88

99
const baseClass = 'condition-value-number'
1010

11-
export const NumberField: React.FC<Props> = ({ disabled, onChange, value }) => {
11+
export const NumberFilter: React.FC<Props> = ({ disabled, onChange, value }) => {
1212
const { t } = useTranslation()
1313

1414
return (

packages/ui/src/elements/WhereBuilder/Condition/Relationship/index.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const baseClass = 'condition-value-relationship'
1818

1919
const maxResultsPerRequest = 10
2020

21-
export const RelationshipField: React.FC<Props> = (props) => {
21+
export const RelationshipFilter: React.FC<Props> = (props) => {
2222
const {
2323
disabled,
2424
field: { admin: { isSortable } = {}, hasMany, relationTo },
@@ -42,13 +42,15 @@ export const RelationshipField: React.FC<Props> = (props) => {
4242
const debouncedSearch = useDebounce(search, 300)
4343
const { i18n, t } = useTranslation()
4444
const relationSlugs = hasMultipleRelations ? relationTo : [relationTo]
45+
4546
const initialRelationMap = () => {
4647
const map: Map<string, number> = new Map()
4748
relationSlugs.forEach((relation) => {
4849
map.set(relation, 1)
4950
})
5051
return map
5152
}
53+
5254
const nextPageByRelationshipRef = React.useRef<Map<string, number>>(initialRelationMap())
5355
const partiallyLoadedRelationshipSlugs = React.useRef<string[]>(relationSlugs)
5456

@@ -205,11 +207,13 @@ export const RelationshipField: React.FC<Props> = (props) => {
205207
}, [hasMany, hasMultipleRelations, value, options])
206208

207209
const handleInputChange = (input: string) => {
208-
dispatchOptions({ type: 'CLEAR', i18n, required: false })
209-
const relationSlug = partiallyLoadedRelationshipSlugs.current[0]
210-
partiallyLoadedRelationshipSlugs.current = relationSlugs
211-
nextPageByRelationshipRef.current.set(relationSlug, 1)
212-
setSearch(input)
210+
if (input !== search) {
211+
dispatchOptions({ type: 'CLEAR', i18n, required: false })
212+
const relationSlug = partiallyLoadedRelationshipSlugs.current[0]
213+
partiallyLoadedRelationshipSlugs.current = relationSlugs
214+
nextPageByRelationshipRef.current.set(relationSlug, 1)
215+
setSearch(input)
216+
}
213217
}
214218

215219
const addOptionByID = useCallback(

packages/ui/src/exports/client/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,9 @@ export { TranslationProvider, useTranslation } from '../../providers/Translation
276276
export { useWindowInfo, WindowInfoProvider } from '../../providers/WindowInfo/index.js'
277277
export { Text as TextCondition } from '../../elements/WhereBuilder/Condition/Text/index.js'
278278
export { Select as SelectCondition } from '../../elements/WhereBuilder/Condition/Select/index.js'
279-
export { RelationshipField as RelationshipCondition } from '../../elements/WhereBuilder/Condition/Relationship/index.js'
280-
export { NumberField as NumberCondition } from '../../elements/WhereBuilder/Condition/Number/index.js'
281-
export { DateField as DateCondition } from '../../elements/WhereBuilder/Condition/Date/index.js'
279+
export { RelationshipFilter as RelationshipCondition } from '../../elements/WhereBuilder/Condition/Relationship/index.js'
280+
export { NumberFilter as NumberCondition } from '../../elements/WhereBuilder/Condition/Number/index.js'
281+
export { DateFilter as DateCondition } from '../../elements/WhereBuilder/Condition/Date/index.js'
282282
export { EmailAndUsernameFields } from '../../elements/EmailAndUsername/index.js'
283283
export { SelectAll } from '../../elements/SelectAll/index.js'
284284
export { SelectRow } from '../../elements/SelectRow/index.js'

test/fields-relationship/payload-types.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,6 @@ export interface FieldsRelationship {
118118
* This will filter the relationship options based on id, which is the same as the relationship field in this document
119119
*/
120120
relationshipFilteredByID?: (string | null) | RelationOne;
121-
/**
122-
* This will filter the relationship options if the filter field in this document is set to "Include me"
123-
*/
124-
relationshipFilteredByField?: (string | null) | RelationOne;
125121
relationshipFilteredAsync?: (string | null) | RelationOne;
126122
relationshipManyFiltered?:
127123
| (
@@ -450,7 +446,6 @@ export interface FieldsRelationshipSelect<T extends boolean = true> {
450446
relationshipRestricted?: T;
451447
relationshipWithTitle?: T;
452448
relationshipFilteredByID?: T;
453-
relationshipFilteredByField?: T;
454449
relationshipFilteredAsync?: T;
455450
relationshipManyFiltered?: T;
456451
filter?: T;

0 commit comments

Comments
 (0)