TheSpicyMeatball / react-nanny

Utils to manage your React Children
Other
85 stars 4 forks source link

Skip going deeper when an element is found using getChildrenByType #17

Closed Timbo925 closed 2 years ago

Timbo925 commented 2 years ago

Hi!

I had trouble using this lib when trying out getChildrenByTypeDeep, with my nested custom components.

The behaviour I wanted was fetching CustomComponentGroup or CustomComponent but not go any deeper in the search tree when a match is found. The current behaviour would return me all 5 matches, while it would like to tweak the searching behaviour so it won't return any CustomComponents found inside the CustomComponentGroup. This is illustrated in the updated test cases.

To solve this, i added a custom parameter skipWhenFound (by default false), that will skip the search if something was found. This way the old behaviour is intact, but the option exist to turn on the new feature.

Added test cases to the getChildrenByTypeDeep file showing this would be a non breaking change. Any comments always welcome ;)

TheSpicyMeatball commented 2 years ago

Merged and published to 2.15.0. Thanks for your idea and contribution!