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

The ability to use ReactNode as label prop of FormTab #5436

Closed
dejancg opened this issue Oct 22, 2020 · 2 comments
Closed

The ability to use ReactNode as label prop of FormTab #5436

dejancg opened this issue Oct 22, 2020 · 2 comments

Comments

@dejancg
Copy link

dejancg commented Oct 22, 2020

Currently, the FormTab component declares label prop as string type (here):

export interface FormTabProps {
    // ...
    label: string;
    // ..
}

However, the FormTab component merely passes the prop value to MuiTab component (translated though), as seen here:

const renderHeader = () => (
    <MuiTab
        // ...
        label={translate(label, { _: label })}
        // ...
    />
);

And the MuiTab component declares the label as ReactNode, as seen here:

export type TabTypeMap<P = {}, D extends React.ElementType = 'div'> = ExtendButtonBaseTypeMap<{
  props: P & {
    // ...
    /**
     * The label element.
     */
    label?: React.ReactNode;
    // ...
  };
  // ...
}>;

To work around this limitation, I did this:

  1. Created a type guard to assert the label type:
const isString = (element: any): element is string => {
  return typeof element === "string" || element instanceof String;
};
  1. Changed the renderHeader func like so:
  const renderHeader = () => (
    <MuiTab
      key={isString(label) ? label : label.props.key} // <-- Might be a better idea to use the FormTab key here
      label={isString(label) ? translate(label, { _: label }) : label}
     // ...
    />
  );
  1. Change the FormTabProps label declaration like so:
export interface FormTabProps {
  // ...
  label: string | ReactElement;
  // ...
}

The obvious downside is that I lost the ability to have my translation key translated automatically when an element is used instead of plain string. But other than that, seems to be working fine. Any thoughts?

@djhi
Copy link
Collaborator

djhi commented Oct 23, 2020

Thanks for reporting. Marking this as an enhancement

@djhi
Copy link
Collaborator

djhi commented Mar 19, 2021

Fixed by #6061

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants