You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "Copilot (via GitHub)" <gi...@apache.org> on 2025/04/27 08:14:59 UTC

Re: [PR] feat: protos page, FileUploadTextarea component [apisix-dashboard]

Copilot commented on code in PR #3001:
URL: https://github.com/apache/apisix-dashboard/pull/3001#discussion_r2062539469


##########
src/components/form/FileUploadTextarea.tsx:
##########
@@ -0,0 +1,114 @@
+import {
+  Textarea as MTextarea,
+  type TextareaProps as MTextareaProps,
+  Box,
+  Group,
+  Button,
+  Input,
+} from '@mantine/core';
+import {
+  useController,
+  type FieldValues,
+  type UseControllerProps,
+} from 'react-hook-form';
+import { genControllerProps } from './util';
+import IconUpload from '~icons/material-symbols/upload';
+import { useRef, useState } from 'react';
+import { useTranslation } from 'react-i18next';
+
+export type FileUploadTextareaProps<T extends FieldValues> = UseControllerProps<T> &
+  MTextareaProps & {
+    acceptFileTypes?: string;
+    uploadButtonText?: string;
+    maxFileSize?: number;
+    onFileLoaded?: (content: string, fileName: string) => void;
+    allowUpload?: boolean;
+  };
+
+export const FileUploadTextarea = <T extends FieldValues>(
+  props: FileUploadTextareaProps<T>
+) => {
+  const { allowUpload = true } = props;
+  const { controllerProps, restProps } = genControllerProps(props, '');
+  const {
+    field: { value, onChange: fOnChange, ...restField },
+    fieldState,
+  } = useController<T>(controllerProps);
+  const { t } = useTranslation();
+
+  const [fileError, setFileError] = useState<string | null>(null);
+  const fileInputRef = useRef<HTMLInputElement>(null);
+
+  const {
+    acceptFileTypes = '.txt,.json,.yaml,.yml',
+    uploadButtonText = '',
+    maxFileSize = 10 * 1024 * 1024,
+    onFileLoaded,
+    onChange,
+    ...textareaProps
+  } = restProps;
+
+  const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
+    const file = event.target.files?.[0];
+    if (!file) return;
+
+    if (file.size > maxFileSize) {
+      const size = Math.round(maxFileSize / 1024 / 1024);
+      setFileError(`${t('form.upload.fileOverSize')} ${size}MB`);
+      return;
+    }
+
+    setFileError(null);
+    if (fileInputRef.current) fileInputRef.current.value = '';
+
+    const reader = new FileReader();
+    reader.onload = (e) => {
+      const content = e.target?.result as string;
+      fOnChange(content);
+      onFileLoaded?.(content, file.name);
+    };
+
+    reader.onerror = (e) => {
+      setFileError(`${t('form.upload.readError')} ${e.target?.error}`);
+    };
+
+    reader.readAsText(file);
+  };
+
+  return (
+    <Box>
+      <MTextarea
+        value={value}
+        onChange={(e) => {
+          fOnChange(e);
+          onChange?.(e);
+        }}
+        resize="vertical"
+        autosize={restField.disabled}

Review Comment:
   [nitpick] Tying the 'autosize' prop to 'restField.disabled' may produce counterintuitive behavior. Consider decoupling these properties so that autosizing is controlled explicitly rather than being dependent on the field's disabled state.
   ```suggestion
           autosize={textareaProps.autosize ?? false}
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org