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

[Textfield] TS2322 with slotProps inputLabel #44633

Open
sampain opened this issue Dec 2, 2024 · 8 comments
Open

[Textfield] TS2322 with slotProps inputLabel #44633

sampain opened this issue Dec 2, 2024 · 8 comments
Assignees
Labels
component: text field This is the name of the generic UI component, not the React module! typescript

Comments

@sampain
Copy link

sampain commented Dec 2, 2024

Steps to reproduce

Steps:

  1. Using Textfield with shrink to true
<TextField
  slotProps={{
    inputLabel: { shrink: true },
  }}
/>
  1. Build with TypeScript (with strict)

Returns

error TS2322: Type '{ shrink: true; } | { shrink: true; } | { id?: string | undefined; role?: AriaRole | undefined; form?: string | undefined; property?: string | undefined; key?: Key | null | undefined; ... 265 more ...; shrink: true; } | ... 7 more ... | { ...; }' is not assignable to type 'SlotProps<ElementType<InputLabelProps, keyof IntrinsicElements>, {}, BaseTextFieldProps> | undefined'.
  Object literal may only specify known properties, and 'shrink' does not exist in type '(Partial<Omit<DetailedHTMLProps<LabelHTMLAttributes<HTMLLabelElement>, HTMLLabelElement>, "ref"> & { ...; }> & SlotCommonProps) | ... 5 more ... | (Partial<...> & SlotCommonProps)'.'.  

Current behavior

Error on build

Expected behavior

Suppose to build like
<TextField InputLabelProps={{ shrink: true }} />

Doc still suggest to use it this way even if InputLabelProps is deprecated: https://mui.com/material-ui/react-text-field/#shrink

Context

No response

Your environment

No response

Search keywords: TS2322 inputLabel slotProps Textfield

@sampain sampain added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Dec 2, 2024
@sai6855
Copy link
Contributor

sai6855 commented Dec 3, 2024

Doc still suggest to use it this way even if InputLabelProps is deprecated

@sampain Opened PR to remove InputLabelProps and update it with slotProps.inputLabel #44634

@siriwatknp
Copy link
Member

@sampain Can you share the repository? I cannot reproduce on my machine.

@siriwatknp siriwatknp added status: waiting for author Issue with insufficient information component: text field This is the name of the generic UI component, not the React module! typescript and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Dec 3, 2024
@sampain
Copy link
Author

sampain commented Dec 3, 2024

@sampain Can you share the repository? I cannot reproduce on my machine.

I cannot share the repository, but I just found out that it's the spread operator in my custom Textfield componant that is causing the issue.
Here's a simplified version of it:

export function CustomTextField(props: TextFieldProps) {
  const { slotProps = {}, ...otherProps } = props;
  const TextFieldElement = (
    <TextField
      slotProps={{
        inputLabel: { shrink: true, ...slotProps.inputLabel },
        ...slotProps,
      }}
      {...otherProps}
    />
  );
  return TextFieldElement;
}

I also have the same issue if I try to do something like this:

export function CustomTextField(props: TextFieldProps) {
  const { slotProps = {}, ...otherProps } = props;
  const { input, ...otherSlotPropss } = slotProps;
  const { shrink } = input;
}

Casting it like this seems to work, would that be the official way of doing this?

        inputLabel: {
          shrink: true,
          ...slotProps.inputLabel,
        } as Partial<InputLabelProps>,

It seems specific to inputLabel however, with something like this, only inputLabel cause an error:

export function CustomTextField(props: TextFieldProps) {
  const { slotProps = {}, ...otherProps } = props;
  const TextFieldElement = (
    <TextField
      slotProps={{
        inputLabel: { error: true, ...slotProps.inputLabel },
        formHelperText: { error: true, ...slotProps.formHelperText },
        htmlInput: { error: true, ...slotProps.htmlInput },
        input: { error: true, ...slotProps.input },
      }}
      {...otherProps}
    />
  );
  return TextFieldElement;
}

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Dec 3, 2024
@metalRudge
Copy link

Im having the same problem but without using the spread operator, the only way I get it to work is by using the InputLabelprops which is deprecated. Is there any other way to alter the animation states of the Input labels ?

@siriwatknp siriwatknp removed the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Dec 12, 2024
@siriwatknp

This comment was marked as off-topic.

@siriwatknp
Copy link
Member

siriwatknp commented Dec 12, 2024

@sampain Can you share the repository? I cannot reproduce on my machine.

I cannot share the repository, but I just found out that it's the spread operator in my custom Textfield componant that is causing the issue. Here's a simplified version of it:

export function CustomTextField(props: TextFieldProps) {
  const { slotProps = {}, ...otherProps } = props;
  const TextFieldElement = (
    <TextField
      slotProps={{
        inputLabel: { shrink: true, ...slotProps.inputLabel },
        ...slotProps,
      }}
      {...otherProps}
    />
  );
  return TextFieldElement;
}

Hmm, I could not reproduce this. https://codesandbox.io/p/sandbox/pedantic-dijkstra-7jj5js?file=%2Fsrc%2FDemo.tsx%3A14%2C7

@siriwatknp
Copy link
Member

Im having the same problem but without using the spread operator, the only way I get it to work is by using the InputLabelprops which is deprecated. Is there any other way to alter the animation states of the Input labels ?

Can you provide a reproducible sandbox or a repo?

@sampain
Copy link
Author

sampain commented Dec 12, 2024

@sampain Can you share the repository? I cannot reproduce on my machine.

I cannot share the repository, but I just found out that it's the spread operator in my custom Textfield componant that is causing the issue. Here's a simplified version of it:

export function CustomTextField(props: TextFieldProps) {
  const { slotProps = {}, ...otherProps } = props;
  const TextFieldElement = (
    <TextField
      slotProps={{
        inputLabel: { shrink: true, ...slotProps.inputLabel },
        ...slotProps,
      }}
      {...otherProps}
    />
  );
  return TextFieldElement;
}

Hmm, I could not reproduce this. https://codesandbox.io/p/sandbox/pedantic-dijkstra-7jj5js?file=%2Fsrc%2FDemo.tsx%3A14%2C7

I could not get a proper sandbox before I ran out of credits, but maybe it's still enough for you to investigate: https://codesandbox.io/p/devbox/dark-bird-qz96tr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: text field This is the name of the generic UI component, not the React module! typescript
Projects
None yet
Development

No branches or pull requests

4 participants