@ioca/react 1.5.28 → 1.5.30

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.
@@ -0,0 +1,192 @@
1
+ import { jsx, jsxs } from 'react/jsx-runtime';
2
+ import { AddRound } from '@ricons/material';
3
+ import classNames from 'classnames';
4
+ import { useState, useRef, useEffect, useMemo, useCallback } from 'react';
5
+ import CreateTag from './create.js';
6
+ import TagItem from './item.js';
7
+
8
+ function Pill(props) {
9
+ const { value = [], tagProps, max, icon = jsx(AddRound, {}), className, label, labelInline, readonly, editable, onChange, onUpdate, validator, format, renderItem, hideCreate, ...restProps } = props;
10
+ const [editingIndex, setEditingIndex] = useState(null);
11
+ const [loadingSet, setLoadingSet] = useState(new Set());
12
+ const instRef = useRef({
13
+ props,
14
+ editingIndex,
15
+ loadingSet,
16
+ setEditingIndex,
17
+ setLoadingSet,
18
+ });
19
+ instRef.current.props = props;
20
+ instRef.current.editingIndex = editingIndex;
21
+ instRef.current.loadingSet = loadingSet;
22
+ instRef.current.setEditingIndex = setEditingIndex;
23
+ instRef.current.setLoadingSet = setLoadingSet;
24
+ useEffect(() => {
25
+ if (editingIndex !== null) {
26
+ const el = document.querySelector(".i-pill-editing");
27
+ el?.focus();
28
+ }
29
+ }, [editingIndex]);
30
+ const cleanTagProps = useMemo(() => {
31
+ if (!tagProps)
32
+ return {};
33
+ const { onClose, dot, dotClass, ...rest } = tagProps;
34
+ return rest;
35
+ }, [tagProps]);
36
+ const handleClose = useCallback((index) => {
37
+ const inst = instRef.current;
38
+ if (inst.props.readonly)
39
+ return;
40
+ const hasAsync = !!inst.props.onUpdate;
41
+ if (hasAsync)
42
+ inst.setLoadingSet((prev) => new Set(prev).add(index));
43
+ setTimeout(async () => {
44
+ try {
45
+ const { props } = instRef.current;
46
+ const values = props.value ?? [];
47
+ const item = values[index];
48
+ if (item === undefined)
49
+ return;
50
+ const result = props.onUpdate?.(undefined, item, "delete");
51
+ if (result instanceof Promise) {
52
+ const ok = await result;
53
+ if (ok === false)
54
+ return;
55
+ }
56
+ const next = [...values];
57
+ next.splice(index, 1);
58
+ props.onChange?.(next);
59
+ }
60
+ finally {
61
+ if (hasAsync) {
62
+ instRef.current.setLoadingSet((prev) => {
63
+ const s = new Set(prev);
64
+ s.delete(index);
65
+ return s;
66
+ });
67
+ }
68
+ }
69
+ }, 0);
70
+ }, []);
71
+ const handleItemClick = useCallback((e, index) => {
72
+ if (e.target.closest(".i-helpericon"))
73
+ return;
74
+ const inst = instRef.current;
75
+ if (inst.props.readonly)
76
+ return;
77
+ if (index === -1 && inst.props.max !== undefined && (inst.props.value?.length ?? 0) >= inst.props.max)
78
+ return;
79
+ inst.setEditingIndex(index);
80
+ }, []);
81
+ const commitEdit = useCallback((index, text) => {
82
+ const inst = instRef.current;
83
+ const formatted = inst.props.format ? inst.props.format(text) : text;
84
+ const hasAsync = !!(inst.props.validator || inst.props.onUpdate);
85
+ if (hasAsync)
86
+ inst.setLoadingSet((prev) => new Set(prev).add(index));
87
+ setTimeout(async () => {
88
+ try {
89
+ const { props } = instRef.current;
90
+ if (props.validator) {
91
+ const valid = await Promise.resolve(props.validator(formatted));
92
+ if (!valid)
93
+ return;
94
+ }
95
+ const values = props.value ?? [];
96
+ if (index === -1) {
97
+ if (values.includes(formatted))
98
+ return;
99
+ const result = props.onUpdate?.(formatted, undefined, "create");
100
+ if (result instanceof Promise) {
101
+ const ok = await result;
102
+ if (ok === false)
103
+ return;
104
+ }
105
+ props.onChange?.([...values, formatted]);
106
+ }
107
+ else {
108
+ const oldValue = values[index];
109
+ if (oldValue === formatted)
110
+ return;
111
+ const result = props.onUpdate?.(formatted, oldValue, "update");
112
+ if (result instanceof Promise) {
113
+ const ok = await result;
114
+ if (ok === false)
115
+ return;
116
+ }
117
+ const next = [...values];
118
+ next[index] = formatted;
119
+ props.onChange?.(next);
120
+ }
121
+ }
122
+ finally {
123
+ if (hasAsync) {
124
+ instRef.current.setLoadingSet((prev) => {
125
+ const s = new Set(prev);
126
+ s.delete(index);
127
+ return s;
128
+ });
129
+ }
130
+ instRef.current.setEditingIndex(null);
131
+ }
132
+ }, 0);
133
+ }, []);
134
+ const handleBlur = useCallback((index) => {
135
+ const inst = instRef.current;
136
+ if (inst.loadingSet.has(index))
137
+ return;
138
+ const el = document.querySelector(".i-pill-editing");
139
+ const text = el?.textContent?.trim();
140
+ if (!text) {
141
+ if (index !== -1) {
142
+ handleClose(index);
143
+ }
144
+ else {
145
+ inst.setEditingIndex(null);
146
+ }
147
+ return;
148
+ }
149
+ commitEdit(index, text);
150
+ }, []);
151
+ const handleKeyDown = useCallback((e, index) => {
152
+ const inst = instRef.current;
153
+ if (inst.loadingSet.has(index))
154
+ return;
155
+ if (e.key === "Enter") {
156
+ e.preventDefault();
157
+ const text = e.currentTarget.textContent?.trim();
158
+ if (!text) {
159
+ if (index !== -1) {
160
+ handleClose(index);
161
+ }
162
+ else {
163
+ inst.setEditingIndex(null);
164
+ }
165
+ return;
166
+ }
167
+ commitEdit(index, text);
168
+ }
169
+ else if (e.key === "Escape") {
170
+ e.preventDefault();
171
+ if (index !== -1) {
172
+ const original = inst.props.value?.[index];
173
+ if (original !== undefined) {
174
+ e.currentTarget.textContent = original;
175
+ }
176
+ }
177
+ inst.setEditingIndex(null);
178
+ }
179
+ }, []);
180
+ const handleStartCreate = useCallback(() => {
181
+ const inst = instRef.current;
182
+ if (inst.props.readonly)
183
+ return;
184
+ if (inst.props.max !== undefined && (inst.props.value?.length ?? 0) >= inst.props.max)
185
+ return;
186
+ inst.setEditingIndex(-1);
187
+ }, []);
188
+ const canCreate = !hideCreate && !readonly && (max === undefined || value.length < max);
189
+ return (jsxs("div", { className: classNames("i-pills i-input-label", { "i-input-inline": labelInline }, className), ...restProps, children: [label && jsx("span", { className: "i-input-label-text", children: label }), jsxs("div", { className: "i-pill-list", children: [value.map((item, i) => (jsx(TagItem, { item: item, index: i, isEditing: editingIndex === i, isLoading: loadingSet.has(i), tagProps: tagProps, editable: editable, readonly: readonly, renderItem: renderItem, onClose: handleClose, onClick: handleItemClick, onBlur: handleBlur, onKeyDown: handleKeyDown }, i))), canCreate && jsx(CreateTag, { isEditing: editingIndex === -1, isLoading: loadingSet.has(-1), createTagProps: cleanTagProps, tagProps: tagProps, onBlur: handleBlur, onKeyDown: handleKeyDown, onStartCreate: handleStartCreate })] })] }));
190
+ }
191
+
192
+ export { Pill as default };
@@ -7,19 +7,17 @@ import Tag from '../tag/tag.js';
7
7
  import Empty from '../utils/empty/index.js';
8
8
 
9
9
  const Options = (props) => {
10
- const { value: val, options, filter, filterPlaceholder, multiple, empty = jsx(Empty, {}), onSelect, onFilter, } = props;
10
+ const { value: val, options, filter, filterPlaceholder, multiple, empty = jsx(Empty, {}), onSelect, onFilter } = props;
11
11
  return (jsxs("div", { className: classNames("i-select-options", {
12
12
  "i-select-options-multiple": multiple,
13
- }), children: [filter && multiple && (jsxs("div", { className: 'i-select-filter', children: [jsx(Icon, { icon: jsx(SearchRound, {}), className: 'color-8 ml-8 my-auto', size: '1.2em' }), jsx("input", { type: 'text', className: 'i-input', placeholder: filterPlaceholder, onChange: onFilter })] })), options.length === 0 && empty, options.map((option, i) => {
13
+ }), children: [filter && multiple && (jsxs("div", { className: "i-select-filter", children: [jsx(Icon, { icon: jsx(SearchRound, {}), className: "color-8 ml-8 my-auto", size: "1.2em" }), jsx("input", { type: "text", className: "i-input", placeholder: filterPlaceholder, onChange: onFilter })] })), options.length === 0 && empty, options.map((option, i) => {
14
14
  const { label, value, disabled } = option;
15
- const isActive = multiple
16
- ? val?.includes(value)
17
- : val === value;
18
- return (jsxs(List.Item, { active: isActive, type: 'option', onClick: () => onSelect?.(value, option), disabled: disabled, children: [multiple && (jsx(Icon, { icon: jsx(CheckRound, {}), className: 'i-select-option-check', size: '1em' })), label] }, value || i));
15
+ const isActive = multiple ? val?.includes(value) : val === value;
16
+ return (jsxs(List.Item, { active: isActive, type: "option", onClick: () => onSelect?.(value, option), disabled: disabled, children: [multiple && jsx(Icon, { icon: jsx(CheckRound, {}), className: "i-select-option-check", size: "1em" }), label] }, value || i));
19
17
  })] }));
20
18
  };
21
19
  const activeOptions = (options = [], value = [], max = 3) => {
22
- const total = options.flatMap((opt) => value.includes(opt.value) ? [opt] : []);
20
+ const total = options.flatMap((opt) => (value.includes(opt.value) ? [opt] : []));
23
21
  if (max >= total.length)
24
22
  return total;
25
23
  const rest = total.length - max;
@@ -34,7 +32,7 @@ const displayValue = (config) => {
34
32
  if (typeof opt === "number")
35
33
  return jsxs(Tag, { children: ["+", opt] }, i);
36
34
  const { label, value } = opt;
37
- return (jsx(Tag, { hoverShowClose: true, onClose: (e) => {
35
+ return (jsx(Tag, { onClose: (e) => {
38
36
  e?.stopPropagation();
39
37
  onSelect?.(value, opt);
40
38
  }, children: label }, value));
@@ -9,7 +9,7 @@ import Helpericon from '../utils/helpericon/helpericon.js';
9
9
  import { displayValue, Options } from './options.js';
10
10
 
11
11
  const Select = (props) => {
12
- const { ref, type = "text", name, label, value = "", placeholder, required, options = [], multiple, prepend, append, labelInline, style, className, message, status = "normal", hideClear, hideArrow, maxDisplay, border, filter, tip, filterPlaceholder = "...", popupProps, onSelect, onChange, ...restProps } = props;
12
+ const { ref, type = "text", name, label, value = "", placeholder, required, options = [], multiple, prepend, append, labelInline, style, className, message, status = "normal", hideClear, hideArrow, maxDisplay, border = true, filter, tip, filterPlaceholder = "...", popupProps, onSelect, onChange, ...restProps } = props;
13
13
  const [filterValue, setFilterValue] = useState("");
14
14
  const [selectedValue, setSelectedValue] = useState(value);
15
15
  const [active, setActive] = useState(false);
@@ -24,9 +24,7 @@ const Select = (props) => {
24
24
  if (!fv || !filter)
25
25
  return formattedOptions;
26
26
  const lowerFv = fv.toLowerCase();
27
- const filterFn = typeof filter === "function"
28
- ? filter
29
- : (opt) => opt._value.includes(lowerFv) || opt._label.includes(lowerFv);
27
+ const filterFn = typeof filter === "function" ? filter : (opt) => opt._value.includes(lowerFv) || opt._label.includes(lowerFv);
30
28
  return formattedOptions.filter(filterFn);
31
29
  }, [formattedOptions, filter, filterValue]);
32
30
  const changeValue = (v) => {
@@ -75,9 +73,7 @@ const Select = (props) => {
75
73
  useEffect(() => {
76
74
  setSelectedValue(value);
77
75
  }, [value]);
78
- const hasValue = multiple
79
- ? selectedValue.length > 0
80
- : !!selectedValue;
76
+ const hasValue = multiple ? selectedValue.length > 0 : !!selectedValue;
81
77
  const clearable = !hideClear && active && hasValue;
82
78
  const text = message ?? tip;
83
79
  return (jsxs("label", { className: classNames("i-input-label", className, {
@@ -94,7 +90,7 @@ const Select = (props) => {
94
90
  multiple,
95
91
  maxDisplay,
96
92
  onSelect: handleSelect,
97
- }) })) : (jsx("input", { className: "i-input i-select", placeholder: placeholder, readOnly: true }))) : null, !multiple && (jsx("input", { value: active ? filterValue : displayLabel, className: "i-input i-select", placeholder: displayLabel || placeholder, onChange: handleInputChange, readOnly: !filter })), jsx(Helpericon, { active: !hideArrow, icon: clearable ? undefined : jsx(UnfoldMoreRound, {}), onClick: handleHelperClick }), append] }) }), text && jsx("span", { className: "i-input-message", children: text })] }));
93
+ }) })) : (jsx("input", { className: "i-input i-select", placeholder: placeholder, readOnly: true }))) : null, !multiple && jsx("input", { value: active ? filterValue : displayLabel, className: "i-input i-select", placeholder: displayLabel || placeholder, onChange: handleInputChange, readOnly: !filter }), jsx(Helpericon, { active: !hideArrow, icon: clearable ? undefined : jsx(UnfoldMoreRound, {}), onClick: handleHelperClick }), append] }) }), text && jsx("span", { className: "i-input-message", children: text })] }));
98
94
  };
99
95
 
100
96
  export { Select as default };
@@ -3,15 +3,13 @@ import classNames from 'classnames';
3
3
  import Helpericon from '../utils/helpericon/helpericon.js';
4
4
 
5
5
  const Tag = (props) => {
6
- const { dot, dotClass, outline, round, size = "normal", hoverShowClose, className, children, onClose, onClick, ...restProps } = props;
6
+ const { dot, dotClass, outline, round, size = "normal", className, children, onClose, onClick, ...restProps } = props;
7
7
  return (jsxs("span", { className: classNames("i-tag", {
8
8
  "i-tag-outline": outline,
9
9
  "i-tag-clickable": onClick,
10
10
  [`i-tag-${size}`]: size !== "normal",
11
11
  round,
12
- }, className), onClick: onClick, ...restProps, children: [dot && jsx("span", { className: classNames("i-tag-dot", dotClass) }), children, onClose && (jsx(Helpericon, { active: true, className: classNames("i-tag-close", {
13
- "i-tag-hover-close": hoverShowClose,
14
- }), onClick: onClose }))] }));
12
+ }, className), onClick: onClick, ...restProps, children: [dot && jsx("span", { className: classNames("i-tag-dot", dotClass) }), children, onClose && (jsx(Helpericon, { active: true, className: "i-tag-close i-tag-hover-close", onClick: onClose }))] }));
15
13
  };
16
14
 
17
15
  export { Tag as default };
package/lib/es/index.js CHANGED
@@ -1,7 +1,6 @@
1
1
  export { default as Affix } from './components/affix/affix.js';
2
2
  export { default as Badge } from './components/badge/badge.js';
3
3
  export { default as Button } from './components/button/button.js';
4
- export { default as Card } from './components/card/card.js';
5
4
  export { default as Checkbox } from './components/checkbox/checkbox.js';
6
5
  export { default as Collapse } from './components/collapse/collapse.js';
7
6
  export { default as Datagrid } from './components/datagrid/datagrid.js';
@@ -23,6 +22,7 @@ export { default as ColorPicker } from './components/picker/colors/index.js';
23
22
  export { default as DatePicker } from './components/picker/dates/index.js';
24
23
  export { default as TimePicker } from './components/picker/time/index.js';
25
24
  export { default as DateRange } from './components/picker/daterange/daterange.js';
25
+ export { default as Pill } from './components/pill/pill.js';
26
26
  export { default as Popconfirm } from './components/popconfirm/popconfirm.js';
27
27
  export { default as Popup } from './components/popup/popup.js';
28
28
  export { default as Progress } from './components/progress/progress.js';