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

fire hover event on supported devices only #20432

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
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
84 changes: 46 additions & 38 deletions src/components/Hoverable/index.js
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,
Expand All @@ -15,6 +17,8 @@ class Hoverable extends Component {
};

this.wrapperView = null;
// Skip hover on not supported devices like mWeb
Copy link
Collaborator

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i have removed comments

this.hasHoverSupport = DeviceCapabilities.hasHoverSupport();
}

componentDidMount() {
Expand Down Expand Up @@ -74,48 +78,52 @@ class Hoverable extends Component {
}

if (_.isFunction(child)) {
child = child(this.state.isHovered);
child = child(this.state.isHovered && this.hasHoverSupport);
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

we can use early return instead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Santhosh-Sellavel Thats good suggestion,
You are suggesting this?

 if (_.isFunction(child)) {
            child = child(this.state.isHovered && this.hasHoverSupport);
 }
  if(!this.hasHoverSupport) {
      return <View>{child}</View>
  }

Copy link
Collaborator

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 && and ternary operation.
Just pushing code.

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>
);
}
}

Expand Down