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

[core] Switch from buttonRef to ref usage #15296

Merged
merged 1 commit into from
Apr 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function Control(props) {
function Option(props) {
return (
<MenuItem
buttonRef={props.innerRef}
ref={props.innerRef}
selected={props.isFocused}
component="div"
style={{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ function Control(props: ControlProps<OptionType>) {
function Option(props: OptionProps<OptionType>) {
return (
<MenuItem
buttonRef={props.innerRef}
ref={props.innerRef}
selected={props.isFocused}
component="div"
style={{
Expand Down
8 changes: 4 additions & 4 deletions docs/src/pages/demos/menus/MenuListComposition.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ const useStyles = makeStyles(theme => ({
function MenuListComposition() {
const classes = useStyles();
const [open, setOpen] = React.useState(false);
const anchorEl = React.useRef(null);
const anchorRef = React.useRef(null);

function handleToggle() {
setOpen(!open);
}

function handleClose(event) {
if (anchorEl.current && anchorEl.current.contains(event.target)) {
if (anchorRef.current && anchorRef.current.contains(event.target)) {
return;
}

Expand All @@ -45,14 +45,14 @@ function MenuListComposition() {
</Paper>
<div>
<Button
buttonRef={anchorEl}
ref={anchorRef}
aria-owns={open ? 'menu-list-grow' : undefined}
aria-haspopup="true"
onClick={handleToggle}
>
Toggle Menu Grow
</Button>
<Popper open={open} anchorEl={anchorEl.current} transition disablePortal>
<Popper open={open} anchorEl={anchorRef.current} transition disablePortal>
{({ TransitionProps, placement }) => (
<Grow
{...TransitionProps}
Expand Down
8 changes: 4 additions & 4 deletions docs/src/pages/demos/menus/MenuListComposition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ const useStyles = makeStyles((theme: Theme) =>
function MenuListComposition() {
const classes = useStyles();
const [open, setOpen] = React.useState(false);
const anchorEl = React.useRef<null | HTMLElement>(null);
const anchorRef = React.useRef<HTMLButtonElement>(null);

function handleToggle() {
setOpen(!open);
}

function handleClose(event: React.MouseEvent<EventTarget>) {
if (anchorEl.current && anchorEl.current.contains(event.target as HTMLElement)) {
if (anchorRef.current && anchorRef.current.contains(event.target as HTMLElement)) {
return;
}

Expand All @@ -47,14 +47,14 @@ function MenuListComposition() {
</Paper>
<div>
<Button
buttonRef={anchorEl}
ref={anchorRef}
aria-owns={open ? 'menu-list-grow' : undefined}
aria-haspopup="true"
onClick={handleToggle}
>
Toggle Menu Grow
</Button>
<Popper open={open} anchorEl={anchorEl.current} transition disablePortal>
<Popper open={open} anchorEl={anchorRef.current} transition disablePortal>
{({ TransitionProps, placement }) => (
<Grow
{...TransitionProps}
Expand Down
12 changes: 4 additions & 8 deletions docs/src/pages/utils/popover/AnchorPlayground.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ const inlineStyles = {
};

class AnchorPlayground extends React.Component {
anchorRef = React.createRef();

state = {
open: false,
anchorOriginVertical: 'top',
Expand Down Expand Up @@ -144,13 +146,7 @@ class AnchorPlayground extends React.Component {
<div>
<Grid container justify="center">
<Grid item className={classes.buttonWrapper}>
<Button
buttonRef={node => {
this.anchorEl = node;
}}
variant="contained"
onClick={this.handleClickButton}
>
<Button ref={this.anchorRef} variant="contained" onClick={this.handleClickButton}>
Open Popover
</Button>
{anchorReference === 'anchorEl' && (
Expand All @@ -166,7 +162,7 @@ class AnchorPlayground extends React.Component {
</Grid>
<Popover
open={open}
anchorEl={this.anchorEl}
anchorEl={this.anchorRef.current}
anchorReference={anchorReference}
anchorPosition={{ top: positionTop, left: positionLeft }}
onClose={this.handleClose}
Expand Down
8 changes: 4 additions & 4 deletions docs/src/pages/utils/popper/ScrollPlayground.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ const styles = theme => ({
});

class ScrollPlayground extends React.Component {
anchorRef = React.createRef();

state = {
arrow: false,
arrowRef: null,
Expand Down Expand Up @@ -182,9 +184,7 @@ class ScrollPlayground extends React.Component {
<Grid className={classes.scroll} container alignItems="center" justify="center">
<div>
<Button
buttonRef={node => {
this.anchorEl = node;
}}
ref={this.anchorRef}
variant="contained"
onClick={this.handleClickButton}
aria-describedby={id}
Expand All @@ -198,7 +198,7 @@ class ScrollPlayground extends React.Component {
<Popper
id={id}
open={open}
anchorEl={this.anchorEl}
anchorEl={this.anchorRef.current}
placement={placement}
disablePortal={disablePortal}
className={classes.popper}
Expand Down
8 changes: 4 additions & 4 deletions packages/material-ui-lab/src/SpeedDial/SpeedDial.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class SpeedDial extends React.Component {
render() {
const {
ariaLabel,
ButtonProps: { buttonRef: origDialButtonRef, ...ButtonProps } = {},
ButtonProps: { ref: origDialButtonRef, ...ButtonProps } = {},
children: childrenProp,
classes,
className: classNameProp,
Expand Down Expand Up @@ -202,10 +202,10 @@ class SpeedDial extends React.Component {
const delay = 30 * (open ? validChildCount : totalValidChildren - validChildCount);
validChildCount += 1;

const { ButtonProps: { buttonRef: origButtonRef, ...ChildButtonProps } = {} } = child.props;
const { ButtonProps: { ref: origButtonRef, ...ChildButtonProps } = {} } = child.props;
const NewChildButtonProps = {
...ChildButtonProps,
buttonRef: this.createHandleSpeedDialActionButtonRef(validChildCount - 1, origButtonRef),
ref: this.createHandleSpeedDialActionButtonRef(validChildCount - 1, origButtonRef),
};

return React.cloneElement(child, {
Expand Down Expand Up @@ -259,7 +259,7 @@ class SpeedDial extends React.Component {
className={classes.fab}
{...clickProp}
{...ButtonProps}
buttonRef={ref => {
ref={ref => {
this.actions[0] = ref;
setRef(origDialButtonRef, ref);
}}
Expand Down
4 changes: 2 additions & 2 deletions packages/material-ui-lab/src/SpeedDial/SpeedDial.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ describe('<SpeedDial />', () => {
<SpeedDial
{...defaultProps}
ButtonProps={{
buttonRef: ref => {
ref: ref => {
dialButtonRef = ref;
},
}}
Expand All @@ -246,7 +246,7 @@ describe('<SpeedDial />', () => {
<SpeedDialAction
key={i}
ButtonProps={{
buttonRef: ref => {
ref: ref => {
actionRefs[i] = ref;
},
}}
Expand Down
5 changes: 4 additions & 1 deletion packages/material-ui/src/ButtonBase/ButtonBase.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
export interface ButtonBaseTypeMap {
props: {
action?: (actions: ButtonBaseActions) => void;
buttonRef?: React.Ref<any> | React.RefObject<any>;
/**
* Prefer `ref` instead.
*/
buttonRef?: React.Ref<unknown>;
centerRipple?: boolean;
disabled?: boolean;
disableRipple?: boolean;
Expand Down
8 changes: 4 additions & 4 deletions test/regressions/tests/Menu/LongMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ const options = [
const ITEM_HEIGHT = 48;

class LongMenu extends React.Component {
buttonRef = React.createRef();

state = {
anchorEl: null,
};

componentDidMount() {
this.setState({ anchorEl: this.buttonRef });
this.setState({ anchorEl: this.buttonRef.current });
}

render() {
Expand All @@ -50,9 +52,7 @@ class LongMenu extends React.Component {
return (
<div className={classes.root}>
<IconButton
buttonRef={ref => {
this.buttonRef = ref;
}}
ref={this.buttonRef}
aria-label="More"
aria-owns={open ? 'long-menu' : undefined}
aria-haspopup="true"
Expand Down