Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update FilterSelect items when props change #1310

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type Action =
| { type: "SET_QUERY"; payload: { query: string } }
| { type: "TOGGLE_SELECTED_ITEM"; payload: { item: Item } }
| { type: "SET_SELECTED_ITEMS"; payload: { items: Items } }
| { type: "SET_ITEMS"; payload: { items: Items } }

const filterSelectReducer = (state: FilterSelectState, action: Action) => {
switch (action.type) {
Expand Down Expand Up @@ -118,6 +119,15 @@ const filterSelectReducer = (state: FilterSelectState, action: Action) => {
selectedItems: items,
}
}

case "SET_ITEMS": {
const { items } = action.payload

return {
...state,
items,
}
}
}
}

Expand Down Expand Up @@ -168,6 +178,12 @@ export const FilterSelectContextProvider: React.FC<
},
})
},
setItems: (items) => {
dispatch({
type: "SET_ITEMS",
payload: { items },
})
},
}

useUpdateEffect(() => {
Expand All @@ -177,6 +193,12 @@ export const FilterSelectContextProvider: React.FC<
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.selectedItems?.length])

useUpdateEffect(() => {
if (props.items) {
contextValue.setItems(props.items)
}
}, [props.items, contextValue])

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if most of this stuff could just be taken out of state and computed in useMemos

useLayoutEffect(() => {
if (props.query?.length) {
contextValue.setQuery(props.query)
Expand Down