mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-03-23 18:01:16 +01:00
feat(ui): auto-resize textareas when typing
This commit is contained in:
@@ -4,11 +4,13 @@
|
||||
{% javascript_pack 'sweetalert2' attrs="defer" %}
|
||||
{% javascript_pack 'select' attrs="defer" %}
|
||||
{% javascript_pack 'datepicker' %}
|
||||
{% javascript_pack 'autosize' attrs="defer" %}
|
||||
|
||||
{% include 'includes/scripts/hyperscript/init_tom_select.html' %}
|
||||
{% include 'includes/scripts/hyperscript/init_date_picker.html' %}
|
||||
{% include 'includes/scripts/hyperscript/hide_amount.html' %}
|
||||
{% include 'includes/scripts/hyperscript/tooltip.html' %}
|
||||
{% include 'includes/scripts/hyperscript/autosize.html' %}
|
||||
{% include 'includes/scripts/hyperscript/htmx_error_handler.html' %}
|
||||
{% include 'includes/scripts/hyperscript/sounds.html' %}
|
||||
{% include 'includes/scripts/hyperscript/swal.html' %}
|
||||
|
||||
7
app/templates/includes/scripts/hyperscript/autosize.html
Normal file
7
app/templates/includes/scripts/hyperscript/autosize.html
Normal file
@@ -0,0 +1,7 @@
|
||||
<script type="text/hyperscript">
|
||||
on htmx:afterSettle
|
||||
for elem in <.textarea/>
|
||||
autosize(elem)
|
||||
end
|
||||
end
|
||||
</script>
|
||||
@@ -1,54 +1,3 @@
|
||||
import autosize from "autosize/dist/autosize";
|
||||
|
||||
let autosize_textareas = document.querySelectorAll('textarea[autosize]');
|
||||
|
||||
autosize(autosize_textareas);
|
||||
|
||||
document.addEventListener('shown.bs.collapse', function () {
|
||||
autosize.update(autosize_textareas);
|
||||
});
|
||||
|
||||
// UPDATE AUTOSIZE TEXT AREAS FOR FORMS INSIDE HTMX MODALS
|
||||
document.addEventListener('updated.bs.modal', function () {
|
||||
let new_autosize_textareas = document.querySelectorAll('textarea[autosize]');
|
||||
autosize(new_autosize_textareas);
|
||||
});
|
||||
|
||||
let charcount_textareas = document.querySelectorAll('textarea[countchars], input[countchars]');
|
||||
charcount_textareas.forEach(formElement => {
|
||||
countTextArea(formElement);
|
||||
formElement.addEventListener('input', () => countTextArea(formElement));
|
||||
});
|
||||
|
||||
function countTextArea(formElement) {
|
||||
let name = formElement.name;
|
||||
|
||||
let max_chars = null;
|
||||
if (formElement.dataset.maxChars) {
|
||||
max_chars = formElement.dataset.maxChars;
|
||||
} else if (formElement.hasAttribute("maxlength")) {
|
||||
max_chars = formElement.getAttribute("maxlength");
|
||||
}
|
||||
|
||||
let cur_chars = formElement.value.length;
|
||||
|
||||
let wrapper = document.querySelector(`#charcount-${name}`);
|
||||
let char_counter = document.querySelector(`#char-counter-${name}`);
|
||||
let max_counter = document.querySelector(`#max-counter-${name}`);
|
||||
|
||||
char_counter.textContent = cur_chars;
|
||||
if (max_counter) {
|
||||
max_counter.textContent = max_chars;
|
||||
wrapper.classList.remove("text-bg-warning", "text-bg-normal", "text-bg-success", "text-bg-danger");
|
||||
|
||||
if (cur_chars === 0) {
|
||||
wrapper.classList.add("text-bg-secondary");
|
||||
} else if (cur_chars > max_chars - 1) {
|
||||
wrapper.classList.add("text-bg-danger");
|
||||
} else if (cur_chars < max_chars && cur_chars > max_chars * (90 / 100)) {
|
||||
wrapper.classList.add("text-bg-warning");
|
||||
} else if (cur_chars < max_chars - ((max_chars * (10 / 100)) - 1)) {
|
||||
wrapper.classList.add("text-bg-success");
|
||||
}
|
||||
}
|
||||
}
|
||||
window.autosize = autosize;
|
||||
|
||||
Reference in New Issue
Block a user