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] Introduce unstable_useId hook #20407

Merged
merged 7 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
11 changes: 2 additions & 9 deletions packages/material-ui-lab/src/Rating/Rating.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
import clsx from 'clsx';
import { chainPropTypes } from '@material-ui/utils';
import { useTheme, withStyles } from '@material-ui/core/styles';
import { capitalize, useForkRef, useIsFocusVisible } from '@material-ui/core/utils';
import { capitalize, useForkRef, useId, useIsFocusVisible } from '@material-ui/core/utils';
import Star from '../internal/svg-icons/Star';

function clamp(value, min, max) {
Expand Down Expand Up @@ -160,14 +160,7 @@ const Rating = React.forwardRef(function Rating(props, ref) {
...other
} = props;

const [defaultName, setDefaultName] = React.useState();
const name = nameProp || defaultName;
React.useEffect(() => {
// Fallback to this default id when possible.
// Use the random value for client-side rendering only.
// We can't use it server-side.
setDefaultName(`mui-rating-${Math.round(Math.random() * 1e5)}`);
}, []);
const name = useId(nameProp);

const { current: isControlled } = React.useRef(valueProp !== undefined);
const [valueState, setValueState] = React.useState(defaultValue);
Expand Down
11 changes: 2 additions & 9 deletions packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-constant-condition */
import * as React from 'react';
import PropTypes from 'prop-types';
import { setRef, useEventCallback, useControlled } from '@material-ui/core/utils';
import { setRef, useEventCallback, useControlled, useId } from '@material-ui/core/utils';

// https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript
// Give up on IE 11 support for this feature
Expand Down Expand Up @@ -103,14 +103,7 @@ export default function useAutocomplete(props) {
value: valueProp,
} = props;

const [defaultId, setDefaultId] = React.useState();
const id = idProp || defaultId;
React.useEffect(() => {
// Fallback to this default id when possible.
// Use the random value for client-side rendering only.
// We can't use it server-side.
setDefaultId(`mui-autocomplete-${Math.round(Math.random() * 1e5)}`);
}, []);
const id = useId(idProp);

const ignoreFocus = React.useRef(false);
const firstFocus = React.useRef(true);
Expand Down
10 changes: 2 additions & 8 deletions packages/material-ui/src/RadioGroup/RadioGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import FormGroup from '../FormGroup';
import useForkRef from '../utils/useForkRef';
import useControlled from '../utils/useControlled';
import RadioGroupContext from './RadioGroupContext';
import useId from '../utils/useId';

const RadioGroup = React.forwardRef(function RadioGroup(props, ref) {
const {
Expand Down Expand Up @@ -52,14 +53,7 @@ const RadioGroup = React.forwardRef(function RadioGroup(props, ref) {
}
};

const [defaultName, setDefaultName] = React.useState();
const name = nameProp || defaultName;
React.useEffect(() => {
// Fallback to this default name when possible.
// Use the random value for client-side rendering only.
// We can't use it server-side.
setDefaultName(`mui-radiogroup-${Math.round(Math.random() * 1e5)}`);
}, []);
const name = useId(nameProp);

return (
<RadioGroupContext.Provider value={{ name, onChange: handleChange, value }}>
Expand Down
6 changes: 3 additions & 3 deletions packages/material-ui/src/RadioGroup/RadioGroup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ describe('<RadioGroup />', () => {
</RadioGroup>,
);

assert.match(findRadio(wrapper, 'zero').props().name, /^mui-radiogroup-[0-9]+/);
assert.match(findRadio(wrapper, 'one').props().name, /^mui-radiogroup-[0-9]+/);
assert.match(findRadio(wrapper, 'zero').props().name, /^mui-[0-9]+/);
assert.match(findRadio(wrapper, 'one').props().name, /^mui-[0-9]+/);
});

describe('imperative focus()', () => {
Expand Down Expand Up @@ -306,7 +306,7 @@ describe('<RadioGroup />', () => {
const radioGroupRef = React.createRef();
const { setProps } = render(<RadioGroupControlled ref={radioGroupRef} />);

expect(radioGroupRef.current.name).to.match(/^mui-radiogroup-[0-9]+/);
expect(radioGroupRef.current.name).to.match(/^mui-[0-9]+/);

setProps({ name: 'anotherGroup' });
expect(radioGroupRef.current).to.have.property('name', 'anotherGroup');
Expand Down
14 changes: 2 additions & 12 deletions packages/material-ui/src/Tooltip/Tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import capitalize from '../utils/capitalize';
import Grow from '../Grow';
import Popper from '../Popper';
import useForkRef from '../utils/useForkRef';
import useId from '../utils/useId';
import setRef from '../utils/setRef';
import useIsFocusVisible from '../utils/useIsFocusVisible';
import useControlled from '../utils/useControlled';
Expand Down Expand Up @@ -245,18 +246,7 @@ const Tooltip = React.forwardRef(function Tooltip(props, ref) {
}, [title, childNode, isControlled]);
}

const [defaultId, setDefaultId] = React.useState();
const id = idProp || defaultId;
React.useEffect(() => {
if (!open || defaultId) {
return;
}

// Fallback to this default id when possible.
// Use the random value for client-side rendering only.
// We can't use it server-side.
setDefaultId(`mui-tooltip-${Math.round(Math.random() * 1e5)}`);
NMinhNguyen marked this conversation as resolved.
Show resolved Hide resolved
}, [open, defaultId]);
const id = useId(idProp);

React.useEffect(() => {
return () => {
Expand Down
1 change: 1 addition & 0 deletions packages/material-ui/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export { default as unsupportedProp } from './unsupportedProp';
export { default as useControlled } from './useControlled';
export { default as useEventCallback } from './useEventCallback';
export { default as useForkRef } from './useForkRef';
export { default as useId } from './useId';
export { default as useIsFocusVisible } from './useIsFocusVisible';
18 changes: 18 additions & 0 deletions packages/material-ui/src/utils/useId.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as React from 'react';

/**
* Private module reserved for @material-ui/x packages.
NMinhNguyen marked this conversation as resolved.
Show resolved Hide resolved
*/
export default function useId(idOverride) {
const [defaultId, setDefaultId] = React.useState(idOverride);
const id = idOverride || defaultId;
React.useEffect(() => {
if (defaultId == null) {
// Fallback to this default id when possible.
// Use the random value for client-side rendering only.
// We can't use it server-side.
setDefaultId(`mui-${Math.round(Math.random() * 1e5)}`);
}
}, [defaultId]);
return id;
}
36 changes: 36 additions & 0 deletions packages/material-ui/src/utils/useId.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import { expect } from 'chai';
import { createClientRender } from 'test/utils/createClientRender';
import useId from './useId';

const TestComponent = ({ id: idProp }) => {
const id = useId(idProp);
return <span>{id}</span>;
};

TestComponent.propTypes = {
id: PropTypes.string,
};

describe('useId', () => {
const render = createClientRender();

it('returns the provided ID', () => {
const { getByText, setProps } = render(<TestComponent id="some-id" />);

expect(getByText('some-id')).to.not.be.null;

setProps({ id: 'another-id' });
expect(getByText('another-id')).to.not.be.null;
});

it("generates an ID if one isn't provided", () => {
const { getByText, setProps } = render(<TestComponent />);

expect(getByText(/^mui-[0-9]+$/)).to.not.be.null;

setProps({ id: 'another-id' });
expect(getByText('another-id')).to.not.be.null;
});
});