From 4ffd39494eda2aa4d3695b1d34e72fb7bcf3bec1 Mon Sep 17 00:00:00 2001 From: Arifia Kasastra R Date: Sun, 17 Aug 2025 00:25:52 +0700 Subject: [PATCH 1/7] feat: Add modern file input field with drag-and-drop and clipboard support --- src/FormField.php | 242 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 242 insertions(+) diff --git a/src/FormField.php b/src/FormField.php index 8b22235..f5734b9 100644 --- a/src/FormField.php +++ b/src/FormField.php @@ -397,6 +397,248 @@ public function file($name, $options = []) return $this->text($name, $options); } + /** + * Modern file input field with drag-and-drop and clipboard paste functionality. + * + * @param string $name The file field name and id attribute. + * @param array $options Additional attribute for the file input. + * @return string Generated modern file input form field. + */ + public function fileModern($name, $options = []) + { + $requiredClass = (isset($options['required']) && $options['required'] == true) ? 'required ' : ''; + $hasError = $this->errorBag->has($this->formatArrayName($name)); + $hasErrorClass = $hasError ? 'has-error' : ''; + $htmlForm = '
'; + + $htmlForm .= $this->setFormFieldLabel($name, $options); + + $fieldId = isset($options['id']) ? $options['id'] : $name; + $accept = isset($options['accept']) ? $options['accept'] : '*/*'; + $multiple = isset($options['multiple']) && $options['multiple'] ? 'multiple' : ''; + $maxSize = isset($options['max_size']) ? $options['max_size'] : '10MB'; + $allowedTypes = isset($options['allowed_types']) ? implode(', ', $options['allowed_types']) : 'All files'; + + // Sanitize field ID for use in HTML IDs (remove square brackets and other invalid characters) + $sanitizedFieldId = preg_replace('/[^a-zA-Z0-9_-]/', '', $fieldId); + + $dropzoneId = 'dropzone-' . $sanitizedFieldId; + $fileInputId = 'file-input-' . $sanitizedFieldId; + $previewId = 'preview-' . $sanitizedFieldId; + + // Container with dropzone styling + $htmlForm .= '
'; + + // Hidden file input + $htmlForm .= ''; + $htmlForm .= '
'; + + // Preview area + $htmlForm .= ''; + + $htmlForm .= '
'; + + // Add the JavaScript for drag-and-drop and clipboard functionality + $htmlForm .= $this->getFileModernScript($dropzoneId, $fileInputId, $previewId, $accept, $multiple); + + $htmlForm .= $this->getInfoTextLine($options); + + $htmlForm .= $this->errorBag->first($this->formatArrayName($name), ':message'); + + $htmlForm .= ''; + + return $htmlForm; + } + + /** + * Generate JavaScript for modern file input functionality. + * + * @param string $dropzoneId The dropzone element ID. + * @param string $fileInputId The file input element ID. + * @param string $previewId The preview element ID. + * @param string $accept File accept types. + * @param string $multiple Multiple files flag. + * @return string JavaScript code. + */ + private function getFileModernScript($dropzoneId, $fileInputId, $previewId, $accept, $multiple) + { + $script = ''; + + return $script; + } + /** * One form which only have "one button" and "hidden fields". * This is suitable for, e.g. set status, delete button, From 0eccec94d77bf17536116e09ba6d50d261f0b427 Mon Sep 17 00:00:00 2001 From: Arifia Kasastra R Date: Sun, 17 Aug 2025 00:26:38 +0700 Subject: [PATCH 2/7] test: Add unit tests for modern file upload field functionality --- tests/Fields/FileModernTest.php | 134 ++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 tests/Fields/FileModernTest.php diff --git a/tests/Fields/FileModernTest.php b/tests/Fields/FileModernTest.php new file mode 100644 index 0000000..c7bcc65 --- /dev/null +++ b/tests/Fields/FileModernTest.php @@ -0,0 +1,134 @@ +formField->fileModern('document'); + + $this->assertTrue(str_contains($result, 'file-dropzone')); + $this->assertTrue(str_contains($result, 'Drop files here or click to browse')); + $this->assertTrue(str_contains($result, 'Or paste images from clipboard')); + $this->assertTrue(str_contains($result, 'type="file"')); + $this->assertTrue(str_contains($result, 'name="document"')); + $this->assertTrue(str_contains($result, 'id="file-input-document"')); + $this->assertTrue(str_contains($result, '