fix(ToolbarFilter): Clear filters on unmount#12266
fix(ToolbarFilter): Clear filters on unmount#12266fallmo wants to merge 2 commits intopatternfly:mainfrom
Conversation
WalkthroughThe change adds a missing Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment Tip CodeRabbit can scan for known vulnerabilities in your dependencies using OSV Scanner.OSV Scanner will automatically detect and report security vulnerabilities in your project's dependencies. No additional configuration is required. |
|
Preview: https://pf-react-pr-12266.surge.sh A11y report: https://pf-react-pr-12266-a11y.surge.sh |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/react-core/src/components/Toolbar/ToolbarFilter.tsx`:
- Around line 77-79: When categoryName changes we need to clear the old key
before setting the new count: update componentDidUpdate to accept prevProps,
compare prevProps.categoryName to this.props.categoryName, and if different call
this.context.updateNumberFilters(getCategoryKey(prevProps.categoryName), 0) to
clear the previous entry, then call
this.context.updateNumberFilters(getCategoryKey(this.props.categoryName),
this.props.labels.length) to set the new count; reference componentDidUpdate,
this.context.updateNumberFilters, getCategoryKey, this.props.categoryName,
prevProps.categoryName and this.props.labels.length.
- Around line 113-121: The Label components are always receiving an onClose
prop, causing a clickable close affordance even when deleteLabel is undefined;
update the labels.map rendering in ToolbarFilter.tsx to only pass the onClose
prop when deleteLabel is truthy (e.g., conditionally include onClose={() =>
deleteLabel(categoryKey, label)} for the string branch and onClose={() =>
deleteLabel(categoryKey, label)} for the object branch), leaving Label without
onClose when deleteLabel is not provided so the close affordance is not shown.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: ca5a2792-0b7d-4709-b78b-a3e6c8c525c3
📒 Files selected for processing (1)
packages/react-core/src/components/Toolbar/ToolbarFilter.tsx
| componentDidUpdate() { | ||
| const { categoryName, labels } = this.props; | ||
| this.context.updateNumberFilters( | ||
| typeof categoryName !== 'string' && categoryName.hasOwnProperty('key') | ||
| ? categoryName.key | ||
| : categoryName.toString(), | ||
| labels.length | ||
| ); | ||
| this.context.updateNumberFilters(getCategoryKey(this.props.categoryName), this.props.labels.length); | ||
| } |
There was a problem hiding this comment.
Clear previous category count when categoryName changes
If categoryName changes without unmounting, the old key can stay populated in filterInfo, leaving stale state. Handle prevProps.categoryName and clear the previous key before updating the new one.
Suggested fix
- componentDidUpdate() {
- this.context.updateNumberFilters(getCategoryKey(this.props.categoryName), this.props.labels.length);
- }
+ componentDidUpdate(prevProps: ToolbarFilterProps) {
+ const prevCategoryKey = getCategoryKey(prevProps.categoryName);
+ const nextCategoryKey = getCategoryKey(this.props.categoryName);
+
+ if (prevCategoryKey !== nextCategoryKey) {
+ this.context.updateNumberFilters(prevCategoryKey, 0);
+ }
+
+ this.context.updateNumberFilters(nextCategoryKey, this.props.labels.length);
+ }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@packages/react-core/src/components/Toolbar/ToolbarFilter.tsx` around lines 77
- 79, When categoryName changes we need to clear the old key before setting the
new count: update componentDidUpdate to accept prevProps, compare
prevProps.categoryName to this.props.categoryName, and if different call
this.context.updateNumberFilters(getCategoryKey(prevProps.categoryName), 0) to
clear the previous entry, then call
this.context.updateNumberFilters(getCategoryKey(this.props.categoryName),
this.props.labels.length) to set the new count; reference componentDidUpdate,
this.context.updateNumberFilters, getCategoryKey, this.props.categoryName,
prevProps.categoryName and this.props.labels.length.
| {labels.map((label) => | ||
| typeof label === 'string' ? ( | ||
| <Label variant="outline" key={label} onClose={() => deleteLabel(categoryKey, label)}> | ||
| <Label variant="outline" key={label} onClose={() => deleteLabel?.(categoryKey, label)}> | ||
| {label} | ||
| </Label> | ||
| ) : ( | ||
| <Label key={label.key} onClose={() => deleteLabel(categoryKey, label)}> | ||
| <Label key={label.key} onClose={() => deleteLabel?.(categoryKey, label)}> | ||
| {label.node} | ||
| </Label> |
There was a problem hiding this comment.
Avoid no-op close affordance when deleteLabel is undefined
onClose is always passed, so labels can appear removable even when no delete handler exists; clicks then do nothing. Pass onClose only when deleteLabel is provided.
Suggested fix
- <Label variant="outline" key={label} onClose={() => deleteLabel?.(categoryKey, label)}>
+ <Label
+ variant="outline"
+ key={label}
+ onClose={deleteLabel ? () => deleteLabel(categoryKey, label) : undefined}
+ >
{label}
</Label>
) : (
- <Label key={label.key} onClose={() => deleteLabel?.(categoryKey, label)}>
+ <Label
+ key={label.key}
+ onClose={deleteLabel ? () => deleteLabel(categoryKey, label) : undefined}
+ >
{label.node}
</Label>
)🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@packages/react-core/src/components/Toolbar/ToolbarFilter.tsx` around lines
113 - 121, The Label components are always receiving an onClose prop, causing a
clickable close affordance even when deleteLabel is undefined; update the
labels.map rendering in ToolbarFilter.tsx to only pass the onClose prop when
deleteLabel is truthy (e.g., conditionally include onClose={() =>
deleteLabel(categoryKey, label)} for the string branch and onClose={() =>
deleteLabel(categoryKey, label)} for the object branch), leaving Label without
onClose when deleteLabel is not provided so the close affordance is not shown.
What:
Fixes #12247. The filter count persisted after ToolbarFilter unmounted because
updateNumberFilters was not triggered during component cleanup.
Changes:
updateNumberFiltersincomponentWillUnmountto reset filter count.getCategoryKeyto reduce duplicate code.labelGroupContentRef?.current?.firstElementChild !== nullwith a truthy checklabelGroupContentRef?.current?.firstElementChild. Previously, if the value was undefined, the check would evaluate to true.Summary by CodeRabbit