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

[joy-ui][Autocomplete] Add type=button to clear button #39263

Merged
merged 1 commit into from
Oct 6, 2023

Conversation

brijeshb42
Copy link
Contributor

@brijeshb42 brijeshb42 commented Oct 2, 2023

Closes #39262

@brijeshb42 brijeshb42 added component: autocomplete This is the name of the generic UI component, not the React module! package: joy-ui Specific to @mui/joy labels Oct 2, 2023
@brijeshb42 brijeshb42 requested a review from siriwatknp October 2, 2023 11:27
@mui-bot
Copy link

mui-bot commented Oct 2, 2023

Netlify deploy preview

https://deploy-preview-39263--material-ui.netlify.app/

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against fff029f

@danilo-leal danilo-leal changed the title [Joy][Autocomplete] Add type=button to clear button [joy-ui][Autocomplete] Add type=button to clear button Oct 2, 2023
Copy link
Member

@oliviertassinari oliviertassinari left a comment

Choose a reason for hiding this comment

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

I don't really see it fixing the root problem:

import * as React from "react";
import Box from "@mui/joy/Box";
import OpenInNew from "@mui/icons-material/OpenInNew";
import IconButtonJoy from "@mui/joy/IconButton";
import IconButtonMd from "@mui/material/IconButton";
import { CssVarsProvider } from "@mui/joy/styles";

export default function ButtonLink() {
  return (
    <Box sx={{ display: "flex", gap: 2, alignItems: "center" }}>
      <CssVarsProvider>
        <IconButtonJoy type={undefined}>
          <OpenInNew />
        </IconButtonJoy>
      </CssVarsProvider>
      <IconButtonMd type={undefined}>
        <OpenInNew />
      </IconButtonMd>
    </Box>
  );
}

Solved with?

diff --git a/packages/mui-joy/src/IconButton/IconButton.tsx b/packages/mui-joy/src/IconButton/IconButton.tsx
index 8a169ab2d4..e876549638 100644
--- a/packages/mui-joy/src/IconButton/IconButton.tsx
+++ b/packages/mui-joy/src/IconButton/IconButton.tsx
@@ -138,6 +138,7 @@ const IconButton = React.forwardRef(function IconButton(inProps, ref) {
     size: sizeProp = 'md',
     slots = {},
     slotProps = {},
+    type,
     ...other
   } = props;
   const buttonGroup = React.useContext(ButtonGroupContext);
  • useAutocomplete doesn't set the type="button". Maybe but not sure, it could mess stuff up if developers are using role="button".
diff --git a/packages/mui-base/src/useAutocomplete/useAutocomplete.js b/packages/mui-base/src/useAutocomplete/useAutocomplete.js
index f81827e684..524ebdf1af 100644
--- a/packages/mui-base/src/useAutocomplete/useAutocomplete.js
+++ b/packages/mui-base/src/useAutocomplete/useAutocomplete.js
@@ -1131,10 +1131,12 @@ export function useAutocomplete(props) {
       disabled: disabledProp,
     }),
     getClearProps: () => ({
+      type: 'button',
       tabIndex: -1,
       onClick: handleClear,
     }),
     getPopupIndicatorProps: () => ({
+      type: 'button',
       tabIndex: -1,
       onClick: handlePopupIndicator,
     }),

@oliviertassinari oliviertassinari added the bug 🐛 Something doesn't work label Oct 2, 2023
@brijeshb42
Copy link
Contributor Author

Looking at the implementation, the actual IconButton uses useButton which in turn sets the type as button if not specified by the user. But in case of Autocomplete, it imports StyledIconButton that only applies the style and not IconButton that uses the useButton.

As per your comment, the 2nd one makes most sense as it'll apply the fix to all present and future components using useAutocomplete. And, although they can, but I doubt people will want to mess with the default a11y features by providing a non button element with role button.

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.

👍 Thanks for the fix!

@siriwatknp siriwatknp requested a review from mj12albert October 4, 2023 09:50
@brijeshb42 brijeshb42 merged commit 4db093d into mui:master Oct 6, 2023
@brijeshb42 brijeshb42 deleted the fix-39262 branch October 6, 2023 16:17
mnajdova pushed a commit to mnajdova/material-ui that referenced this pull request Oct 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: autocomplete This is the name of the generic UI component, not the React module! package: base-ui Specific to @mui/base package: joy-ui Specific to @mui/joy
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Autocomplete clear button acting as type="submit" by default
4 participants