-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Admin][UI] Add error indicators for form tabs and accordions (#16263)
| Q | A |-----------------|----- | Branch? | bootstrap-admin-panel | Bug fix? | kinda | New feature? | no | BC breaks? | no | Deprecations? | no | Related tickets | - | License | MIT https://github.com/Sylius/Sylius/assets/9448101/a94e450e-6530-49e7-b594-d67ff828dd51
- Loading branch information
Showing
6 changed files
with
128 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/Sylius/Bundle/AdminBundle/Resources/assets/controllers/CompoundFormErrorsController.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { Controller } from '@hotwired/stimulus'; | ||
|
||
/* stimulusFetch: 'lazy' */ | ||
export default class extends Controller { | ||
tabErrorBadgeClass = 'tab-error'; | ||
accordionErrorBadgeClass = 'accordion-error'; | ||
observer; | ||
|
||
initialize() { | ||
super.initialize(); | ||
|
||
this.observer = new MutationObserver(() => { | ||
this.reloadBadges(); | ||
}); | ||
} | ||
|
||
connect() { | ||
super.connect(); | ||
|
||
this.updateFormErrors(); | ||
this.observe(); | ||
} | ||
|
||
disconnect() { | ||
super.disconnect(); | ||
|
||
this.observer.disconnect(); | ||
} | ||
|
||
observe() { | ||
this.observer.observe(this.element, { attributes: false, childList: true, subtree: true }); | ||
} | ||
|
||
reloadBadges() { | ||
this.observer.disconnect(); | ||
this.clearBadges(); | ||
this.updateFormErrors(); | ||
this.observe(); | ||
} | ||
|
||
clearBadges() { | ||
this.element.querySelectorAll(".tab-error").forEach(el => el.remove()); | ||
this.element.querySelectorAll(".accordion-error").forEach(el => el.remove()); | ||
} | ||
|
||
updateFormErrors() { | ||
this.updateErrorBadges(this.element.querySelector('[role="tablist"]'), this.tabErrorBadgeClass); | ||
this.updateErrorBadges(this.element.querySelector('div.accordion'), this.accordionErrorBadgeClass); | ||
} | ||
|
||
updateErrorBadges(controlElementsContainer, badgeClass) { | ||
if (null === controlElementsContainer) { | ||
return; | ||
} | ||
|
||
const document = controlElementsContainer.ownerDocument; | ||
controlElementsContainer.querySelectorAll('button[type="button"][data-bs-toggle]').forEach((controlElement) => { | ||
const errorsCount = this.countErrors(controlElement); | ||
if (errorsCount > 0) { | ||
const errorElement = document.createElement('div'); | ||
errorElement.classList.add(badgeClass); | ||
errorElement.innerText = errorsCount.toString(); | ||
controlElement.appendChild(errorElement); | ||
} | ||
}); | ||
} | ||
|
||
countErrors(element) { | ||
const elementTarget = this.element.querySelector(element.getAttribute('data-bs-target')); | ||
|
||
return elementTarget.querySelectorAll('.is-invalid').length + | ||
elementTarget.querySelectorAll('.alert-danger').length; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/Sylius/Bundle/AdminBundle/templates/shared/form_theme.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters