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

[Table] Extended labelRowsPerPage support from string to ReactNode #20488

Closed
2 tasks done
romanyanke opened this issue Apr 10, 2020 · 8 comments · Fixed by #21226
Closed
2 tasks done

[Table] Extended labelRowsPerPage support from string to ReactNode #20488

romanyanke opened this issue Apr 10, 2020 · 8 comments · Fixed by #21226
Labels
accessibility a11y component: table This is the name of the generic UI component, not the React module! good first issue Great for first contributions. Enable to learn the contribution process.

Comments

@romanyanke
Copy link

  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

Here you allow to use labelRowsPerPage as React.Node https://github.com/mui-org/material-ui/blob/27471b4564eb40ff769352d73a29938d25804e45/packages/material-ui/src/TablePagination/TablePagination.d.ts#L23

Here you use it as a string https://github.com/mui-org/material-ui/blob/c253b2813822239672b7a2a589324e5e00b97820/packages/material-ui/src/TablePagination/TablePagination.js#L125

So this is vaild but will generate en error "Warning: Failed prop type: Invalid prop aria-label of type object supplied to ForwardRef(SelectInput), expected string."

<TablePagination
  labelRowsPerPage={<b>I will warn</b>}

Expected Behavior 🤔

I still want to pass a React.Node without a warning

Your Environment 🌎

Tech Version
Material-UI v4.9.9
React 16.13.1
Browser *
TypeScript 3.8.3
etc.
@oliviertassinari oliviertassinari added the component: table This is the name of the generic UI component, not the React module! label Apr 10, 2020
@oliviertassinari
Copy link
Member

@romanyanke Thanks for mentioning this inconsistency. Would the following solve the problem?

diff --git a/packages/material-ui/src/TablePagination/TablePagination.js b/packages/material-ui/src/TablePagination/TablePagination.js
index 8ab941190..9c7a0302b 100644
--- a/packages/material-ui/src/TablePagination/TablePagination.js
+++ b/packages/material-ui/src/TablePagination/TablePagination.js
@@ -10,6 +10,7 @@ import TableCell from '../TableCell';
 import Toolbar from '../Toolbar';
 import Typography from '../Typography';
 import TablePaginationActions from './TablePaginationActions';
+import useId from '../utils/unstable_useId';

 export const styles = (theme) => ({
   /* Styles applied to the root element. */
@@ -102,6 +103,7 @@ const TablePagination = React.forwardRef(function TablePagination(props, ref) {
     colSpan = colSpanProp || 1000; // col-span over everything
   }

+  const labelId = useId();
   const MenuItemComponent = SelectProps.native ? 'option' : MenuItem;

   return (
@@ -109,7 +111,7 @@ const TablePagination = React.forwardRef(function TablePagination(props, ref) {
       <Toolbar className={classes.toolbar}>
         <div className={classes.spacer} />
         {rowsPerPageOptions.length > 1 && (
-          <Typography color="inherit" variant="body2" className={classes.caption}>
+          <Typography color="inherit" variant="body2" className={classes.caption} id={labelId}>
             {labelRowsPerPage}
           </Typography>
         )}
@@ -122,7 +124,7 @@ const TablePagination = React.forwardRef(function TablePagination(props, ref) {
             input={<InputBase className={clsx(classes.input, classes.selectRoot)} />}
             value={rowsPerPage}
             onChange={onChangeRowsPerPage}
-            inputProps={{ 'aria-label': labelRowsPerPage }}
+            labelId={labelId}
             {...SelectProps}
           >
             {rowsPerPageOptions.map((rowsPerPageOption) => (

Do you want to submit a pull request? :)

@oliviertassinari oliviertassinari added good first issue Great for first contributions. Enable to learn the contribution process. accessibility a11y labels Apr 10, 2020
@eps1lon
Copy link
Member

eps1lon commented Apr 22, 2020

I'm generally in favor of referencing the label instead of duplicating it.

However, this particular use case can be solved by separating markup and styles:

const useStyles = makeStyles({
  caption: {
    fontWeight: "bold"
  }
});

export default function Demo() {
  const classes = useStyles();
  return <TablePagination classes={classes} labelRowsPerPage="I will warn" />;
}

@oliviertassinari
Copy link
Member

oliviertassinari commented Apr 22, 2020

@eps1lon A couple of internationalization libraries are implemented with a component, for instance, react-intl's <FormattedMessage /> or react-18next's <Trans />. Allowing a node would help for these cases. @romanyanke What's your use case for using a React.ReactNode over a string?

@eps1lon
Copy link
Member

eps1lon commented Apr 22, 2020

Sure. That's why I'm generally in favor. react-intl has useIntl().formatMessage for usage in attributes.

@romanyanke
Copy link
Author

@oliviertassinari react-intl's is exactly my case.
I'm using material-ui with typescript a lot and there are props that requires only string. The other props requires ReactNode. In my opinion ReactNode is much greater in terms of good API.

@oliviertassinari oliviertassinari changed the title TablePagination labelRowsPerPage prop issue [Table] Extended labelRowsPerPage support from string to ReactNode May 22, 2020
@AlexAndriyanenko
Copy link
Contributor

Is this still open, can i work on this issue?

@AlexAndriyanenko
Copy link
Contributor

I can see that in #20685 pull request ReactNode was changed to string type. Kind of disappointed what exactly should be done. It should support both string and ReactNode types?

@oliviertassinari
Copy link
Member

@AlexAndriyanenko We would love to receive your contribution to this problem :). Regarding #20685, the definition was updated to match the implementation. We would need to apply the opposite diff to solve this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accessibility a11y component: table This is the name of the generic UI component, not the React module! good first issue Great for first contributions. Enable to learn the contribution process.
Projects
None yet
4 participants