@neovici/cosmoz-autocomplete 7.0.0 → 7.2.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.
@@ -75,11 +75,12 @@ const autocomplete = (props) => {
75
75
  'text-property',
76
76
  'limit',
77
77
  'show-single',
78
- 'keep-query',
79
- 'keep-opened',
78
+ 'auto-close',
79
+ 'auto-clear',
80
80
  'hide-empty',
81
81
  'default-index',
82
82
  'item-height',
83
83
  'item-limit',
84
+ 'wrap'
84
85
  ];
85
86
  export { autocomplete, Autocomplete, observedAttributes };
@@ -7,6 +7,7 @@ interface ChipProps extends Props {
7
7
  slot?: string;
8
8
  className?: string;
9
9
  content: unknown;
10
+ hidden?: boolean;
10
11
  }
11
- export declare const chip: ({ content, onClear, disabled, className, slot, }: ChipProps) => import("lit-html").TemplateResult<1>;
12
+ export declare const chip: ({ content, onClear, disabled, hidden, className, slot, }: ChipProps) => import("lit-html").TemplateResult<1>;
12
13
  export {};
@@ -36,12 +36,13 @@ export const Chip = ({ onClear, disabled }) => html `
36
36
  </span>`)}
37
37
  `;
38
38
  customElements.define('cosmoz-autocomplete-chip', component(Chip, { observedAttributes: ['disabled'] }));
39
- export const chip = ({ content, onClear, disabled, className = 'chip', slot, }) => html `<cosmoz-autocomplete-chip
39
+ export const chip = ({ content, onClear, disabled, hidden, className = 'chip', slot, }) => html `<cosmoz-autocomplete-chip
40
40
  class=${ifDefined(className)}
41
41
  slot=${ifDefined(slot)}
42
42
  part="chip"
43
43
  exportparts="chip-text, chip-clear"
44
44
  ?disabled=${disabled}
45
+ ?hidden=${hidden}
45
46
  .onClear=${onClear}
46
47
  title=${ifDefined(typeof content === 'string' ? content : undefined)}
47
48
  >${content}</cosmoz-autocomplete-chip
@@ -12,5 +12,6 @@ export const selection = ({ value: values, onDeselect, textual, disabled, }) =>
12
12
  className: 'badge',
13
13
  disabled: true,
14
14
  slot: 'control',
15
+ hidden: true,
15
16
  }),
16
17
  ];
@@ -3,6 +3,7 @@ export default css `
3
3
  :host {
4
4
  display: block;
5
5
  position: relative;
6
+ min-width: 35px;
6
7
  }
7
8
 
8
9
  cosmoz-input::part(control) {
@@ -41,6 +42,13 @@ export default css `
41
42
  display: none;
42
43
  }
43
44
 
45
+ :host([wrap]) cosmoz-input::part(control) {
46
+ flex-wrap: wrap;
47
+ }
48
+ :host([wrap]) .chip {
49
+ max-width: 100%;
50
+ }
51
+
44
52
  slot {
45
53
  display: contents !important;
46
54
  }
@@ -5,8 +5,8 @@ type Source<I> = (opts: {
5
5
  interface Base<I> {
6
6
  value: I | I[];
7
7
  limit?: number;
8
- keepQuery?: boolean;
9
- keepOpened?: boolean;
8
+ autoClose?: boolean;
9
+ autoClear?: boolean;
10
10
  onText: (text: string) => void;
11
11
  onChange: (value: I[], done?: () => void) => void;
12
12
  onSelect: (value: I, meta: Meta<I>) => void;
@@ -26,7 +26,7 @@ export interface Props<I> extends Base<I> {
26
26
  disabled?: boolean;
27
27
  onFocus?: (focused?: boolean) => void;
28
28
  }
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>) => {
29
+ export declare const useAutocomplete: <I>({ value: _value, text, onChange: _onChange, onText: _onText, onSelect, limit, source, textProperty, textual: _textual, valueProperty, external, hideEmpty, autoClose, autoClear, ...thru }: Props<I>) => {
30
30
  active: boolean | undefined;
31
31
  query: string;
32
32
  textual: (i: I) => string;
@@ -1,4 +1,4 @@
1
- import { useCallback, useMemo } from 'haunted';
1
+ import { useCallback, useMemo, useEffect } from 'haunted';
2
2
  import { without, array } from '@neovici/cosmoz-utils/array';
3
3
  import { prop, strProp } from '@neovici/cosmoz-utils/object';
4
4
  import { useHost } from '@neovici/cosmoz-utils/hooks/use-host';
@@ -6,8 +6,8 @@ import { useMeta } from '@neovici/cosmoz-utils/hooks/use-meta';
6
6
  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
- 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) => {
9
+ export const useAutocomplete = ({ value: _value, text, onChange: _onChange, onText: _onText, onSelect, limit, source, textProperty, textual: _textual, valueProperty, external, hideEmpty, autoClose, autoClear, ...thru }) => {
10
+ const textual = useMemo(() => (_textual ?? strProp)(textProperty), [_textual, textProperty]), { active, focused, 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) => [
@@ -15,7 +15,7 @@ export const useAutocomplete = ({ value: _value, text, onChange: _onChange, onTe
15
15
  ...without(value, prop(valueProperty))(source),
16
16
  ]), [source$, value, valueProperty]);
17
17
  useKeys({
18
- active,
18
+ focused,
19
19
  empty,
20
20
  limit,
21
21
  value,
@@ -23,13 +23,17 @@ export const useAutocomplete = ({ value: _value, text, onChange: _onChange, onTe
23
23
  onChange,
24
24
  onText,
25
25
  });
26
+ useEffect(() => {
27
+ if (!focused)
28
+ onText('');
29
+ }, [focused]);
26
30
  const meta = useMeta({
27
31
  onText,
28
32
  onChange,
29
33
  value,
30
34
  limit,
31
- keepQuery,
32
- keepOpened,
35
+ autoClose,
36
+ autoClear,
33
37
  setClosed,
34
38
  onSelect,
35
39
  });
@@ -54,17 +58,12 @@ export const useAutocomplete = ({ value: _value, text, onChange: _onChange, onTe
54
58
  setClosed(false);
55
59
  }, [onText, text, setClosed]),
56
60
  onSelect: useCallback((newVal) => {
57
- const { onSelect } = meta;
58
- if (onSelect) {
59
- return onSelect(newVal, meta);
60
- }
61
- const { onChange, onText, limit, value: val, keepQuery, keepOpened, setClosed, } = meta;
62
- if (!keepQuery) {
61
+ meta.onSelect?.(newVal, meta);
62
+ const { onChange, onText, limit, value: val, autoClose, autoClear, setClosed, } = meta;
63
+ if (autoClear)
63
64
  onText('');
64
- }
65
- if (!keepOpened) {
65
+ if (autoClose)
66
66
  setClosed(true);
67
- }
68
67
  const value = array(val);
69
68
  onChange((value.includes(newVal)
70
69
  ? without(newVal)(value)
@@ -1,5 +1,5 @@
1
1
  export interface Props<I> {
2
- active?: boolean;
2
+ focused?: boolean;
3
3
  empty?: boolean;
4
4
  hideEmpty?: boolean;
5
5
  value: I | I[];
@@ -7,4 +7,4 @@ export interface Props<I> {
7
7
  onChange: (v: I[]) => void;
8
8
  onText: (s: string) => void;
9
9
  }
10
- export declare const useKeys: <I>({ active, empty, ...info }: Props<I>) => void;
10
+ export declare const useKeys: <I>({ focused, empty, ...info }: Props<I>) => void;
@@ -1,8 +1,8 @@
1
1
  import { useEffect } from 'haunted';
2
2
  import { array } from '@neovici/cosmoz-utils/array';
3
3
  import { useMeta } from '@neovici/cosmoz-utils/hooks/use-meta';
4
- export const useKeys = ({ active, empty, ...info }) => {
5
- const enabled = active && empty, meta = useMeta(info);
4
+ export const useKeys = ({ focused, empty, ...info }) => {
5
+ const enabled = focused && empty, meta = useMeta(info);
6
6
  useEffect(() => {
7
7
  if (!enabled) {
8
8
  return;
@@ -1,4 +1,6 @@
1
- export declare const useOverflow: <I>({ value, active, }: {
1
+ export declare const useOverflow: <I>({ value, active, wrap, limit, }: {
2
2
  value: I[];
3
3
  active?: boolean | undefined;
4
+ wrap?: boolean | undefined;
5
+ limit?: number | undefined;
4
6
  }) => void;
@@ -9,8 +9,6 @@ const overflow = (host) => {
9
9
  chip.hidden = false;
10
10
  }
11
11
  const input = host.shadowRoot.querySelector('cosmoz-input');
12
- if (input.matches('[data-one]'))
13
- return;
14
12
  const limit = input.shadowRoot
15
13
  ?.querySelector('.control')
16
14
  ?.getBoundingClientRect();
@@ -30,17 +28,21 @@ const overflow = (host) => {
30
28
  chips[i].hidden = true;
31
29
  }
32
30
  };
33
- export const useOverflow = ({ value, active, }) => {
31
+ export const useOverflow = ({ value, active, wrap, limit, }) => {
34
32
  const host = useHost();
33
+ // eslint-disable-next-line eqeqeq
34
+ const enabled = !(wrap || limit == 1);
35
35
  const doRaf = useMemo(() => raf(() => overflow(host)), []);
36
36
  const [width, setWidth] = useState(0);
37
37
  useLayoutEffect(() => {
38
+ if (!enabled)
39
+ return;
38
40
  const input = host.shadowRoot.querySelector('cosmoz-input');
39
41
  const observer = new ResizeObserver((e) => {
40
42
  setWidth(e[0].contentRect.width);
41
43
  });
42
44
  observer.observe(input);
43
45
  return () => observer.disconnect();
44
- }, []);
45
- useLayoutEffect(() => doRaf(), [width, active, value]);
46
+ }, [enabled]);
47
+ useLayoutEffect(() => (enabled ? doRaf() : undefined), [enabled, width, active, value]);
46
48
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neovici/cosmoz-autocomplete",
3
- "version": "7.0.0",
3
+ "version": "7.2.0",
4
4
  "description": "A autocomplete input web component",
5
5
  "keywords": [
6
6
  "lit-html",
@@ -68,8 +68,8 @@
68
68
  },
69
69
  "dependencies": {
70
70
  "@lit-labs/virtualizer": "^2.0.0",
71
- "@neovici/cosmoz-dropdown": "^3.0.0",
72
- "@neovici/cosmoz-input": "^3.0.0",
71
+ "@neovici/cosmoz-dropdown": "^3.4.0",
72
+ "@neovici/cosmoz-input": "^3.11.0",
73
73
  "@neovici/cosmoz-utils": "^5.0.0",
74
74
  "haunted": "^5.0.0",
75
75
  "lit-html": "^2.0.0"
@@ -77,12 +77,12 @@
77
77
  "devDependencies": {
78
78
  "@commitlint/cli": "^17.0.0",
79
79
  "@commitlint/config-conventional": "^17.0.0",
80
- "@neovici/cfg": "^1.13.0",
80
+ "@neovici/cfg": "^1.34.0",
81
81
  "@open-wc/testing": "^3.0.0",
82
82
  "@semantic-release/changelog": "^6.0.0",
83
83
  "@semantic-release/git": "^10.0.0",
84
84
  "@storybook/storybook-deployer": "^2.8.5",
85
- "@web/dev-server-storybook": "^0.6.0",
85
+ "@web/dev-server-storybook": "^0.7.0",
86
86
  "eslint-import-resolver-exports": "^1.0.0-beta.5",
87
87
  "husky": "^8.0.0",
88
88
  "semantic-release": "^21.0.0",