The MDSWCBase
class accounts for common functionality that can be leveraged across all MDS web components. It extends HTMLElement and is in turn extended by all MDS web components.
Prop
|
Type
|
Default
|
Description
|
---|---|---|---|
|
Boolean |
|
Determines if the |
|
Boolean |
|
Determines if |
|
Object |
Props that were passed to the constructor |
Stores the default props so they can be referenced later for slot overrides. |
|
Object |
|
Stores an object that tracks if each existing slot has been already rendered once. |
|
Object |
|
Stores an object that tracks if each existing slot has content. |
|
Boolean |
|
Determines if the component has slots. |
|
Boolean |
|
Determines if the component has been rendered or not. |
|
Boolean |
|
Determines if the component render can be invoked. |
|
Number |
|
Stores the timeout value in milliseconds that determines how much time a component has available to render before the |
|
Object |
|
Object that stores the current key/value pairs for each of the props of the component. |
The MDSWCBase
class contains methods that are part of the native custom element lifecycle, instance methods that provide common base functionality, and static helper methods. All methods can further be classified as private
or public
depending on their intended use outside of the MDSWCBase
class.
Status
|
Description
|
---|---|
Private
|
Denotes an instance method that is used internally to MDSWC and should not be overwritten. |
Public
|
Denotes a static helper method or instance method that can be invoked in components that extend |
The MDSWCBase
class leverages a subset of the custom element lifecycle methods. These methods are used to track when an element has been inserted into the DOM and when changes to the element attributes occur.
Method Name
|
Type
|
Description
|
---|---|---|
|
Private
|
Called when an observed attribute has been added, removed, updated, or replaced. Also called for initial values when an element is created by the parser or upgraded. |
|
Private
|
Called every time the element is inserted into the DOM. |
|
Public
|
Static getter method defined on each custom element. It returns the element attributes which should be watched for changes. |
MDSWCBase
contains several instance methods that provide common functionality to components which extend from MDSWCBase
.
Method Name
|
Type
|
Description
|
---|---|---|
|
Public
|
Fallback method meant to be overwritten at the child component level. |
|
Public
|
Returns a |
|
Private
|
Configures a getter and setter for all defined props. |
|
Private
|
Returns the default slot content. If no default slot content exists, it will return the Boolean value |
|
Private
|
Invoked once from |
|
Public
|
Returns a Boolean value determined by the existence of default slot content. |
|
Public
|
Returns a Boolean value determined by the existence of named slot content. |
|
Private
|
Returns an Object containing default and instance prop values. The default values are determined by the component, while the instance props are determined by values passed via HTML attributes. |
|
Private
|
Binds label functionality to MWC labeler if an MWC configuration and MWC id exist, otherwise it returns a placeholder labeler that always returns false. |
|
Private
|
Bind logging functionality to the MWC logger via a deep clone. |
|
Public
|
Suppresses the |
|
Public
|
Calls |
|
Private
|
A debounced method which renders the template and binds all events using the |
|
Private
|
Retrieves a copy of the template from the child component and checks for the existence of slots. If default slot content or named slot content is detected, it evaluates that content against possible slot prop overrides and creates the appropriate nodes needed to render the content. Once all slot content is evaluated and created, it removes all children from the currently rendered custom element and renders the newly created template. |
|
Private
|
Assigns the initial component state to the |
|
Private
|
Iterates through all the default and named slots and updates the |
|
Public
|
Suppresses the render functionality while setting a new value for a property. All validation rules still apply on the set prop value. |
|
Public
|
Creates and dispatches a |
|
Private
|
Composes and logs prop validation error messages based on the arguments it receives. |
|
Private
|
Returns a Boolean value determined by the existence of an |
|
Private
|
Iterates through all props and calls the |
|
Private
|
Validates a prop against all validation rules (e.g., required, enum, type, custom validation) specified for it. |
|
Private
|
Validates that the prop value matches the specified prop type and returns a Boolean value. |
MDSWCBase
contains several static methods that are common utility functions used by multiple MDSWC components.
Method Name
|
Type
|
Description
|
---|---|---|
|
Public
|
Returns the language id set in the MWC application helper. If no id is set, it will return |
|
Public
|
Takes a kebab-cased string like |
|
Public
|
Returns a generated random number between 0 and |
|
Public
|
Returns the data type of the prop passed to it. The data type will be one of: |
|
Public
|
Converts a camelCased string like |
|
Public
|
Given two nodes that contain child nodes, this method moves child nodes from oldParent to newParent. |
|
Public
|
Returns an object containing prop type to grammatical articles for more grammatically correct error messages. For example, |
Extending from MDSWC Base:
class MyComponent extends MDSWCBase {
static get defaultProps() {
// List all props as camelCased object keys in alphabetical order
return {
myProp: {
type: String, // type is required for all props
}
};
}
// Observe all attributes
static get observedAttributes() {
const attributes = Object.keys(MyComponent.defaultProps).map(p => MDSWCBase.kebabCase(p));
return attributes;
}
// Pass props to the MDSWC Constructor
constructor() {
super(MyComponent.defaultProps);
}
// define the component template that MDSWC Base
get template() {
return `
<div>
<h1>${this.myProp}</h1>
</div>
`;
}
}