Skip to content

Commit

Permalink
fix(plasma-new-hope): fix attach onChange
Browse files Browse the repository at this point in the history
  • Loading branch information
denivladislav committed Oct 14, 2024
1 parent fde0ef6 commit 1bb510e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
27 changes: 19 additions & 8 deletions packages/plasma-new-hope/src/components/Attach/Attach.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { forwardRef, useEffect, useRef, useState } from 'react';
import type { ChangeEvent } from 'react';
import { useForkRef, useIsomorphicLayoutEffect } from '@salutejs/plasma-core';

import { RootProps } from '../../engines';
Expand Down Expand Up @@ -28,6 +29,10 @@ export const attachRoot = (Root: RootProps<HTMLDivElement, AttachProps>) =>
style,
isLoading,
disabled,
id,
name,
onChange,
onClear,
...rest
} = props;

Expand Down Expand Up @@ -115,19 +120,27 @@ export const attachRoot = (Root: RootProps<HTMLDivElement, AttachProps>) =>
inputRef.current.click();
};

const handleChange = () => {
if (!inputRef.current || !inputRef.current.files) {
const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
if (!e.target.files) {
return;
}

setFilename(inputRef.current.files[0].name);
if (onChange) {
onChange(e);
}

setFilename(e.target.files[0].name);
};

const handleClear = () => {
if (!inputRef.current) {
return;
}

if (onClear) {
onClear();
}

inputRef.current.value = '';
setFilename('');
setTruncatedFilename('');
Expand All @@ -145,13 +158,11 @@ export const attachRoot = (Root: RootProps<HTMLDivElement, AttachProps>) =>
ref={inputRef}
accept={accept}
type="file"
id="attachHiddenInput"
name="attachHiddenInput"
id={id}
name={name}
onChange={handleChange}
/>
<StyledHiddenInputHelper ref={inputHelperRef} id="attachHiddenInputHelper">
{filename}
</StyledHiddenInputHelper>
<StyledHiddenInputHelper ref={inputHelperRef}>{filename}</StyledHiddenInputHelper>

<AttachButton
buttonType={buttonType}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export type BaseAttachProps = {
* Вид Attach
*/
view?: string;
/**
* Callback при удалении прикрепленного файла
*/
onClear?: () => void;
};

export type AttachButtonProps = (
Expand Down

0 comments on commit 1bb510e

Please sign in to comment.