viclafouch / mui-file-input

A file input designed for the React library MUI
https://viclafouch.github.io/mui-file-input/
MIT License
77 stars 17 forks source link

`inputProps`, `InputProps`, `startAdornment: <AttachFileIcon />`, `accept: ".pdf"` are mixed up in 4.0.4 #52

Closed LikeDreamwalker closed 1 month ago

LikeDreamwalker commented 2 months ago

Describe the bug I'm not sure if this is a bug or if it is designed in this way, but it's really weird. When I am trying to set the component to only accept a specific file type and also show the AttachFileIcon (not sure if it is related to a specific icon and not tested), I get stuck cause of the wrong type and prop of the component.

To Reproduce

  1. Using 4.0.4 version
  2. Use the component, which should be able to only accept a specific type of file and show the icon From the document, we can get a component like this to use the accept prop:
    <MuiFileInput inputProps={{ accept: 'video/*' }} />

    And for rendering the icon, I haven't found it in the document but the source code in this repo for the main page of the document:

        <MuiFileInput
          value={files}
          multiple
          onChange={handleChange}
          placeholder="Insert a file"
          clearIconButtonProps={{
            children: <CloseIcon fontSize="small" />
          }}
          InputProps={{
            startAdornment: <AttachFileIcon />
          }}
        />

    And I don't know if you noticed, but actually, there are two writing styles for the input-props, and they both indeed can work on version 4.0.4. When I am trying to use:

            <MuiFileInput
              value={value}
              onChange={handleChange}
              InputProps={{
                startAdornment: <AttachFileIcon />,
                accept: ".pdf",
              }}
              placeholder="Insert a file"
              clearIconButtonProps={{
                children: <CloseIcon fontSize="small" />,
              }}
            />

    I will get: image back, which means that accept doesn't exist and accept is really not working.

And if I use:

            <MuiFileInput
              value={value}
              onChange={handleChange}
              inputProps={{
                startAdornment: <AttachFileIcon />,
                accept: ".pdf",
              }}
              // inputProps={{ accept: ".pdf" }}
              placeholder="Insert a file"
              clearIconButtonProps={{
                children: <CloseIcon fontSize="small" />,
              }}
            />

I will get an error in console saying: image And by changing startAdornment to startadornment, it won't show the icon but can avoid browser error.

So, the finally working version for me is like:

            <MuiFileInput
              value={value}
              onChange={handleChange}
              InputProps={{
                startAdornment: <AttachFileIcon />,
              }}
              inputProps={{ accept: ".pdf" }}
              placeholder="Insert a file"
              clearIconButtonProps={{
                children: <CloseIcon fontSize="small" />,
              }}
            />

Expected behavior InputProps and inputProps should be merged into one prop.

Desktop (please complete the following information):

Additional context

{
  "name": "colossal-reader-app",
  "version": "0.1.0",
  "private": true,
  "packageManager": "pnpm@9.1.3",
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@ant-design/nextjs-registry": "^1.0.0",
    "@emotion/cache": "^11.11.0",
    "@emotion/react": "^11.11.4",
    "@emotion/styled": "^11.11.5",
    "@mui/icons-material": "^5.15.19",
    "@mui/material": "^5.15.19",
    "@mui/material-nextjs": "^5.15.11",
    "@mui/system": "^5.15.15",
    "@types/ua-parser-js": "^0.7.39",
    "mui-file-input": "^4.0.4",
    "next": "14.2.3",
    "react": "^18",
    "react-dom": "^18",
    "sass": "^1.77.4",
    "ua-parser-js": "^1.0.38"
  },
  "devDependencies": {
    "@types/node": "^20",
    "@types/react": "^18",
    "@types/react-dom": "^18",
    "eslint": "^8",
    "eslint-config-next": "14.2.3",
    "typescript": "^5"
  }
}
viclafouch commented 1 month ago

Hello !

It's not a bug, it comes from the MUI TextField Documentation.

Your final version is correct. Nothing less, nothing more ;)