-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
POC - Adding subscription controllers #1901
Changes from 1 commit
365512e
2307580
015aec7
367dda8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { isCustomFormElement } from '../form/form-helper.js'; | ||
import { EventSubscriberController, ForPropertySubscriberController } from '../../helpers/subscriptionControllers.js'; | ||
|
||
export const ValidationCustomMixin = superclass => class extends superclass { | ||
|
||
|
@@ -11,60 +11,49 @@ export const ValidationCustomMixin = superclass => class extends superclass { | |
|
||
constructor() { | ||
super(); | ||
this._forElement = null; | ||
|
||
this._eventSubscriberController = new EventSubscriberController(this, {}, | ||
{ eventName: 'd2l-validation-custom-connected' } | ||
); | ||
|
||
this._forPropertySubscriberController = new ForPropertySubscriberController(this, | ||
{ onUnsubscribe: this._onUnsubscribe.bind(this) }, | ||
{ forProperty: 'for' } | ||
); | ||
} | ||
|
||
get forElement() { | ||
return this._forElement; | ||
if (this.for) { | ||
// Validation custom components only support one for element | ||
return this._forPropertySubscriberController.providers[0]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest doing an array length check first and return |
||
} else { | ||
return this._eventSubscriberController.provider; | ||
} | ||
} | ||
|
||
connectedCallback() { | ||
super.connectedCallback(); | ||
this._updateForElement(); | ||
this.dispatchEvent(new CustomEvent('d2l-validation-custom-connected', { bubbles: true })); | ||
this._eventSubscriberController.hostConnected(); | ||
} | ||
|
||
disconnectedCallback() { | ||
super.disconnectedCallback(); | ||
if (isCustomFormElement(this._forElement)) { | ||
this._forElement.validationCustomDisconnected(this); | ||
} | ||
this._forElement = null; | ||
this.dispatchEvent(new CustomEvent('d2l-validation-custom-disconnected')); | ||
this._eventSubscriberController.hostDisconnected(); | ||
this._forPropertySubscriberController.hostDisconnected(); | ||
} | ||
|
||
updated(changedProperties) { | ||
super.updated(changedProperties); | ||
|
||
changedProperties.forEach((_, prop) => { | ||
if (prop === 'for') { | ||
this._updateForElement(); | ||
} | ||
}); | ||
this._forPropertySubscriberController.hostUpdated(changedProperties); | ||
} | ||
|
||
async validate() { | ||
throw new Error('ValidationCustomMixin requires validate to be overridden'); | ||
} | ||
|
||
_updateForElement() { | ||
const oldForElement = this._forElement; | ||
if (this.for) { | ||
const root = this.getRootNode(); | ||
this._forElement = root.getElementById(this.for); | ||
if (!this._forElement) { | ||
throw new Error(`validation-custom failed to find element with id ${this.for}`); | ||
} | ||
} else { | ||
this._forElement = null; | ||
} | ||
if (this._forElement !== oldForElement) { | ||
if (isCustomFormElement(oldForElement)) { | ||
oldForElement.validationCustomDisconnected(this); | ||
} | ||
if (isCustomFormElement(this._forElement)) { | ||
this._forElement.validationCustomConnected(this); | ||
} | ||
_onUnsubscribe() { | ||
if (this._forPropertySubscriberController.provider.length === 0) { | ||
throw new Error(`validation-custom failed to find element with id ${this.for}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When does this end up getting thrown? Seems like before it would throw during Having some kind of built-in error throwing capability in the controller itself might be a nice future enhancement too... could simplify the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I didn't test this out before/after but wanted to keep it in to remember - I assume it always "just worked" because people are putting the validator right beside the element. I was looking at adding a "waitTime" to handle There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the idea of an error callback a lot. 👍 |
||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the event subscriber needed? Seems like the custom validator isn't expecting descendent elements to fire this event.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can tell, the event is used by
FormMixin
andFormElementMixin
to allow for someone to put ad2l-validation-custom
element inside of one of them without adding afor
id. Like, ifd2l-input-text
wanted to instead just put ad2l-validation-custom
element inside of itself. There are no examples of this on the demo pages and I had to hack something together to see it happen. When we actually implement this, I think it's worth trying to figure out if anyone actually ever uses this, and decide if that's something we'd still want to support.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoa! Yeah I'd definitely lean towards just removing that functionality altogether.