-
-
Notifications
You must be signed in to change notification settings - Fork 187
OnClickOutside for use with ES6 classes #22
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
Conversation
I don't think it's wise to solve the problem around ES6 classes and mixins in this repo, while React itself hasn't solved this problem yet. Instead, I'd like the put up for consideration that it is more important that you file this solution on the React repositoriy on http://github.com/facebook/react/issues, so that they can have a look at what you're doing and whether that's a solution that they want to adopt as official "and here is how you do all this using ES6/ES2015". If there is consensus that this is a way forward for React, then I will be more than happy to accept PRs that implement the official solution. I'll keep this PR open for now, because if they accept your solution then we can pretty much immediately land it, but I won't land it quite yet. |
@marnusw Why not just make a higher-order component for that, that provides |
Thanks @alexkuz, based on your suggestion I have updated the solution with a higher-order component/wrapper function. This is much simpler, and I believe closer to the current de facto standard way of extending ES6 class components. I'll take it to the React team for comment from here if there is no further input. |
Nice! Go for it. I'm not a fan on the notation myself, since it is an obvious solution on first thought, but further thought reveals rather insane notation if you carry it over to complex real world UIs:
I still need to read through ECAMScript 262 v6 to see if there are neat things that ES6 allows that we haven't started taking advantage of yet... |
As I understand it the idea is that you build up the functionality of a specific component with wrappers within a single file, and then still use a single component in the parent; i.e. the wrapping is transparent outside the inner component's definition, consider: // FocussedSection.jsx
class FocussedSection extends React.Component {
handleClickOutside = (event) => {
// Remove Focus
}
}
export default listenToClickOutside(FocussedSection); // SectionList.jsx
import FocussedSection from './FocussedSection.jsx'
class SectionList extends React.Component {
render() {
return (
<div>
{sections.map(section => <FocussedSection {...section}/>)}
</div>
);
}
} The outcome is exactly the same as applying the mixin to the <ThingWrap>
<ExtensionWrapping>
<AuthFunctionality>
// No OutsideClicks or VRSupport components here if they are both wrappers
// inside the SecondaryUI file.
<SecondaryUI>Not so bad</SecondaryUI>
</AuthFunctionality>
</ExtensionWrapping>
</ThingWrap> Unless I'm missing something which is very possible... The idea stems from this blog post originally and is also used by:
I think an ES7 decorator might well be the "neat thing to take advantage of" here. I could add decorator support to this PR as well! |
@Pomax, given the most recent comments on facebook/react#5010, what are your thoughts? If you'd be willing to merge in principle I'll get this PR ready for merge. I can hardly remember the status of it, but I'm sure it still needs some docs etc. |
In principle it's fine, but it completely changes how this thing works, so it'll basically be a deprecation of everything currently in the repo, with a new major version, new docs, and new code, since it won't be a mixin anymore. |
I was thinking the two options could run in parallel? That is why I was glad that I eventually found a solution which is a minimal wrapper around the mixin allowing use as a decorator with a similar interface, i.e. you still only have to declare the Let me see if I can update the docs etc. in the next few days so it reflects the two options, then I'll check back to see what you think. Unless you definitely want to deprecate the mixin from your side. I think mixins are still very much in common use though. |
looking forward to that. One thing that might happen is that this repo gets deprecated in favour of react-onoutsideevent that came out of #45, but for the while that project has not been bower/npm published yet. |
19e212f
to
367a53e
Compare
I've updated the documentation to indicate how the higher-order component/decorator might complement the existing mixin rather than replace it. I would love to hear your thoughts. You might note that I added one extra feature in the decorator which is as yet undocumented: the ability to pass a handler down on the I looked over As a side note, I like the camel casing of the |
The outsideevent project (which is basically this mixin, but made generic) does use |
.gitignore
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hm, I'm not sure a .gitignore is necessary for these files - it's easy enough to pick them out of a PR.
If you can take out the |
Thanks @Pomax, the I'll put filing a PR on the outsideevent project on my to-do list. I might do so next weekend. |
@marnusw: thanks! One last q, would it be possible to squash this to one commit? (just to make reverts easier should they end up being necessary) |
Sure, I can do that. As far as I know if you do a merge via GitHub or using |
Squashed, ready to merge... |
awesome, thanks! |
OnClickOutside for use with ES6 classes
I'm using ES6 class React Components which means mixins aren't supported. I'd like to propose the following to enable such use. It uses the same implementation as the mixin except that the component is passed into the various functions rather than them being member functions of the component itself. This also lends itself toward some configuration when initially registering the component.
I'd like to add some documentation to the main readme etc., but I'm expecting some discussion around this and would like to confirm your interest in this solution before going there.
Excellent work on the mixin by the way!