Ghost/ghost/admin/app/modifiers/validation-status.js
Kevin Ansfield 9fd87f565d Migrated <GhValidationStatusContainer> to {{validation-status}} modifier
no issue

- moved logic from `<GhValidationStatusContainer>` to a new `validation-status` modifier
  - removes a usage of the `ValidationState` mixin
  - migrated uses of the component to a mixin
  - paves the way for full removal of the `ValidationState` mixin in later refactors (mixins are deprecated)
- migrated `<GhFormGroup>` to a glimmer component
  - swapped the extend of `GhValidationStatusContainer` to usage of the `validation-status` modifier with a template-only component
  - updated all `<GhFormGroup>` to use the standard `class=""` instead of `@classNames=""` and `@class=""`
  - allows `data-test-*` attributes to be added to uses of `<FormGroup>` to help when complex components are grouped as a form input
2022-12-09 12:38:35 +00:00

47 lines
1.4 KiB
JavaScript

import Modifier from 'ember-modifier';
import {isEmpty} from '@ember/utils';
const errorClass = 'error';
const successClass = 'success';
export default class ValidationStatusModifier extends Modifier {
modify(element, positional, {errors, property, hasValidated}) {
const validationClass = this.errorClass(errors, property, hasValidated);
element.classList.remove(errorClass);
element.classList.remove(successClass);
if (validationClass) {
element.classList.add(validationClass);
}
}
errorClass(errors, property, hasValidated) {
const hasError = this.hasError(errors, property, hasValidated);
if (hasValidated && hasValidated.includes(property)) {
return hasError ? errorClass : successClass;
} else {
return '';
}
}
hasError(errors, property, hasValidated) {
// if we aren't looking at a specific property we always want an error class
if (!property && errors && !errors.get('isEmpty')) {
return true;
}
// If we haven't yet validated this field, there is no validation class needed
if (!hasValidated || !hasValidated.includes(property)) {
return false;
}
if (errors && !isEmpty(errors.errorsFor(property))) {
return true;
}
return false;
}
}