@neovici/cosmoz-input 3.9.0 → 3.10.0
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/dist/cosmoz-input.js +3 -1
- package/dist/cosmoz-textarea.js +3 -1
- package/dist/use-input.d.ts +4 -0
- package/dist/use-input.js +11 -4
- package/package.json +1 -1
package/dist/cosmoz-input.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { html } from 'lit-html';
|
|
2
2
|
import { live } from 'lit-html/directives/live.js';
|
|
3
|
+
import { ref } from 'lit-html/directives/ref.js';
|
|
3
4
|
import { ifDefined } from 'lit-html/directives/if-defined.js';
|
|
4
5
|
import { component } from 'haunted';
|
|
5
6
|
import { useInput } from './use-input';
|
|
@@ -17,10 +18,11 @@ const observedAttributes = [
|
|
|
17
18
|
...attributes,
|
|
18
19
|
];
|
|
19
20
|
export const Input = (host) => {
|
|
20
|
-
const { type = 'text', pattern, allowedPattern, autocomplete, value, readonly, disabled, min, max, step, maxlength, } = host, { onChange, onFocus, onInput } = useInput(host);
|
|
21
|
+
const { type = 'text', pattern, allowedPattern, autocomplete, value, readonly, disabled, min, max, step, maxlength, } = host, { onChange, onFocus, onInput, onRef } = useInput(host);
|
|
21
22
|
const onBeforeInput = useAllowedPattern(allowedPattern);
|
|
22
23
|
return render(html `
|
|
23
24
|
<input
|
|
25
|
+
${ref(onRef)}
|
|
24
26
|
style="--chars: ${value?.toString()?.length ?? 0}ch"
|
|
25
27
|
id="input"
|
|
26
28
|
part="input"
|
package/dist/cosmoz-textarea.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { html } from 'lit-html'; // eslint-disable-line object-curly-newline
|
|
2
2
|
import { live } from 'lit-html/directives/live.js';
|
|
3
|
+
import { ref } from 'lit-html/directives/ref.js';
|
|
3
4
|
import { ifDefined } from 'lit-html/directives/if-defined.js';
|
|
4
5
|
import { component } from 'haunted';
|
|
5
6
|
import { useInput } from './use-input';
|
|
@@ -7,10 +8,11 @@ import { useAutoHeight } from './use-auto-height';
|
|
|
7
8
|
import { render, attributes } from './render';
|
|
8
9
|
const observedAttributes = ['rows', ...attributes];
|
|
9
10
|
export const Textarea = (host) => {
|
|
10
|
-
const { autocomplete, value, placeholder, readonly, disabled, rows, cols, maxlength, } = host, { onChange, onFocus, onInput } = useInput(host);
|
|
11
|
+
const { autocomplete, value, placeholder, readonly, disabled, rows, cols, maxlength, } = host, { onChange, onFocus, onInput, onRef } = useInput(host);
|
|
11
12
|
useAutoHeight(host);
|
|
12
13
|
return render(html `
|
|
13
14
|
<textarea id="input" part="input"
|
|
15
|
+
${ref(onRef)}
|
|
14
16
|
autocomplete=${ifDefined(autocomplete)}
|
|
15
17
|
placeholder=${placeholder || ' '}
|
|
16
18
|
rows=${rows ?? 1} cols=${ifDefined(cols)}
|
package/dist/use-input.d.ts
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
|
+
type Input = HTMLInputElement | HTMLTextAreaElement;
|
|
1
2
|
export interface BaseInput extends HTMLElement {
|
|
2
3
|
value?: string | number;
|
|
3
4
|
maxRows?: number;
|
|
4
5
|
focused?: boolean;
|
|
5
6
|
disabled?: boolean;
|
|
7
|
+
onInputRef?: (el: Input) => void;
|
|
6
8
|
}
|
|
7
9
|
export declare const useInput: <T extends BaseInput>(host: T) => {
|
|
8
10
|
onChange: (e: Event) => boolean;
|
|
9
11
|
onFocus: (e: FocusEvent) => void;
|
|
10
12
|
onInput: (e: InputEvent) => void;
|
|
13
|
+
onRef: (el?: Element) => void;
|
|
11
14
|
};
|
|
15
|
+
export {};
|
package/dist/use-input.js
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
|
-
import { useCallback, useEffect } from 'haunted';
|
|
1
|
+
import { useCallback, useEffect, useRef } from 'haunted';
|
|
2
2
|
import { useImperativeApi } from '@neovici/cosmoz-utils/hooks/use-imperative-api';
|
|
3
3
|
import { notifyProperty } from '@neovici/cosmoz-utils/hooks/use-notify-property';
|
|
4
|
-
// TODO: use useRef instead of callback with querySelector
|
|
5
4
|
export const useInput = (host) => {
|
|
6
|
-
const
|
|
7
|
-
|
|
5
|
+
const { onInputRef } = host;
|
|
6
|
+
const inputRef = useRef(undefined);
|
|
7
|
+
const onRef = useCallback((el) => {
|
|
8
|
+
const ref = el;
|
|
9
|
+
inputRef.current = ref;
|
|
10
|
+
onInputRef?.(ref);
|
|
11
|
+
}, [onInputRef]);
|
|
12
|
+
const root = host.shadowRoot, onChange = useCallback((e) => host.dispatchEvent(new Event(e.type, { bubbles: e.bubbles })), []), onInput = useCallback((e) => notifyProperty(host, 'value', e.target.value), []), onFocus = useCallback((e) => notifyProperty(host, 'focused', e.type === 'focus'), []), focus = useCallback(() => inputRef.current?.focus(), []), validate = useCallback(() => {
|
|
13
|
+
const valid = inputRef.current?.checkValidity();
|
|
8
14
|
host.toggleAttribute('invalid', !valid);
|
|
9
15
|
return valid;
|
|
10
16
|
}, []);
|
|
@@ -29,5 +35,6 @@ export const useInput = (host) => {
|
|
|
29
35
|
onChange,
|
|
30
36
|
onFocus,
|
|
31
37
|
onInput,
|
|
38
|
+
onRef,
|
|
32
39
|
};
|
|
33
40
|
};
|