@neovici/cosmoz-input 1.7.0 → 2.0.0-beta.1
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/cosmoz-input.js +29 -15
- package/cosmoz-textarea.js +19 -16
- package/package.json +11 -12
- package/styles.js +2 -2
- package/use-input.js +34 -23
- package/es-dev-server.config.js +0 -8
package/cosmoz-input.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { html } from 'lit-html'; // eslint-disable-line object-curly-newline
|
|
2
|
-
import { live } from 'lit-html/directives/live';
|
|
3
|
-
import { ifDefined } from 'lit-html/directives/if-defined';
|
|
2
|
+
import { live } from 'lit-html/directives/live.js';
|
|
3
|
+
import { ifDefined } from 'lit-html/directives/if-defined.js';
|
|
4
4
|
|
|
5
5
|
import { component } from 'haunted';
|
|
6
6
|
import { useInput, useAllowedPattern } from './use-input';
|
|
7
7
|
import { render, attributes } from './render';
|
|
8
8
|
|
|
9
|
-
export const Input = host => {
|
|
9
|
+
export const Input = (host) => {
|
|
10
10
|
const {
|
|
11
11
|
type = 'text',
|
|
12
12
|
pattern,
|
|
@@ -19,21 +19,35 @@ export const Input = host => {
|
|
|
19
19
|
min,
|
|
20
20
|
max,
|
|
21
21
|
step,
|
|
22
|
-
maxlength
|
|
22
|
+
maxlength,
|
|
23
23
|
} = host,
|
|
24
24
|
{ onChange, onFocus, onInput } = useInput(host),
|
|
25
25
|
onBeforeInput = useAllowedPattern(allowedPattern);
|
|
26
|
-
return render(
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
26
|
+
return render(
|
|
27
|
+
html`<input
|
|
28
|
+
id="input"
|
|
29
|
+
part="input"
|
|
30
|
+
type=${type}
|
|
31
|
+
pattern=${ifDefined(pattern)}
|
|
32
|
+
autocomplete=${ifDefined(autocomplete)}
|
|
33
|
+
placeholder=${placeholder || ' '}
|
|
34
|
+
?readonly=${readonly}
|
|
35
|
+
?aria-disabled=${disabled}
|
|
36
|
+
?disabled=${disabled}
|
|
37
|
+
.value=${live(value ?? '')}
|
|
38
|
+
maxlength=${ifDefined(maxlength)}
|
|
39
|
+
@beforeinput=${onBeforeInput}
|
|
40
|
+
@input=${onInput}
|
|
41
|
+
@change=${onChange}
|
|
42
|
+
@focus=${onFocus}
|
|
43
|
+
@blur=${onFocus}
|
|
44
|
+
min=${ifDefined(min)}
|
|
45
|
+
max=${ifDefined(max)}
|
|
46
|
+
step=${ifDefined(step)}
|
|
47
|
+
/>`,
|
|
48
|
+
host
|
|
49
|
+
);
|
|
35
50
|
},
|
|
36
|
-
|
|
37
51
|
observedAttributes = [
|
|
38
52
|
'type',
|
|
39
53
|
'pattern',
|
|
@@ -41,7 +55,7 @@ export const Input = host => {
|
|
|
41
55
|
'min',
|
|
42
56
|
'max',
|
|
43
57
|
'step',
|
|
44
|
-
...attributes
|
|
58
|
+
...attributes,
|
|
45
59
|
];
|
|
46
60
|
|
|
47
61
|
customElements.define('cosmoz-input', component(Input, { observedAttributes }));
|
package/cosmoz-textarea.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { html } from 'lit-html'; // eslint-disable-line object-curly-newline
|
|
2
|
-
import { live } from 'lit-html/directives/live';
|
|
3
|
-
import { ifDefined } from 'lit-html/directives/if-defined';
|
|
2
|
+
import { live } from 'lit-html/directives/live.js';
|
|
3
|
+
import { ifDefined } from 'lit-html/directives/if-defined.js';
|
|
4
4
|
|
|
5
5
|
import { component } from 'haunted';
|
|
6
6
|
import { useInput, useAutosize } from './use-input';
|
|
7
7
|
import { render, attributes } from './render';
|
|
8
8
|
|
|
9
|
-
export const Textarea = host => {
|
|
9
|
+
export const Textarea = (host) => {
|
|
10
10
|
const {
|
|
11
11
|
autocomplete,
|
|
12
12
|
value,
|
|
@@ -15,24 +15,27 @@ export const Textarea = host => {
|
|
|
15
15
|
disabled,
|
|
16
16
|
rows,
|
|
17
17
|
cols,
|
|
18
|
-
maxlength
|
|
18
|
+
maxlength,
|
|
19
19
|
} = host,
|
|
20
20
|
{ onChange, onFocus, onInput } = useInput(host);
|
|
21
21
|
|
|
22
22
|
useAutosize(host);
|
|
23
23
|
|
|
24
|
-
return render(
|
|
24
|
+
return render(
|
|
25
|
+
html`
|
|
25
26
|
<textarea id="input" part="input" style="resize: none"
|
|
26
|
-
autocomplete=${
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
autocomplete=${ifDefined(autocomplete)} placeholder=${
|
|
28
|
+
placeholder || ' '
|
|
29
|
+
} rows=${rows ?? 1} cols=${ifDefined(cols)}
|
|
30
|
+
?readonly=${readonly} ?aria-disabled=${disabled} ?disabled=${disabled}
|
|
31
|
+
.value=${live(value ?? '')} maxlength=${ifDefined(maxlength)} @input=${onInput}
|
|
32
|
+
@change=${onChange} @focus=${onFocus} @blur=${onFocus}>`,
|
|
33
|
+
host
|
|
34
|
+
);
|
|
31
35
|
},
|
|
36
|
+
observedAttributes = ['rows', ...attributes];
|
|
32
37
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
customElements.define('cosmoz-textarea', component(Textarea, { observedAttributes }));
|
|
38
|
+
customElements.define(
|
|
39
|
+
'cosmoz-textarea',
|
|
40
|
+
component(Textarea, { observedAttributes })
|
|
41
|
+
);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@neovici/cosmoz-input",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0-beta.1",
|
|
4
4
|
"description": "A input web component",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"lit-html",
|
|
@@ -23,11 +23,10 @@
|
|
|
23
23
|
"scripts": {
|
|
24
24
|
"lint": "eslint --cache --ext .js .",
|
|
25
25
|
"lint-tsc": "tsc",
|
|
26
|
-
"start": "
|
|
26
|
+
"start": "wds",
|
|
27
27
|
"test": "wtr --coverage",
|
|
28
28
|
"test:watch": "wtr --watch",
|
|
29
29
|
"prepare": "husky install",
|
|
30
|
-
"storybook": "start-storybook --node-resolve --watch --open",
|
|
31
30
|
"storybook:build": "build-storybook",
|
|
32
31
|
"storybook:deploy": "storybook-to-ghpages"
|
|
33
32
|
},
|
|
@@ -56,22 +55,22 @@
|
|
|
56
55
|
]
|
|
57
56
|
},
|
|
58
57
|
"dependencies": {
|
|
59
|
-
"@neovici/cosmoz-utils": "^
|
|
60
|
-
"haunted": "^
|
|
61
|
-
"lit-html": "^
|
|
58
|
+
"@neovici/cosmoz-utils": "^4.0.0-beta.2",
|
|
59
|
+
"haunted": "^5.0.0",
|
|
60
|
+
"lit-html": "^2.0.0"
|
|
62
61
|
},
|
|
63
62
|
"devDependencies": {
|
|
64
|
-
"@commitlint/cli": "^
|
|
65
|
-
"@commitlint/config-conventional": "^
|
|
63
|
+
"@commitlint/cli": "^17.0.0",
|
|
64
|
+
"@commitlint/config-conventional": "^17.0.0",
|
|
66
65
|
"@neovici/cfg": "^1.13.1",
|
|
67
|
-
"@open-wc/
|
|
68
|
-
"@open-wc/testing": "^2.5.0",
|
|
66
|
+
"@open-wc/testing": "^3.0.0",
|
|
69
67
|
"@semantic-release/changelog": "^6.0.0",
|
|
70
68
|
"@semantic-release/git": "^10.0.0",
|
|
71
69
|
"@storybook/storybook-deployer": "^2.8.5",
|
|
72
|
-
"
|
|
70
|
+
"@web/dev-server-storybook": "^0.5.1",
|
|
71
|
+
"husky": "^8.0.0",
|
|
73
72
|
"semantic-release": "^19.0.0",
|
|
74
|
-
"sinon": "^
|
|
73
|
+
"sinon": "^14.0.0",
|
|
75
74
|
"typescript": "^4.6.0"
|
|
76
75
|
}
|
|
77
76
|
}
|
package/styles.js
CHANGED
|
@@ -3,7 +3,7 @@ export const styles = css`
|
|
|
3
3
|
:host {
|
|
4
4
|
--font-family: var(
|
|
5
5
|
--cosmoz-input-font-family,
|
|
6
|
-
var(--paper-font-subhead_-_font-family,
|
|
6
|
+
var(--paper-font-subhead_-_font-family, 'Roboto', 'Noto', sans-serif)
|
|
7
7
|
);
|
|
8
8
|
--font-size: var(
|
|
9
9
|
--cosmoz-input-font-size,
|
|
@@ -109,7 +109,7 @@ export const styles = css`
|
|
|
109
109
|
position: relative;
|
|
110
110
|
}
|
|
111
111
|
.line::before {
|
|
112
|
-
content:
|
|
112
|
+
content: '';
|
|
113
113
|
position: absolute;
|
|
114
114
|
display: block;
|
|
115
115
|
border-bottom: 2px solid transparent;
|
package/use-input.js
CHANGED
|
@@ -2,11 +2,20 @@ import { useCallback, useEffect, useMemo } from 'haunted';
|
|
|
2
2
|
import { useImperativeApi } from '@neovici/cosmoz-utils/lib/hooks/use-imperative-api';
|
|
3
3
|
import { notifyProperty } from '@neovici/cosmoz-utils/lib/hooks/use-notify-property';
|
|
4
4
|
|
|
5
|
-
export const useInput = host => {
|
|
5
|
+
export const useInput = (host) => {
|
|
6
6
|
const root = host.shadowRoot,
|
|
7
|
-
onChange = useCallback(
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
onChange = useCallback(
|
|
8
|
+
(e) => host.dispatchEvent(new Event(e.type, { bubbles: e.bubbles })),
|
|
9
|
+
[]
|
|
10
|
+
),
|
|
11
|
+
onInput = useCallback(
|
|
12
|
+
(e) => notifyProperty(host, 'value', e.target.value),
|
|
13
|
+
[]
|
|
14
|
+
),
|
|
15
|
+
onFocus = useCallback(
|
|
16
|
+
(e) => notifyProperty(host, 'focused', e.type === 'focus'),
|
|
17
|
+
[]
|
|
18
|
+
),
|
|
10
19
|
focus = useCallback(() => root.querySelector('#input')?.focus(), []),
|
|
11
20
|
validate = useCallback(() => {
|
|
12
21
|
const valid = root.querySelector('#input')?.checkValidity();
|
|
@@ -17,12 +26,13 @@ export const useInput = host => {
|
|
|
17
26
|
useImperativeApi({ focus, validate }, [focus, validate]);
|
|
18
27
|
|
|
19
28
|
useEffect(() => {
|
|
20
|
-
const onMouseDown = e => {
|
|
29
|
+
const onMouseDown = (e) => {
|
|
21
30
|
if (e.defaultPrevented || e.target.matches('input, textarea, label')) {
|
|
22
31
|
return;
|
|
23
32
|
}
|
|
24
33
|
e.preventDefault(); // don't blur
|
|
25
|
-
if (!host.matches(':focus-within')) {
|
|
34
|
+
if (!host.matches(':focus-within')) {
|
|
35
|
+
// if input not focused
|
|
26
36
|
focus(); // focus input
|
|
27
37
|
}
|
|
28
38
|
};
|
|
@@ -34,24 +44,24 @@ export const useInput = host => {
|
|
|
34
44
|
return {
|
|
35
45
|
onChange,
|
|
36
46
|
onFocus,
|
|
37
|
-
onInput
|
|
47
|
+
onInput,
|
|
38
48
|
};
|
|
39
49
|
},
|
|
40
|
-
useAllowedPattern = allowedPattern
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const regexp = new RegExp(allowedPattern, 'u');
|
|
45
|
-
return e => {
|
|
46
|
-
if (!e.defaultPrevent && e.data && !regexp.test(e.data)) {
|
|
47
|
-
e.preventDefault();
|
|
50
|
+
useAllowedPattern = (allowedPattern) =>
|
|
51
|
+
useMemo(() => {
|
|
52
|
+
if (allowedPattern == null) {
|
|
53
|
+
return;
|
|
48
54
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
55
|
+
const regexp = new RegExp(allowedPattern, 'u');
|
|
56
|
+
return (e) => {
|
|
57
|
+
if (!e.defaultPrevent && e.data && !regexp.test(e.data)) {
|
|
58
|
+
e.preventDefault();
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}, [allowedPattern]),
|
|
62
|
+
autosize = (input) => {
|
|
53
63
|
input.style.height = '';
|
|
54
|
-
input.style.height = `${
|
|
64
|
+
input.style.height = `${input.scrollHeight}px`;
|
|
55
65
|
},
|
|
56
66
|
limit = (input, maxRows) => {
|
|
57
67
|
if (maxRows > 0) {
|
|
@@ -64,16 +74,17 @@ export const useInput = host => {
|
|
|
64
74
|
input.setAttribute('rows', rows);
|
|
65
75
|
}
|
|
66
76
|
},
|
|
67
|
-
useAutosize = host => {
|
|
77
|
+
useAutosize = (host) => {
|
|
68
78
|
const { value, maxRows } = host,
|
|
69
79
|
input = useMemo(() => () => host.shadowRoot.querySelector('#input'), []);
|
|
70
80
|
useEffect(() => limit(input(), maxRows), [maxRows, input]);
|
|
71
81
|
useEffect(() => autosize(input()), [input, value]);
|
|
72
82
|
useEffect(() => {
|
|
73
83
|
const el = input(),
|
|
74
|
-
observer = new ResizeObserver(() =>
|
|
84
|
+
observer = new ResizeObserver(() =>
|
|
85
|
+
requestAnimationFrame(() => autosize(el))
|
|
86
|
+
);
|
|
75
87
|
observer.observe(el);
|
|
76
88
|
return () => observer.unobserve(el);
|
|
77
89
|
}, [input]);
|
|
78
|
-
|
|
79
90
|
};
|