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

[docs] Allow function prop to return undefined #32766

Merged
merged 1 commit into from
May 25, 2022

Conversation

m4theushw
Copy link
Member

@m4theushw m4theushw commented May 13, 2022

If a prop receives a function whose return has undefined, then yarn docs:api crashes with:

TypeError: resolveType for 'UndefinedLiteral' not implemented

This can be tested applying the diff below:

diff --git a/packages/mui-material/src/Rating/Rating.d.ts b/packages/mui-material/src/Rating/Rating.d.ts
index 1eb262d4c4..704c4443f9 100644
--- a/packages/mui-material/src/Rating/Rating.d.ts
+++ b/packages/mui-material/src/Rating/Rating.d.ts
@@ -42,12 +42,12 @@ export interface RatingProps
    *
    * For localization purposes, you can use the provided [translations](/material-ui/guides/localization/).
    * @param {number} value The rating label's value to format.
-   * @returns {string}
+   * @returns {string | undefined}
    * @default function defaultLabelText(value) {
    *   return `${value} Star${value !== 1 ? 's' : ''}`;
    * }
    */
-  getLabelText?: (value: number) => string;
+  getLabelText?: (value: number) => string | undefined;
   /**
    * If `true`, only the selected icon will be highlighted.
    * @default false

The reason for this change is because in mui/mui-x#4859 I added a prop to the DataGrid which can return null and other values. I can't add undefined because the docs generator crashes.

Sorry, something went wrong.

@mui-bot
Copy link

mui-bot commented May 13, 2022

No bundle size changes

Generated by 🚫 dangerJS against 93dbf9d

@m4theushw m4theushw added the docs Improvements or additions to the documentation label May 14, 2022
@m4theushw m4theushw requested review from mnajdova and siriwatknp May 16, 2022 12:34
@siriwatknp
Copy link
Member

@m4theushw is it better to use React.ReactNode?

@m4theushw
Copy link
Member Author

@siriwatknp I didn't understand you. Do you mean to use React.ReactNode in the diff? The diff is only an example of how to test this PR. In mui/mui-x#4859 I don't want to support React elements.

@siriwatknp
Copy link
Member

@m4theushw I mean we can just update the getLabelText type to return React.ReactNode instead of string and run docs:api. It looks better from the Material UI perspective. Does it work for mui-x?

diff --git a/packages/mui-material/src/Rating/Rating.d.ts b/packages/mui-material/src/Rating/Rating.d.ts
index 1eb262d4c4..667ee0aaeb 100644
--- a/packages/mui-material/src/Rating/Rating.d.ts
+++ b/packages/mui-material/src/Rating/Rating.d.ts
@@ -47,7 +47,7 @@ export interface RatingProps
    *   return `${value} Star${value !== 1 ? 's' : ''}`;
    * }
    */
-  getLabelText?: (value: number) => string;
+  getLabelText?: (value: number) => React.ReactNode;
   /**
    * If `true`, only the selected icon will be highlighted.
    * @default false

@m4theushw
Copy link
Member Author

@siriwatknp Replacing string with React.ReactNode could work, but I don't want to change the Rating component. I only used getLabelText because it was the first function prop I found to demonstrate the bug. In MUI X I don't want to use React.ReactNode.

Copy link
Member

@siriwatknp siriwatknp left a comment

Choose a reason for hiding this comment

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

👍 Looks good.

@m4theushw m4theushw merged commit 890a302 into mui:master May 25, 2022
@m4theushw m4theushw deleted the support-undefined-return branch May 25, 2022 21:44
@oliviertassinari
Copy link
Member

oliviertassinari commented Jun 11, 2022

For context, getLabelText is applied as a DOM attribute, it has to be a string.

aria-label={readOnly ? getLabelText(value) : null}

I also suspect that it should never be an empty string/null/undefined.


The reason for this change is because in mui/mui-x#4859 I added a prop to the DataGrid which can return null and other values. I can't add undefined because the docs generator crashes.

@m4theushw It could have been great to have an exact example on this PR. It's not clear to me why we need to support undefined.

@m4theushw
Copy link
Member Author

@oliviertassinari This PR was created to allow getRowHeight prop to receive () => undefined or () => null. Since we don't change the behavior based on the return, for better DX we support all. If only () => null is supported, developers will have to figure out why the other value can't be used.

The prop is in https://github.com/mui/mui-x/blob/9e317d512d0f1f637961003b9f5de9a40eb54b3b/packages/grid/x-data-grid/src/models/params/gridRowParams.ts#L66

@oliviertassinari
Copy link
Member

oliviertassinari commented Jun 13, 2022

@m4theushw Thanks for the context:

Screenshot 2022-06-13 at 22 00 40

I see this approach used here as well:

Screenshot 2022-06-13 at 21 52 28

We might be able to argue that using only one of the two for the types and the docs could improve the DX too. Why? Because it would mean that developers don't have to choose between using null or undefined, we made the decision for them. We could make the code support any nullish value to avoid breaking changes (but without encouraging it). So in the end, maybe:

export type GridRowHeightReturnValue = number | undefined | 'auto';

is a step forward. But then, it's a bit confusing for me. What does returning undefined mean? I had to check the docs to understand. So maybe this would be even clearer 🤷‍♂️:

export type GridRowHeightReturnValue = number | 'props.rowHeight' | 'auto';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Improvements or additions to the documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants