-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
fire hover event on supported devices only #20432
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import _ from 'underscore'; | ||
import React, {Component} from 'react'; | ||
import {View} from 'react-native'; | ||
import {propTypes, defaultProps} from './hoverablePropTypes'; | ||
import * as DeviceCapabilities from '../../libs/DeviceCapabilities'; | ||
|
||
/** | ||
* It is necessary to create a Hoverable component instead of relying solely on Pressable support for hover state, | ||
|
@@ -15,6 +17,8 @@ class Hoverable extends Component { | |
}; | ||
|
||
this.wrapperView = null; | ||
// Skip hover on not supported devices like mWeb | ||
this.hasHoverSupport = DeviceCapabilities.hasHoverSupport(); | ||
} | ||
|
||
componentDidMount() { | ||
|
@@ -74,48 +78,52 @@ class Hoverable extends Component { | |
} | ||
|
||
if (_.isFunction(child)) { | ||
child = child(this.state.isHovered); | ||
child = child(this.state.isHovered && this.hasHoverSupport); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can use early return instead There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Santhosh-Sellavel Thats good suggestion, if (_.isFunction(child)) {
child = child(this.state.isHovered && this.hasHoverSupport);
}
if(!this.hasHoverSupport) {
return <View>{child}</View>
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Look if there is any better ways refactor if not lets just proceed with above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let child = this.props.children;
if (_.isArray(this.props.children) && this.props.children.length === 1) {
child = this.props.children[0];
}
// return child if device doesn't has hoverSupport
if (!this.hasHoverSupport) {
const childrenWithHoverState = _.isFunction(child) ? child(false) : child;
return <View >{childrenWithHoverState}</View>;
}
if (_.isFunction(child)) {
child = child(this.state.isHovered);
} This looks good to me. as we are not doing |
||
return React.cloneElement(React.Children.only(child), { | ||
ref: (el) => { | ||
this.wrapperView = el; | ||
|
||
// Call the original ref, if any | ||
const {ref} = child; | ||
if (_.isFunction(ref)) { | ||
ref(el); | ||
return; | ||
} | ||
|
||
if (_.isObject(ref)) { | ||
ref.current = el; | ||
} | ||
}, | ||
onMouseEnter: (el) => { | ||
this.setIsHovered(true); | ||
|
||
if (_.isFunction(child.props.onMouseEnter)) { | ||
child.props.onMouseEnter(el); | ||
} | ||
}, | ||
onMouseLeave: (el) => { | ||
this.setIsHovered(false); | ||
|
||
if (_.isFunction(child.props.onMouseLeave)) { | ||
child.props.onMouseLeave(el); | ||
} | ||
}, | ||
onBlur: (el) => { | ||
if (!this.wrapperView.contains(el.relatedTarget)) { | ||
return this.hasHoverSupport ? ( | ||
React.cloneElement(React.Children.only(child), { | ||
ref: (el) => { | ||
this.wrapperView = el; | ||
|
||
// Call the original ref, if any | ||
const {ref} = child; | ||
if (_.isFunction(ref)) { | ||
ref(el); | ||
return; | ||
} | ||
|
||
if (_.isObject(ref)) { | ||
ref.current = el; | ||
} | ||
}, | ||
onMouseEnter: (el) => { | ||
this.setIsHovered(true); | ||
|
||
if (_.isFunction(child.props.onMouseEnter)) { | ||
child.props.onMouseEnter(el); | ||
} | ||
}, | ||
onMouseLeave: (el) => { | ||
this.setIsHovered(false); | ||
} | ||
|
||
if (_.isFunction(child.props.onBlur)) { | ||
child.props.onBlur(el); | ||
} | ||
}, | ||
}); | ||
if (_.isFunction(child.props.onMouseLeave)) { | ||
child.props.onMouseLeave(el); | ||
} | ||
}, | ||
onBlur: (el) => { | ||
if (!this.wrapperView.contains(el.relatedTarget)) { | ||
this.setIsHovered(false); | ||
} | ||
|
||
if (_.isFunction(child.props.onBlur)) { | ||
child.props.onBlur(el); | ||
} | ||
}, | ||
}) | ||
) : ( | ||
<View>{child}</View> | ||
); | ||
} | ||
} | ||
|
||
|
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.
can remove this comment too
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.
i have removed comments