Skip to content

Commit 268b901

Browse files
authored
Merge pull request #695 from devtron-labs/fix/file-upload-unique-id
fix: FileUpload - unique id
2 parents abb469a + d4a265f commit 268b901

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/Shared/Components/FileUpload/FileUpload.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { ChangeEvent } from 'react'
17+
import { ChangeEvent, useMemo } from 'react'
1818

19-
import { ReactComponent as ICUploadArrowAnimated } from '@Icons/ic-upload-arrow-animated.svg'
20-
import { ReactComponent as ICFileText } from '@Icons/ic-file-text.svg'
2119
import { ReactComponent as ICCross } from '@Icons/ic-cross.svg'
20+
import { ReactComponent as ICFileText } from '@Icons/ic-file-text.svg'
21+
import { ReactComponent as ICUploadArrowAnimated } from '@Icons/ic-upload-arrow-animated.svg'
2222
import { Tooltip } from '@Common/Tooltip'
23-
2423
import { ComponentSizeType } from '@Shared/constants'
24+
import { getUniqueId } from '@Shared/Helpers'
2525

2626
import { Button, ButtonStyleType, ButtonVariantType } from '../Button'
2727
import { FileUploadProps } from './types'
@@ -36,6 +36,8 @@ export const FileUpload = ({
3636
fileTypes = [],
3737
onUpload,
3838
}: FileUploadProps) => {
39+
const id = useMemo(() => `fileInput-${getUniqueId()}`, [])
40+
3941
// METHODS
4042
const handleFileChange = (event: ChangeEvent<HTMLInputElement>) => {
4143
if (event.target.files) {
@@ -88,11 +90,11 @@ export const FileUpload = ({
8890
) : (
8991
<div className="dc__position-rel flex">
9092
{/* eslint-disable-next-line jsx-a11y/label-has-associated-control */}
91-
<label htmlFor="fileInput" className="m-0 fw-4 cb-5 fs-13 lh-20 flex-grow-1 cursor">
93+
<label htmlFor={id} className="m-0 fw-4 cb-5 fs-13 lh-20 flex-grow-1 cursor">
9294
{label || 'Upload file…'}
9395
</label>
9496
<input
95-
id="fileInput"
97+
id={id}
9698
type="file"
9799
className="dc__visibility-hidden dc__position-abs dc__top-0 dc__right-0 dc__bottom-0 dc__left-0"
98100
onChange={handleFileChange}

0 commit comments

Comments
 (0)