From f67ed27faa8cad31b2c9ea91e2406bd4ac9b6e0b Mon Sep 17 00:00:00 2001 From: jeffchew Date: Mon, 30 Oct 2023 12:43:52 -0400 Subject: [PATCH] feat(components): separate class and template exports --- src/components/test-input/src/test-input.js | 37 ++++++++++++++++++ .../test-input/src/test-input.template.js | 20 ++++++++++ src/components/test-input/test-input.js | 39 ++----------------- 3 files changed, 61 insertions(+), 35 deletions(-) create mode 100644 src/components/test-input/src/test-input.js create mode 100644 src/components/test-input/src/test-input.template.js diff --git a/src/components/test-input/src/test-input.js b/src/components/test-input/src/test-input.js new file mode 100644 index 00000000..c44ad219 --- /dev/null +++ b/src/components/test-input/src/test-input.js @@ -0,0 +1,37 @@ +/** + * @license + * + * Copyright IBM Corp. 2023 + * + * This source code is licensed under the Apache-2.0 license found in the + * LICENSE file in the root directory of this source tree. + */ + +import { LitElement } from 'lit'; +import { SearchTypeaheadAPI } from '../../../services/SearchTypeahead/index.js'; + +/** + * Input component using search typeahead api + */ +export default class testInput extends LitElement { + static properties = { + searchResults: { attribute: false }, + }; + + constructor() { + super(); + this.searchResults; + } + + async getResults(searchQuery) { + const response = await SearchTypeaheadAPI.getResults(searchQuery); + return response.map((res) => res[0]); + } + + _handleInput(event) { + const { value } = event.target; + this.getResults(value).then((res) => { + this.searchResults = res; + }); + } +} diff --git a/src/components/test-input/src/test-input.template.js b/src/components/test-input/src/test-input.template.js new file mode 100644 index 00000000..7dc04b82 --- /dev/null +++ b/src/components/test-input/src/test-input.template.js @@ -0,0 +1,20 @@ +/** + * @license + * + * Copyright IBM Corp. 2023 + * + * This source code is licensed under the Apache-2.0 license found in the + * LICENSE file in the root directory of this source tree. + */ +import { html } from 'lit'; + +export function testInputTemplate(cls) { + const { _handleInput: handleInput } = cls; + return html`
+ + + ${cls.searchResults + ? cls.searchResults.map((result) => html`

${result}

`) + : undefined} +
`; +} diff --git a/src/components/test-input/test-input.js b/src/components/test-input/test-input.js index 993d331a..8241be25 100644 --- a/src/components/test-input/test-input.js +++ b/src/components/test-input/test-input.js @@ -8,45 +8,14 @@ */ import settings from '../../globals/settings.js'; -import { LitElement, html } from 'lit'; -import { SearchTypeaheadAPI } from '../../services/SearchTypeahead/index.js'; +import testInput from './src/test-input.js'; +import { testInputTemplate } from './src/test-input.template.js'; const { stablePrefix: c4aiPrefix } = settings; -/** - * Input component using search typeahead api - */ -export default class C4AITestInput extends LitElement { - static properties = { - searchResults: { attribute: false }, - }; - - constructor() { - super(); - this.searchResults; - } - - async getResults(searchQuery) { - const response = await SearchTypeaheadAPI.getResults(searchQuery); - return response.map((res) => res[0]); - } - - _handleInput(event) { - const { value } = event.target; - this.getResults(value).then((res) => { - this.searchResults = res; - }); - } - +class C4AITestInput extends testInput { render() { - const { _handleInput: handleInput } = this; - return html`
- - - ${this.searchResults - ? this.searchResults.map((result) => html`

${result}

`) - : undefined} -
`; + return testInputTemplate(this); } }