material-components / material-components-web-react

Material Components for React (MDC React)
MIT License
2.02k stars 227 forks source link

Forwarding refs? #1071

Open emilmuller opened 3 years ago

emilmuller commented 3 years ago

I'm using a Button component from a different library, and I want to add a ripple to it.

But it is impossible to get a ref to it because withRipple doesn't forward the ref. I.e. <ButtonWithRipple ref={myref} /> does not get forwarded.

import { withRipple, InjectedProps } from '@material/react-ripple';

interface ButtonWithRippleProps extends ButtonProps, InjectedProps<HTMLElement> {
  initRipple: React.Ref<HTMLElement>;
  unbounded: boolean;
}

const ButtonWithRipple = withRipple<ButtonWithRippleProps, HTMLElement>((props: ButtonWithRippleProps) => {
  const { initRipple, unbounded, ...otherProps } = props;
  return <Button ref={initRipple} {...otherProps} />;
});