Skip to content

Commit

Permalink
feat(api): add register static method
Browse files Browse the repository at this point in the history
AuroInput.register is to easily register the element without extra importing
`import "@aurodesignsystem/auro-input"` will still register this element to <auro-input>
`import { AuroInput } from '../src/auro-input.js` wont register this element until AuroInput.register gets called
  • Loading branch information
sun-mota committed Oct 17, 2024
1 parent 80b84af commit 663a6fc
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 18 deletions.
3 changes: 1 addition & 2 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@

<script type="module">
import { AuroInput } from '../src/auro-input.js';
import * as RuntimeUtils from '@aurodesignsystem/auro-library/scripts/utils/runtimeUtils.mjs';

RuntimeUtils.default.prototype.registerComponent('custom-input', AuroInput);
AuroInput.register('custom-input');

import { initExamples } from "./index.min.js";

Expand Down
5 changes: 2 additions & 3 deletions demo/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,12 @@ The `<auro-input>` component supports setting a custom validity message specific

There are two important parts of every Auro component. The <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes">class</a> and the custom clement. The class is exported and then used as part of defining the Web Component. When importing this component as described in the <a href="#install">install</a> section, the class is imported and the `auro-input` custom element is defined automatically.

To protect from versioning conflicts with other instances of the component being loaded, it is recommended to use our `registerComponent(name)` method and pass in a unique name.
To protect from versioning conflicts with other instances of the component being loaded, it is recommended to use our `AuroInput.register(name)` method and pass in a unique name.

```js
import { AuroInput } from './src/auro-input.js';
import * as RuntimeUtils from '@aurodesignsystem/auro-library/scripts/utils/runtimeUtils.mjs';

RuntimeUtils.default.prototype.registerComponent('custom-input', AuroInput);
AuroInput.register('custom-input');
```

This will create a new custom element that you can use in your HTML that will function identically to the `<auro-input>` element.
Expand Down
5 changes: 2 additions & 3 deletions docs/partials/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,12 @@ The `<auro-input>` component supports setting a custom validity message specific

There are two important parts of every Auro component. The <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes">class</a> and the custom clement. The class is exported and then used as part of defining the Web Component. When importing this component as described in the <a href="#install">install</a> section, the class is imported and the `auro-input` custom element is defined automatically.

To protect from versioning conflicts with other instances of the component being loaded, it is recommended to use our `registerComponent(name)` method and pass in a unique name.
To protect from versioning conflicts with other instances of the component being loaded, it is recommended to use our `AuroInput.register(name)` method and pass in a unique name.

```js
import { AuroInput } from './src/auro-input.js';
import * as RuntimeUtils from '@aurodesignsystem/auro-library/scripts/utils/runtimeUtils.mjs';

RuntimeUtils.default.prototype.registerComponent('custom-input', AuroInput);
AuroInput.register('custom-input');
```

This will create a new custom element that you can use in your HTML that will function identically to the `<auro-input>` element.
Expand Down
3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { AuroInput } from './src/auro-input.js';
import * as RuntimeUtils from '@aurodesignsystem/auro-library/scripts/utils/runtimeUtils.mjs';

RuntimeUtils.default.prototype.registerComponent('custom-input', AuroInput);
AuroInput.register();
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 14 additions & 5 deletions src/auro-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import i18n from './i18n.js';
import BaseInput from './base-input.js';

import { AuroDependencyVersioning } from '@aurodesignsystem/auro-library/scripts/runtime/dependencyTagVersioning.mjs';
import AuroLibraryRuntimeUtils from '@aurodesignsystem/auro-library/scripts/utils/runtimeUtils.mjs';

import { AuroIcon } from '@aurodesignsystem/auro-icon/src/auro-icon.js';
import iconVersion from './iconVersion.js';
import { AuroButton } from '@aurodesignsystem/auro-button/src/auro-button.js';
Expand All @@ -39,6 +41,18 @@ export class AuroInput extends BaseInput {
this.buttonTag = versioning.generateTag('auro-button', buttonVersion, AuroButton);
}

/**
* This will register this element with the browser.
* @param {string} [name="auro-inpu"] - The name of element that you want to register to.
*
* @example
* AuroInput.register("custom-input") // this will register this element to <custom-input/>
*
*/
static register(name = "auro-input") {
AuroLibraryRuntimeUtils.prototype.registerComponent(name, AuroInput);
}

/**
* Function to determine if the input is meant to render an icon visualizing the input type.
* @private
Expand Down Expand Up @@ -206,8 +220,3 @@ export class AuroInput extends BaseInput {
`;
}
}

// default internal definition
if (!customElements.get("auro-input")) {
customElements.define("auro-input", AuroInput);
}
2 changes: 1 addition & 1 deletion test/auro-input.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fixture, html, expect, elementUpdated, oneEvent } from '@open-wc/testing';
import '../src/auro-input.js';
import '../index.js';

describe('auro-input', () => {

Expand Down

0 comments on commit 663a6fc

Please sign in to comment.