@digital-realty/ix-search-bar 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 ix-search-bar
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # \<ix-search-bar>
2
+
3
+ This webcomponent follows the [open-wc](https://github.com/open-wc/open-wc) recommendation.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm i @digital-realty/ix-search-bar
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```html
14
+ <script type="module">
15
+ import '@digital-realty/ix-search-bar';
16
+ </script>
17
+
18
+ <ix-search-bar></ix-search-bar>
19
+ ```
20
+ ### In React
21
+ ```html
22
+ <script type="module">
23
+ import { IxSearchBar } from '@digital-realty/ix-search-bar/IxSearchBar'
24
+ </script>
25
+ ```
26
+ ## Linting and formatting
27
+
28
+ To scan the project for linting and formatting errors, run
29
+
30
+ ```bash
31
+ npm run lint
32
+ ```
33
+
34
+ To automatically fix linting and formatting errors, run
35
+
36
+ ```bash
37
+ npm run format
38
+ ```
39
+
40
+ ## Testing with Web Test Runner
41
+
42
+ To execute a single test run:
43
+
44
+ ```bash
45
+ npm run test
46
+ ```
47
+
48
+ To run the tests in interactive watch mode run:
49
+
50
+ ```bash
51
+ npm run test:watch
52
+ ```
53
+
54
+
55
+ ## Tooling configs
56
+
57
+ For most of the tools, the configuration is in the `package.json` to reduce the amount of files in your project.
58
+
59
+ If you customize the configuration a lot, you can consider moving them to individual files.
60
+
61
+ ## Local Demo with `web-dev-server`
62
+
63
+ ```bash
64
+ npm start
65
+ ```
66
+
67
+ To run a local development server that serves the basic demo located in `demo/index.html`
@@ -0,0 +1,2 @@
1
+ export { IxSearchBar } from './IxSearchBar.js';
2
+ //# sourceMappingURL=index.js.map
package/package.json ADDED
@@ -0,0 +1,99 @@
1
+ {
2
+ "name": "@digital-realty/ix-search-bar",
3
+ "description": "Webcomponent ix-search-bar following open-wc recommendations",
4
+ "license": "MIT",
5
+ "author": "Digital realty",
6
+ "version": "2.0.4",
7
+ "type": "module",
8
+ "main": "dist/src/index.js",
9
+ "module": "dist/src/index.js",
10
+ "exports": {
11
+ ".": "./dist/src/index.js",
12
+ "./ix-search-bar.js": "./dist/src/ix-search-bar.js",
13
+ "./IxSearchBar": "./dist/src/react/IxSearchBar.js"
14
+ },
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "files": [
19
+ "src"
20
+ ],
21
+ "scripts": {
22
+ "analyze": "cem analyze --litelement",
23
+ "start": "tsc && concurrently -k -r \"tsc --watch --preserveWatchOutput\" \"wds\"",
24
+ "build": "tsc && npm run analyze -- --exclude dist",
25
+ "prepublish": "tsc && npm run analyze -- --exclude dist",
26
+ "lint": "eslint --ext .ts,.html . --ignore-path .gitignore && prettier \"**/*.ts\" --check --ignore-path .gitignore",
27
+ "format": "eslint --ext .ts,.html . --fix --ignore-path .gitignore && prettier \"**/*.ts\" --write --ignore-path .gitignore",
28
+ "test": "tsc && wtr --coverage",
29
+ "test:watch": "tsc && concurrently -k -r \"tsc --watch --preserveWatchOutput\" \"wtr --watch\""
30
+ },
31
+ "dependencies": {
32
+ "@digital-realty/ix-textbox": "^2.0.5",
33
+ "@lit-labs/react": "^2.0.3",
34
+ "lit": "^2.0.2",
35
+ "react": "^18.2.0"
36
+ },
37
+ "devDependencies": {
38
+ "@custom-elements-manifest/analyzer": "^0.4.17",
39
+ "@open-wc/building-rollup": "^3.0.2",
40
+ "@open-wc/eslint-config": "^9.2.1",
41
+ "@open-wc/testing": "^3.1.6",
42
+ "@types/react": "^18.2.22",
43
+ "@typescript-eslint/eslint-plugin": "^5.48.0",
44
+ "@typescript-eslint/parser": "^5.48.0",
45
+ "@web/dev-server": "^0.1.34",
46
+ "@web/test-runner": "^0.14.0",
47
+ "concurrently": "^5.3.0",
48
+ "eslint": "^8.31.0",
49
+ "eslint-config-prettier": "^8.3.0",
50
+ "husky": "^4.3.8",
51
+ "lint-staged": "^10.5.4",
52
+ "prettier": "^2.4.1",
53
+ "tslib": "^2.3.1",
54
+ "typescript": "^4.5.2"
55
+ },
56
+ "customElements": "custom-elements.json",
57
+ "eslintConfig": {
58
+ "parser": "@typescript-eslint/parser",
59
+ "root": true,
60
+ "extends": [
61
+ "@open-wc/eslint-config",
62
+ "eslint-config-prettier",
63
+ "plugin:json/recommended"
64
+ ],
65
+ "plugins": [
66
+ "@typescript-eslint"
67
+ ],
68
+ "rules": {
69
+ "class-methods-use-this": "off",
70
+ "no-unused-vars": "off",
71
+ "@typescript-eslint/no-unused-vars": [
72
+ "error"
73
+ ],
74
+ "import/no-unresolved": "off",
75
+ "import/extensions": [
76
+ "error",
77
+ "always",
78
+ {
79
+ "ignorePackages": true
80
+ }
81
+ ]
82
+ }
83
+ },
84
+ "prettier": {
85
+ "singleQuote": true,
86
+ "arrowParens": "avoid"
87
+ },
88
+ "husky": {
89
+ "hooks": {
90
+ "pre-commit": "lint-staged"
91
+ }
92
+ },
93
+ "lint-staged": {
94
+ "*.ts": [
95
+ "eslint --fix",
96
+ "prettier --write"
97
+ ]
98
+ }
99
+ }
@@ -0,0 +1,94 @@
1
+ import { html, LitElement } from 'lit';
2
+ import { property } from 'lit/decorators.js';
3
+ import '@digital-realty/ix-textbox';
4
+ import { IxSearchBarStyle } from './ix-search-bar-styles.js';
5
+ import { Item, TextChangeEvent } from './types/index.js';
6
+
7
+ export class IxSearchBar extends LitElement {
8
+ @property({ type: String }) value = '';
9
+
10
+ @property({ type: Array }) items: Item[] = [];
11
+
12
+ @property({ type: Array }) searchFields: string[] = [];
13
+
14
+ @property({ type: Array }) foundItems: Item[] = [];
15
+
16
+ static get styles() {
17
+ return [IxSearchBarStyle];
18
+ }
19
+
20
+ connectedCallback() {
21
+ super.connectedCallback();
22
+ this.resetResults();
23
+ }
24
+
25
+ resetResults() {
26
+ this.foundItems = this.items;
27
+ }
28
+
29
+ get itemsCount() {
30
+ if (Array.isArray(this.foundItems) && this.foundItems.length) {
31
+ return this.foundItems.length;
32
+ }
33
+ return 0;
34
+ }
35
+
36
+ get itemsLabel() {
37
+ return this.itemsCount === 1 ? 'item' : 'items';
38
+ }
39
+
40
+ _dispatchEvent(items: Item[]): void {
41
+ const resultsEvent = new CustomEvent('onResultsFound', {
42
+ detail: { items },
43
+ composed: true,
44
+ });
45
+ this.dispatchEvent(resultsEvent);
46
+ }
47
+
48
+ filterItems(value: string) {
49
+ this.value = value;
50
+ if (value === '') {
51
+ this.resetResults();
52
+ this._dispatchEvent(this.items);
53
+ return;
54
+ }
55
+
56
+ const results: Item[] = [];
57
+
58
+ for (const item of this.items) {
59
+ for (const field of this.searchFields) {
60
+ if (field in item) {
61
+ if (
62
+ item[field]
63
+ .toLocaleLowerCase()
64
+ .includes(this.value.toLocaleLowerCase())
65
+ ) {
66
+ results.push(item);
67
+ break;
68
+ }
69
+ }
70
+ }
71
+ }
72
+
73
+ this.foundItems = results;
74
+ this._dispatchEvent(results);
75
+ }
76
+
77
+ render() {
78
+ return html`
79
+ <ix-textbox
80
+ type="search"
81
+ @textChange="${(e: TextChangeEvent) =>
82
+ this.filterItems(e.detail.value)}"
83
+ ></ix-textbox>
84
+
85
+ <div class="items-count">${this.itemsCount} ${this.itemsLabel}</div>
86
+ `;
87
+ }
88
+ }
89
+
90
+ declare global {
91
+ interface HTMLElementTagNameMap {
92
+ 'ix-search-bar': IxSearchBar;
93
+ }
94
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { IxSearchBar } from './IxSearchBar.js';
@@ -0,0 +1,29 @@
1
+ import { css } from 'lit';
2
+
3
+ export const IxSearchBarStyle = css`
4
+ :host {
5
+ display: flex;
6
+ justify-content: space-between;
7
+ align-items: center;
8
+ border: 1px solid lightgrey;
9
+ border-width: 1px 0;
10
+ padding: 1rem 0;
11
+ flex-wrap: wrap;
12
+ margin-bottom: 1.5rem;
13
+ }
14
+
15
+ html {
16
+ color: #333;
17
+ }
18
+
19
+ ix-textbox {
20
+ color: #333;
21
+ flex: 1 0 min(280px, 100%);
22
+ }
23
+
24
+ .items-count {
25
+ flex: 1000 0 7em;
26
+ text-align: right;
27
+ padding: 0.5em 0;
28
+ }
29
+ `;
@@ -0,0 +1,3 @@
1
+ import { IxSearchBar } from './IxSearchBar.js';
2
+
3
+ window.customElements.define('ix-search-bar', IxSearchBar);
@@ -0,0 +1,11 @@
1
+ import React from 'react';
2
+ import { createComponent } from '@lit-labs/react';
3
+ import { IxSearchBar as LitComp } from '../IxSearchBar.js';
4
+
5
+ window.customElements.define('ix-search-bar', LitComp);
6
+
7
+ export const IxSearchBar = createComponent({
8
+ tagName: 'ix-search-bar',
9
+ elementClass: LitComp,
10
+ react: React,
11
+ });
@@ -0,0 +1,12 @@
1
+ /**
2
+ * The item being searched for.
3
+ */
4
+ export type Item = {
5
+ [key: string]: string;
6
+ };
7
+
8
+ /**
9
+ * An event which is fired when the textfield's value changes.
10
+ * It passes the value of the textfield.
11
+ */
12
+ export type TextChangeEvent = CustomEvent<{ value: string }>;