Cyber Week Sale -30% Our only 30% sale of the year, extended for one more week!
See sale prices

Image Input Component

In the input image component, the input value is always emptied on change. When the form is submitted, the file will not be present

<pre> protected _change(): void { const payload = {cancel: false}; this._fireEvent('change', payload); this._dispatchEvent('change', payload); if (payload.cancel === true) { return; } const reader = new FileReader(); reader.onload = () => { this._previewElement.style.backgroundImage = `url(${reader.result})`; } reader.readAsDataURL(this._inputElement.files[0]); this._inputElement.value = ""; this._hiddenElement.value = ""; this._lastMode = 'new'; this._element.classList.add('changed'); this._removeElement.classList.remove('hidden'); this._element.classList.remove('empty'); this._fireEvent('changed'); this._dispatchEvent('changed'); } </pre>
Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (3)


Yes, it's probably a bug. We will fix it. Here's the workaround you can try for now.

Thanks


Hi

When a user selects a file, the component is clearing the input's value immediately after reading it for preview, which means the file won't be available when the form is submitted.

Please modify this file: /src/core/components/image-input/image-input.ts In the _change() method, these lines are causing the issue:

<pre> // ... existing code ... protected _change(): void { const payload = {cancel: false}; this._fireEvent('change', payload); this._dispatchEvent('change', payload); if (payload.cancel === true) { return; } // Store the file for submission const file = this._inputElement.files[0]; const reader = new FileReader(); reader.onload = () => { this._previewElement.style.backgroundImage = `url(${reader.result})`; } reader.readAsDataURL(file); // Don't clear the input value - this is the key change // this._inputElement.value = ""; // You can use the hidden input to store additional info if needed // this._hiddenElement.value = ""; this._lastMode = 'new'; this._element.classList.add('changed'); this._removeElement.classList.remove('hidden'); this._element.classList.remove('empty'); this._fireEvent('changed'); this._dispatchEvent('changed'); } // ... existing code ... </pre>

Hi,

How come this is its behavior, isn't it wrong?


Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(
buy metronic keenthemes mega bundle

ReUI

Open-source React components, blocks and templates built on shadcn/ui and Tailwind CSS by Keenthemes.