@neovici/cosmoz-autocomplete 6.3.0 → 7.0.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.
@@ -11,6 +11,8 @@ export interface Props<I> extends Base<I> {
11
11
  itemHeight?: number;
12
12
  itemLimit?: number;
13
13
  }
14
- type AProps<I> = Omit<Props<I>, keyof RProps<I>> & RProps<I>;
14
+ type AProps<I> = Omit<Props<I>, keyof RProps<I>> & RProps<I> & {
15
+ onInputRef?: (el?: Element) => void;
16
+ };
15
17
  declare const autocomplete: <I>(props: AProps<I>) => import("lit-html").TemplateResult<1>, Autocomplete: <I>(props: Props<I>) => import("lit-html").TemplateResult<1>, observedAttributes: string[];
16
18
  export { autocomplete, Autocomplete, observedAttributes };
@@ -9,6 +9,7 @@ import { useAutocomplete } from './use-autocomplete';
9
9
  import { listbox } from '../listbox';
10
10
  import style from './styles.css';
11
11
  import { selection } from './selection';
12
+ import { useOverflow } from './use-overflow';
12
13
  const blank = () => nothing;
13
14
  const inputParts = ['input', 'label', 'line', 'error']
14
15
  .map((part) => `${part}: input-${part}`)
@@ -32,7 +33,7 @@ const autocomplete = (props) => {
32
33
  .label=${label}
33
34
  .placeholder=${isSingle ? undefined : placeholder}
34
35
  ?no-label-float=${noLabelFloat}
35
- ?always-float-label=${isSingle || alwaysFloatLabel}
36
+ ?always-float-label=${value?.length > 0 || alwaysFloatLabel}
36
37
  ?readonly=${isSingle}
37
38
  ?disabled=${disabled}
38
39
  ?invalid=${until(values$.then(() => invalid, () => true), invalid)}
@@ -44,6 +45,7 @@ const autocomplete = (props) => {
44
45
  @click=${onClick}
45
46
  autocomplete="off"
46
47
  exportparts=${inputParts}
48
+ ?data-one=${isOne}
47
49
  >
48
50
  <slot name="prefix" slot="prefix"></slot>
49
51
  <slot name="suffix" slot="suffix"></slot>
@@ -58,10 +60,14 @@ const autocomplete = (props) => {
58
60
  </cosmoz-input>
59
61
 
60
62
  ${suggestions}`;
61
- }, Autocomplete = (props) => autocomplete({
62
- ...props,
63
- ...useAutocomplete(props),
64
- }), observedAttributes = [
63
+ }, Autocomplete = (props) => {
64
+ const thru = {
65
+ ...props,
66
+ ...useAutocomplete(props),
67
+ };
68
+ useOverflow(thru);
69
+ return autocomplete(thru);
70
+ }, observedAttributes = [
65
71
  'disabled',
66
72
  'invalid',
67
73
  'no-label-float',
@@ -0,0 +1,2 @@
1
+ declare const _default: string;
2
+ export default _default;
@@ -0,0 +1,46 @@
1
+ import { tagged as css } from '@neovici/cosmoz-utils';
2
+ export default css `
3
+ :host {
4
+ border-radius: var(--cosmoz-autocomplete-chip-border-radius, 500px);
5
+ background: var(--cosmoz-autocomplete-chip-bg-color, #cbcfdb);
6
+ margin: 0px 0 2px 0;
7
+ display: flex;
8
+ flex-direction: row;
9
+ align-items: center;
10
+ flex: 0.0001 1 fit-content;
11
+ max-width: 18ch;
12
+ min-width: 40px;
13
+ padding: 0 4px 0 8px;
14
+ gap: 4px;
15
+ }
16
+ .content {
17
+ color: var(--cosmoz-autocomplete-chip-color, #424242);
18
+ font-size: var(--cosmoz-autocomplete-chip-text-font-size, 12px);
19
+ line-height: var(--cosmoz-autocomplete-chip-text-line-height, 22px);
20
+ overflow: hidden;
21
+ text-overflow: ellipsis;
22
+ white-space: nowrap;
23
+ flex: auto;
24
+ min-width: 16px;
25
+ }
26
+ .clear {
27
+ background-color: var(--cosmoz-autocomplete-chip-clear-bg-color, #81899b);
28
+ border-radius: 50%;
29
+ cursor: pointer;
30
+ width: 16px;
31
+ height: 16px;
32
+ stroke: var(
33
+ --cosmoz-autocomplete-chip-clear-stroke,
34
+ var(--cosmoz-autocomplete-chip-bg-color, #cbcfdb)
35
+ );
36
+ display: block;
37
+ flex: none;
38
+ }
39
+ .clear:hover {
40
+ filter: brightness(90%);
41
+ }
42
+ .clear svg {
43
+ display: block;
44
+ transform: translate(3.5px, 3.5px);
45
+ }
46
+ `;
@@ -0,0 +1,12 @@
1
+ interface Props {
2
+ onClear?: () => void;
3
+ disabled?: boolean;
4
+ }
5
+ export declare const Chip: ({ onClear, disabled }: Props) => import("lit-html").TemplateResult<1>;
6
+ interface ChipProps extends Props {
7
+ slot?: string;
8
+ className?: string;
9
+ content: unknown;
10
+ }
11
+ export declare const chip: ({ content, onClear, disabled, className, slot, }: ChipProps) => import("lit-html").TemplateResult<1>;
12
+ export {};
@@ -0,0 +1,48 @@
1
+ import { html, component } from 'haunted';
2
+ import { when } from 'lit-html/directives/when.js';
3
+ import { ifDefined } from 'lit-html/directives/if-defined.js';
4
+ import styles from './chip.css';
5
+ const clear = html `
6
+ <svg
7
+ width="9"
8
+ height="8"
9
+ viewBox="0 0 9 8"
10
+ fill="none"
11
+ xmlns="http://www.w3.org/2000/svg"
12
+ >
13
+ <line
14
+ x1="7.53033"
15
+ y1="0.994808"
16
+ x2="1.16637"
17
+ y2="7.35877"
18
+ stroke-width="1.5"
19
+ />
20
+ <line
21
+ x1="7.46967"
22
+ y1="7.35882"
23
+ x2="1.10571"
24
+ y2="0.99486"
25
+ stroke-width="1.5"
26
+ />
27
+ </svg>
28
+ `;
29
+ export const Chip = ({ onClear, disabled }) => html `
30
+ <style>
31
+ ${styles}
32
+ </style>
33
+ <div class="content" part="content chip-text"><slot></slot></div>
34
+ ${when(!disabled, () => html ` <span class="clear" part="clear chip-clear" @click=${onClear}>
35
+ ${clear}
36
+ </span>`)}
37
+ `;
38
+ customElements.define('cosmoz-autocomplete-chip', component(Chip, { observedAttributes: ['disabled'] }));
39
+ export const chip = ({ content, onClear, disabled, className = 'chip', slot, }) => html `<cosmoz-autocomplete-chip
40
+ class=${ifDefined(className)}
41
+ slot=${ifDefined(slot)}
42
+ part="chip"
43
+ exportparts="chip-text, chip-clear"
44
+ ?disabled=${disabled}
45
+ .onClear=${onClear}
46
+ title=${ifDefined(typeof content === 'string' ? content : undefined)}
47
+ >${content}</cosmoz-autocomplete-chip
48
+ >`;
@@ -7,5 +7,5 @@ interface Props<I> {
7
7
  textual: (i: I) => string;
8
8
  disabled?: boolean;
9
9
  }
10
- export declare const selection: <I>({ value, isOne, onDeselect, textual, disabled, }: Props<I>) => import("lit-html").TemplateResult<1> | undefined;
10
+ export declare const selection: <I>({ value: values, onDeselect, textual, disabled, }: Props<I>) => import("lit-html").TemplateResult<1>[];
11
11
  export {};
@@ -1,58 +1,16 @@
1
- import { html } from 'lit-html'; // eslint-disable-line object-curly-newline
2
- import { when } from 'lit-html/directives/when.js';
3
- const clear = html `
4
- <svg
5
- width="9"
6
- height="8"
7
- viewBox="0 0 9 8"
8
- fill="none"
9
- xmlns="http://www.w3.org/2000/svg"
10
- >
11
- <line
12
- x1="7.53033"
13
- y1="0.994808"
14
- x2="1.16637"
15
- y2="7.35877"
16
- stroke-width="1.5"
17
- />
18
- <line
19
- x1="7.46967"
20
- y1="7.35882"
21
- x2="1.10571"
22
- y2="0.99486"
23
- stroke-width="1.5"
24
- />
25
- </svg>
26
- `;
27
- const chip = ({ text, onClear, disabled, isOne, }) => html `
28
- <div class="chip" part="chip" slot="suffix" title=${text} ?data-one=${isOne}>
29
- <span class="chip-text" part="chip-text">${text}</span>
30
- ${when(!disabled, () => html ` <span class="chip-clear" part="chip-clear" @click=${onClear}>
31
- ${clear}
32
- </span>`)}
33
- </div>
34
- `;
35
- const badge = ({ value, onDeselect, }) => {
36
- const len = value?.length;
37
- return when(len > 0, () => html `<div class="badge" slot="suffix" part="badge">
38
- ${len}
39
- <span
40
- class="badge-clear"
41
- part="chip-clear"
42
- @click=${() => onDeselect(value)}
43
- >${clear}</span
44
- >
45
- </div>`);
46
- };
47
- export const selection = ({ value, isOne, onDeselect, textual, disabled, }) => {
48
- if (isOne || value.length === 1) {
49
- const val = value[0];
50
- return when(val, () => chip({
51
- text: textual(val),
52
- onClear: () => onDeselect(val),
53
- disabled,
54
- isOne,
55
- }));
56
- }
57
- return badge({ value, onDeselect });
58
- };
1
+ import { html } from 'lit-html';
2
+ import { chip } from './chip';
3
+ export const selection = ({ value: values, onDeselect, textual, disabled, }) => [
4
+ ...values.map((value) => chip({
5
+ content: textual(value),
6
+ onClear: () => onDeselect(value),
7
+ disabled,
8
+ slot: 'control',
9
+ })),
10
+ chip({
11
+ content: html `<span></span>`,
12
+ className: 'badge',
13
+ disabled: true,
14
+ slot: 'control',
15
+ }),
16
+ ];
@@ -4,84 +4,41 @@ export default css `
4
4
  display: block;
5
5
  position: relative;
6
6
  }
7
- .chip {
8
- border-radius: var(--cosmoz-autocomplete-chip-border-radius, 500px);
9
- background: var(--cosmoz-autocomplete-chip-bg-color, #cbcfdb);
10
- margin: 1px 4px 1px 0;
11
- white-space: nowrap;
12
- overflow: hidden;
7
+
8
+ cosmoz-input::part(control) {
13
9
  display: flex;
14
- flex-direction: row;
15
- align-items: center;
16
- flex: 0.00001 1 min-content;
17
- max-width: 285px;
18
- max-width: 28ch;
10
+ gap: 4px;
11
+ min-width: 35px;
19
12
  }
20
- .chip[data-one] {
21
- position: absolute;
22
- left: 0;
23
- top: 1px;
24
- bottom: 1px;
25
- right: 0;
26
- margin: 0;
27
- z-index: 2;
28
- pointer-events: auto;
29
- max-width: initial;
13
+ cosmoz-input::part(input) {
14
+ flex: 1 24px;
15
+ min-width: 0;
30
16
  }
31
- .chip-text {
32
- color: var(--cosmoz-autocomplete-chip-color, #424242);
33
- margin: var(--cosmoz-autocomplete-chip-text-margin, 0 10px);
34
- font-size: var(--cosmoz-autocomplete-chip-text-font-size, 12px);
35
- line-height: var(--cosmoz-autocomplete-chip-text-line-height, 22px);
36
- overflow: hidden;
37
- text-overflow: ellipsis;
38
- flex: 1;
17
+ cosmoz-input:not([data-one])::part(input):focus {
18
+ flex: 4 0.00001 50px;
19
+ min-width: 20px;
39
20
  }
40
- .chip-clear {
41
- margin: 2px 4px 2px -6px;
42
- background-color: var(--cosmoz-autocomplete-chip-clear-bg-color, #81899b);
43
- border-radius: 500px;
44
- cursor: pointer;
45
- width: 16px;
46
- height: 16px;
47
- stroke: var(
48
- --cosmoz-autocomplete-chip-clear-stroke,
49
- var(--cosmoz-autocomplete-chip-bg-color, #cbcfdb)
50
- );
51
- display: block;
21
+ .badge {
22
+ min-width: initial;
23
+ flex: none;
24
+ text-align: center;
25
+ padding: 0 4px;
52
26
  }
53
27
 
54
- .chip-clear svg,
55
- .badge-clear svg {
56
- display: block;
57
- transform: translate(3.5px, 3.5px);
28
+ [data-one]::part(input) {
29
+ flex: 0;
30
+ }
31
+ [data-one] .chip {
32
+ max-width: initial;
33
+ flex: 1;
58
34
  }
59
35
 
60
- .badge {
61
- background: var(--cosmoz-autocomplete-chip-bg-color, #cbcfdb);
62
- color: var(--cosmoz-autocomplete-chip-color, #424242);
63
- font-weight: 500;
64
- font-size: 12px;
65
- line-height: 22px;
66
- padding: 0 4px 0 6px;
67
- gap: 4px;
68
- border-radius: 90px;
69
- text-align: center;
70
- box-sizing: border-box;
71
- /* fallback for unsupported min-content FF, Safari */
72
- flex: 0.01 2 30px;
73
- flex: 0 2 min-content;
74
- display: flex;
75
- align-items: center;
36
+ [data-one] .badge {
37
+ display: none;
76
38
  }
77
- .badge-clear {
78
- background-color: var(--cosmoz-autocomplete-chip-clear-bg-color, #81899b);
79
- border-radius: 500px;
80
- cursor: pointer;
81
- width: 16px;
82
- height: 16px;
83
- stroke: var(--cosmoz-autocomplete-chip-bg-color, #cbcfdb);
84
- display: block;
39
+
40
+ [hidden] {
41
+ display: none;
85
42
  }
86
43
 
87
44
  slot {
@@ -19,7 +19,7 @@ export interface Props<I> extends Base<I> {
19
19
  text: string;
20
20
  source: I[] | Source<I>;
21
21
  textProperty?: string;
22
- textual?: (i: I) => string;
22
+ textual?: (prop?: string) => (i: I) => string;
23
23
  valueProperty?: string;
24
24
  external?: boolean;
25
25
  hideEmpty?: boolean;
@@ -27,6 +27,7 @@ export interface Props<I> extends Base<I> {
27
27
  onFocus?: (focused?: boolean) => void;
28
28
  }
29
29
  export declare const useAutocomplete: <I>({ value: _value, text, onChange: _onChange, onText: _onText, onSelect, limit, source, textProperty, textual: _textual, valueProperty, external, hideEmpty, keepQuery, keepOpened, ...thru }: Props<I>) => {
30
+ active: boolean | undefined;
30
31
  query: string;
31
32
  textual: (i: I) => string;
32
33
  value: I[];
@@ -7,7 +7,7 @@ import { useFocus } from '@neovici/cosmoz-dropdown/use-focus';
7
7
  import { useKeys } from './use-keys';
8
8
  import { search, notify, useNotify, EMPTY$ } from './util';
9
9
  export const useAutocomplete = ({ value: _value, text, onChange: _onChange, onText: _onText, onSelect, limit, source, textProperty, textual: _textual, valueProperty, external, hideEmpty, keepQuery, keepOpened, ...thru }) => {
10
- const textual = useMemo(() => _textual || strProp(textProperty), [_textual, textProperty]), { active, onFocus, setClosed } = useFocus(thru), empty = !text, query = useMemo(() => text?.trim().toLowerCase(), [text]), host = useHost(), onText = useNotify(host, _onText, 'text'), onChange = useCallback((val) => {
10
+ const textual = useMemo(() => (_textual ?? strProp)(textProperty), [_textual, textProperty]), { active, onFocus, setClosed } = useFocus(thru), empty = !text, query = useMemo(() => text?.trim().toLowerCase(), [text]), host = useHost(), onText = useNotify(host, _onText, 'text'), onChange = useCallback((val) => {
11
11
  _onChange?.(val, () => setClosed(true));
12
12
  notify(host, 'value', val);
13
13
  }, [_onChange]), source$ = useMemo(() => Promise.resolve(typeof source === 'function' ? source({ query, active }) : source), [source, active, query]), value = useMemo(() => array(_value), [_value]), values$ = useMemo(() => source$.then((source) => [
@@ -34,6 +34,7 @@ export const useAutocomplete = ({ value: _value, text, onChange: _onChange, onTe
34
34
  onSelect,
35
35
  });
36
36
  return {
37
+ active,
37
38
  query,
38
39
  textual,
39
40
  value,
@@ -0,0 +1,4 @@
1
+ export declare const useOverflow: <I>({ value, active, }: {
2
+ value: I[];
3
+ active?: boolean | undefined;
4
+ }) => void;
@@ -0,0 +1,46 @@
1
+ import { useLayoutEffect, useState, useMemo } from 'haunted';
2
+ import { useHost } from '@neovici/cosmoz-utils/hooks/use-host';
3
+ import { raf } from './util';
4
+ const overflow = (host) => {
5
+ const chips = host.shadowRoot.querySelectorAll('.chip');
6
+ const badge = host.shadowRoot.querySelector('.badge');
7
+ badge.hidden = true;
8
+ for (const chip of chips) {
9
+ chip.hidden = false;
10
+ }
11
+ const input = host.shadowRoot.querySelector('cosmoz-input');
12
+ if (input.matches('[data-one]'))
13
+ return;
14
+ const limit = input.shadowRoot
15
+ ?.querySelector('.control')
16
+ ?.getBoundingClientRect();
17
+ let i;
18
+ for (i = 0; i < chips.length; i++) {
19
+ const chip = chips[i];
20
+ const bounds = chip.getBoundingClientRect();
21
+ const isIn = bounds.x + bounds.width <= limit.x + limit.width - 24;
22
+ if (!isIn) {
23
+ break;
24
+ }
25
+ }
26
+ const overflown = chips.length - i;
27
+ badge.querySelector('span').textContent = '+' + overflown.toString();
28
+ badge.hidden = overflown < 1;
29
+ for (; i < chips.length; i++) {
30
+ chips[i].hidden = true;
31
+ }
32
+ };
33
+ export const useOverflow = ({ value, active, }) => {
34
+ const host = useHost();
35
+ const doRaf = useMemo(() => raf(() => overflow(host)), []);
36
+ const [width, setWidth] = useState(0);
37
+ useLayoutEffect(() => {
38
+ const input = host.shadowRoot.querySelector('cosmoz-input');
39
+ const observer = new ResizeObserver((e) => {
40
+ setWidth(e[0].contentRect.width);
41
+ });
42
+ observer.observe(input);
43
+ return () => observer.disconnect();
44
+ }, []);
45
+ useLayoutEffect(() => doRaf(), [width, active, value]);
46
+ };
@@ -2,3 +2,7 @@ export declare const search: <I>(source: I[], query: string, textual: (i: I) =>
2
2
  export declare const notify: <T>(host: EventTarget, name: string, detail: T) => boolean;
3
3
  export declare const useNotify: <V>(host: EventTarget, fn: ((v: V) => void) | undefined, name: string) => (val: V) => void;
4
4
  export declare const EMPTY: never[], EMPTY$: Promise<never[]>;
5
+ type Arr = unknown[];
6
+ type ArrFn<T extends Arr> = (...args: T) => void;
7
+ export declare const raf: <A extends Arr, F extends ArrFn<A> = ArrFn<A>>(fn: F) => (...args: A) => () => void;
8
+ export {};
@@ -23,3 +23,16 @@ export const useNotify = (host, fn, name) => useCallback((val) => {
23
23
  notify(host, name, val);
24
24
  }, [fn]);
25
25
  export const EMPTY = [], EMPTY$ = Promise.resolve(EMPTY);
26
+ export const raf = (fn) => (...args) => {
27
+ let id;
28
+ const cleanup = () => {
29
+ if (id)
30
+ cancelAnimationFrame(id);
31
+ };
32
+ cleanup();
33
+ id = requestAnimationFrame(() => {
34
+ id = undefined;
35
+ fn(...args);
36
+ });
37
+ return cleanup;
38
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neovici/cosmoz-autocomplete",
3
- "version": "6.3.0",
3
+ "version": "7.0.0",
4
4
  "description": "A autocomplete input web component",
5
5
  "keywords": [
6
6
  "lit-html",