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

feat(icon-button): exposes ref prop #199

Merged
merged 4 commits into from
Mar 2, 2021
Merged

Conversation

akornmeier
Copy link
Contributor

update: icon button ref was just internal for the tooltip, adds ability to use ref freely

@akornmeier akornmeier requested review from scurker, schne324 and okry March 1, 2021 23:50
Comment on lines 24 to 26
if (typeof ref === 'function') {
ref(buttonRef.current);
}
Copy link
Member

@scurker scurker Mar 2, 2021

Choose a reason for hiding this comment

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

I think we may want to use useImperativeHandle here:

useImperativeHandle(ref, () => buttonRef.current)

That way the ref passed as props below can continue to use the internal ref value, whether it was passed from the parent as a ref or functional ref, or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What is the difference between the imperativeHandle and what is implemented in the PR? The TS compiler really REALLY does not like: useImperativeHandle(ref, () => buttonRef.current)

Copy link
Contributor Author

@akornmeier akornmeier Mar 2, 2021

Choose a reason for hiding this comment

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

From what i see useImperativeHandle is more suited to provide a subset of (in this case) <HTMLButton> properties to the forwardRef like "focus". I would think we want to expose the full ref, which is what is implemented.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah, nevermind... i got this to compile, changes inbound and down.

Copy link
Member

Choose a reason for hiding this comment

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

FWIW, I was able to get it working with this as well:

function IconButton({
  icon,
  label,
  tooltipPlacement = 'auto',
  className,
  ...other
}: IconButtonProps, ref: React.Ref<HTMLButtonElement>) {
  const buttonRef = useRef<HTMLButtonElement>(null);
  useImperativeHandle(ref, () => buttonRef.current as HTMLButtonElement)
  return (...)
}

What you have is probably fine too.

@akornmeier akornmeier requested a review from scurker March 2, 2021 18:13
Comment on lines 47 to 52
IconButton.propTypes = {
icon: PropTypes.string,
label: PropTypes.string,
tooltipPlayemnt: PropTypes.string,
buttonRef: PropTypes.any
};
Copy link
Member

Choose a reason for hiding this comment

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

I don't care about propTypes here as I find them pretty useless, but others have complained when they were removed. cc @schne324

Copy link
Contributor Author

@akornmeier akornmeier Mar 2, 2021

Choose a reason for hiding this comment

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

buttonRef was actually not exposed properly w/ prop types and they were being problematic with forwardRef + Typescript, they become essentially extraneous with TS and interfaces

Copy link
Contributor Author

Choose a reason for hiding this comment

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

also: tooltipPlayemnt

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 can put them back, however they were not even properly enforcing required props.

Copy link
Member

Choose a reason for hiding this comment

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

Put them back, the rest of the library has them so for now (even though I think they're pointless) let's keep them

Copy link
Contributor Author

Choose a reason for hiding this comment

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

word. i'll fix them up to enforce the required props n stuff

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added the PropTypes object back.

scurker
scurker previously approved these changes Mar 2, 2021
Copy link
Member

@scurker scurker left a comment

Choose a reason for hiding this comment

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

Approving, but I'd like @schne324's to weigh in on removing the propTypes as well.

@schne324
Copy link
Member

schne324 commented Mar 2, 2021

to weigh in on removing the propTypes as well.

@stephenmathieson iirc you had some concerns with removing them. Thoughts here?

@akornmeier akornmeier merged commit 15ccc98 into develop Mar 2, 2021
@akornmeier akornmeier deleted the feat/icon-button-ref branch March 2, 2021 20:28
@stephenmathieson
Copy link
Member

@stephenmathieson iirc you had some concerns with removing them. Thoughts here?

Only reason I suggest we keep them around is for non-TypeScript users. I agree that they're pointless if you're using TS, but for JS apps, they're pretty handy.

Not a big deal to me either way tho 🤷

@akornmeier
Copy link
Contributor Author

valid point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants