Skip to content

Instantly share code, notes, and snippets.

@Qubadi
Last active October 9, 2024 18:28
Show Gist options
  • Save Qubadi/c83878e6244261b39ff387d4c9739f67 to your computer and use it in GitHub Desktop.
Save Qubadi/c83878e6244261b39ff387d4c9739f67 to your computer and use it in GitHub Desktop.
JetFormBuilder media field, enhanced choose file
UPDATED: 07.05.2024
1. We've enhanced the JetFormBuilder with a refined custom code, improving both style and functionality for file selection. The latest
update introduces support for the repeater field's media capabilities, offering a more streamlined and efficient user experience for
handling multiple files within forms.
2. Copy this code, create a new HTML snippet, and paste it into your snippet plugin. Save it as a header or footer.
__________________________________________
See the youtube tutorials here ( OLD ):
https://youtu.be/lOghzLiR73Q?si=DbNNcCkKzkv_mAQl
__________________________________________
<style>
/* Custom styles for the file upload field */
.jet-form-builder__field-wrap.jet-form-builder-file-upload {
background: #ecf6ff !important;
border: 3px dashed #c7d4e1 !important;
padding: 20px !important;
text-align: center;
position: relative;
margin-bottom: 20px;
display: flex;
flex-direction: column;
align-items: center;
border-radius: 6px !important;
}
/* Styles for the image upload */
.jet-form-builder-file-upload__file img {
display: block;
width: 100%;
height: 100% !important;
padding: 0;
margin: 0;
-o-object-fit: cover;
object-fit: cover;
-o-object-position: center center;
object-position: center center;
border-radius: 6px !important;
box-shadow: 0 0 30px -8px rgba(0, 0, 0, 0.24) !important;
}
/* Styles for image content */
.jet-form-builder-file-upload__content {
min-height: auto !important;
}
/* Styles for the custom "Choose File" and "File Uploaded" buttons */
.addfile {
padding: 10px 20px;
background-color: #0037fd !important;
color: #ffffff !important;
border: none !important;
font-size: 16px;
font-weight: 600;
cursor: pointer;
border-radius: 6px;
margin-top: 10px; /* Space between button and label */
}
.addfile:hover {
background-color: #000000 !important;
color: #ffffff !important;
}
/* Styles for the label that shows file upload status */
.labeladdfile {
padding: 6px 12px;
background-color: transparent; /* Default background is transparent */
color: #000000 !important;
font-size: 12px;
font-weight: 400;
display: block;
margin-top: 10px;
border-radius: 6px;
}
/* Class added when files are uploaded */
.files-uploaded {
background-color: #09b872 !important;
padding: 6px 12px;
color: #ffffff !important;
border-radius: 6px;
}
/* Hide the default file input visually */
.jet-form-builder-file-upload__input {
display: none;
}
/* Responsive adjustments */
@media (max-width: 767px) {
.addfile, .labeladdfile {
padding: 8px 16px;
font-size: 12px;
}
}
@media (max-width: 1024px) {
.addfile, .labeladdfile {
padding: 8px 16px;
font-size: 12px;
}
}
</style>
<script>
document.addEventListener('DOMContentLoaded', function() {
const { addAction } = window.JetPlugins.hooks;
addAction('jet.fb.input.makeReactive', 'set-upload-labels', function(input) {
if (!input?.nodes?.length || !input.nodes[0].classList.contains('jet-form-builder-file-upload__input')) {
return;
}
const $ = jQuery;
const upload = $(input.nodes[0]);
const fields = $(input.nodes[0].closest('.jet-form-builder-file-upload__fields'));
if (!fields.find('.addfile').length) {
fields.append(`<input type="button" class="addfile" value="Choose File"/>`);
fields.append(`<div class="labeladdfile">Choose File</div>`);
}
const labelAdd = fields.find('.labeladdfile');
const buttonAdd = fields.find('.addfile');
upload.css('display', 'none');
buttonAdd.click(function() {
upload.trigger('click');
});
input.value.watch(function() {
const fileCount = this.current.length;
const fileText = fileCount > 0 ? `Uploaded files (${fileCount})` : 'Choose File';
buttonAdd.val(fileCount > 0 ? 'File Uploaded' : 'Choose File');
labelAdd.html(fileText);
labelAdd.toggleClass('files-uploaded', fileCount > 0);
});
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment