@neovici/cosmoz-input 5.0.1 → 5.0.3
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 +4 -0
- package/dist/use-input.js +3 -4
- package/package.json +1 -1
package/dist/cosmoz-input.js
CHANGED
|
@@ -3,6 +3,7 @@ import { live } from 'lit-html/directives/live.js';
|
|
|
3
3
|
import { ref } from 'lit-html/directives/ref.js';
|
|
4
4
|
import { ifDefined } from 'lit-html/directives/if-defined.js';
|
|
5
5
|
import { component } from '@pionjs/pion';
|
|
6
|
+
import { useImperativeApi } from '@neovici/cosmoz-utils/hooks/use-imperative-api';
|
|
6
7
|
import { useInput } from './use-input';
|
|
7
8
|
import { useAllowedPattern } from './use-allowed-pattern';
|
|
8
9
|
import { render, attributes } from './render';
|
|
@@ -21,6 +22,9 @@ const observedAttributes = [
|
|
|
21
22
|
export const Input = (host) => {
|
|
22
23
|
const { type = 'text', pattern, allowedPattern, autocomplete, value, readonly, disabled, min, max, step, maxlength, } = host, { onChange, onFocus, onInput, onRef } = useInput(host);
|
|
23
24
|
const onBeforeInput = useAllowedPattern(allowedPattern);
|
|
25
|
+
useImperativeApi({
|
|
26
|
+
focus: () => host.shadowRoot?.querySelector('#input')?.focus(),
|
|
27
|
+
}, []);
|
|
24
28
|
return render(html `
|
|
25
29
|
<input
|
|
26
30
|
${ref(onRef)}
|
package/dist/use-input.js
CHANGED
|
@@ -3,8 +3,7 @@ import { useImperativeApi } from '@neovici/cosmoz-utils/hooks/use-imperative-api
|
|
|
3
3
|
import { notifyProperty } from '@neovici/cosmoz-utils/hooks/use-notify-property';
|
|
4
4
|
export const useInput = (host) => {
|
|
5
5
|
const inputRef = useRef(undefined);
|
|
6
|
-
const onRef = useCallback((el) => (inputRef.current = el), [])
|
|
7
|
-
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(() => {
|
|
6
|
+
const onRef = useCallback((el) => (inputRef.current = el), []), 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(() => {
|
|
8
7
|
const valid = inputRef.current?.checkValidity();
|
|
9
8
|
host.toggleAttribute('invalid', !valid);
|
|
10
9
|
return valid;
|
|
@@ -23,8 +22,8 @@ export const useInput = (host) => {
|
|
|
23
22
|
focus(); // focus input
|
|
24
23
|
}
|
|
25
24
|
};
|
|
26
|
-
|
|
27
|
-
return () =>
|
|
25
|
+
host.addEventListener('mousedown', onMouseDown);
|
|
26
|
+
return () => host.removeEventListener('mousedown', onMouseDown);
|
|
28
27
|
}, [focus]);
|
|
29
28
|
return {
|
|
30
29
|
onChange,
|