@neovici/cosmoz-input 3.8.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.
@@ -1,10 +1,12 @@
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';
6
7
  import { useAllowedPattern } from './use-allowed-pattern';
7
8
  import { render, attributes } from './render';
9
+ import { getPlaceholder } from './util';
8
10
  const observedAttributes = [
9
11
  'type',
10
12
  'pattern',
@@ -16,17 +18,18 @@ const observedAttributes = [
16
18
  ...attributes,
17
19
  ];
18
20
  export const Input = (host) => {
19
- const { type = 'text', pattern, allowedPattern, autocomplete, value, placeholder, 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);
20
22
  const onBeforeInput = useAllowedPattern(allowedPattern);
21
23
  return render(html `
22
24
  <input
25
+ ${ref(onRef)}
23
26
  style="--chars: ${value?.toString()?.length ?? 0}ch"
24
27
  id="input"
25
28
  part="input"
26
29
  type=${type}
27
30
  pattern=${ifDefined(pattern)}
28
31
  autocomplete=${ifDefined(autocomplete)}
29
- placeholder=${placeholder || ' '}
32
+ placeholder=${getPlaceholder(host)}
30
33
  ?readonly=${readonly}
31
34
  ?aria-disabled=${disabled}
32
35
  ?disabled=${disabled}
@@ -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/styles.js CHANGED
@@ -102,8 +102,10 @@ export const styles = css `
102
102
  transform: translateY(calc(var(--label-scale) * -100%))
103
103
  scale(var(--label-scale));
104
104
  }
105
- #input:not(:placeholder-shown):focus + label {
105
+ :host(:not(always-float-label):focus-within) #input::placeholder,
106
+ :host(:focus-within) label {
106
107
  color: var(--focused-color);
108
+ opacity: 1;
107
109
  }
108
110
 
109
111
  .line {
@@ -137,7 +139,7 @@ export const styles = css `
137
139
  }
138
140
 
139
141
  :host([no-label-float]) .float,
140
- :host([no-label-float]) #input:not(:placeholder-shown) + label {
142
+ :host([no-label-float]) label {
141
143
  display: none;
142
144
  }
143
145
 
@@ -190,7 +192,7 @@ export const styles = css `
190
192
  width: var(--width);
191
193
  min-width: calc(2ch + 0.25em);
192
194
  }
193
- :host([type="color"]) .line {
195
+ :host([type='color']) .line {
194
196
  display: none;
195
197
  }
196
198
  `;
@@ -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 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(() => root.querySelector('#input')?.focus(), []), validate = useCallback(() => {
7
- const valid = root.querySelector('#input')?.checkValidity();
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
  };
package/dist/util.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ interface Props {
2
+ label?: string;
3
+ noLabelFloat?: boolean;
4
+ placeholder?: string;
5
+ }
6
+ export declare const getPlaceholder: ({ placeholder, noLabelFloat, label }: Props) => string;
7
+ export {};
package/dist/util.js ADDED
@@ -0,0 +1,3 @@
1
+ export const getPlaceholder = ({ placeholder, noLabelFloat, label }) => {
2
+ return (noLabelFloat ? label : undefined) || placeholder || ' ';
3
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neovici/cosmoz-input",
3
- "version": "3.8.0",
3
+ "version": "3.10.0",
4
4
  "description": "A input web component",
5
5
  "keywords": [
6
6
  "lit-html",