@mpen/react-basic-inputs 0.2.9 → 0.2.10

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.
@@ -85,6 +85,31 @@ export declare type InvalidValueToOption<T> = (value: T) => SelectOption<T>;
85
85
  */
86
86
  declare type NonNil = {};
87
87
 
88
+ export declare type NumberChangeEvent = {
89
+ value: number;
90
+ type: 'change';
91
+ timeStamp: number;
92
+ target: HtmlInputElement;
93
+ };
94
+
95
+ export declare type NumberChangeEventHandler = EventCallback<NumberChangeEvent>;
96
+
97
+ export declare const NumberInput: ForwardRefExoticComponent<Omit<Omit<Omit<Omit<DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>, "ref">, "value" | "onChange" | "formatOnChange"> & {
98
+ onChange?: InputChangeEventHandler | undefined;
99
+ value?: string | undefined;
100
+ formatOnChange?: ((value: string) => string) | undefined;
101
+ } & RefAttributes<HTMLInputElement>, "ref">, "value" | "onChange" | "placeholder" | "type"> & {
102
+ value?: number | undefined;
103
+ placeholder?: string | number | undefined;
104
+ onChange?: NumberChangeEventHandler | undefined;
105
+ } & RefAttributes<HTMLInputElement>>;
106
+
107
+ export declare type NumberInputProps = OverrideProps<typeof Input, {
108
+ value?: number;
109
+ placeholder?: string | number;
110
+ onChange?: NumberChangeEventHandler;
111
+ }, 'type'>;
112
+
88
113
  declare type Override<Base, Extension, DeleteKeys extends PropertyKey = never> = Omit<Base, keyof Extension | DeleteKeys> & Extension;
89
114
 
90
115
  declare type OverrideProps<Base extends ElementType, Extension, DeleteKeys extends PropertyKey = never> = Override<ComponentPropsWithoutRef<Base>, Extension, DeleteKeys>;
@@ -1,144 +1,160 @@
1
- var V = Object.defineProperty;
2
- var $ = (e, t, n) => t in e ? V(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
- var w = (e, t, n) => ($(e, typeof t != "symbol" ? t + "" : t, n), n);
4
- import { jsx as x, jsxs as k } from "react/jsx-runtime";
5
- import { useDebugValue as H, useRef as h, useInsertionEffect as L, useEffect as j, useMemo as S, useCallback as D, createElement as q, forwardRef as R, useState as T, useImperativeHandle as F, useLayoutEffect as z, useId as Y } from "react";
1
+ var $ = Object.defineProperty;
2
+ var k = (e, t, r) => t in e ? $(e, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : e[t] = r;
3
+ var F = (e, t, r) => (k(e, typeof t != "symbol" ? t + "" : t, r), r);
4
+ import { jsx as N, jsxs as L } from "react/jsx-runtime";
5
+ import { useDebugValue as Y, useRef as g, useInsertionEffect as H, useEffect as j, useMemo as S, useCallback as _, createElement as q, forwardRef as T, useState as w, useImperativeHandle as z, useLayoutEffect as G, useId as P } from "react";
6
6
  const U = Object.freeze(() => {
7
7
  });
8
- function B(e) {
8
+ function W(e) {
9
9
  return e;
10
10
  }
11
- let b;
12
- typeof window < "u" ? b = (e) => {
13
- H(e);
14
- const t = h(P);
15
- L(() => {
11
+ let p;
12
+ typeof window < "u" ? p = (e) => {
13
+ Y(e);
14
+ const t = g(J);
15
+ H(() => {
16
16
  t.current = e;
17
17
  }, [e]);
18
- const n = h(null);
19
- return n.current || (n.current = function() {
18
+ const r = g(null);
19
+ return r.current || (r.current = function() {
20
20
  return t.current.apply(this, arguments);
21
- }), n.current;
22
- } : b = U;
23
- function J(e) {
24
- return b(e);
21
+ }), r.current;
22
+ } : p = U;
23
+ function B(e) {
24
+ return p(e);
25
25
  }
26
- function P() {
26
+ function J() {
27
27
  throw new Error("INVALID_USE_EVENT_INVOCATION: the callback from useEvent cannot be invoked before the component has mounted.");
28
28
  }
29
- function W() {
30
- var e = h(!0);
29
+ function C() {
30
+ var e = g(!0);
31
31
  return e.current ? (e.current = !1, !0) : e.current;
32
32
  }
33
33
  var O = function(e, t) {
34
- var n = W();
34
+ var r = C();
35
35
  j(function() {
36
- if (!n)
36
+ if (!r)
37
37
  return e();
38
38
  }, t);
39
39
  };
40
- function C(e, ...t) {
40
+ function Q(e, ...t) {
41
41
  return typeof e == "function" ? e(...t) : e;
42
42
  }
43
- function G(e, t) {
43
+ function X(e, t) {
44
44
  if (e.uniqueKey != null)
45
- return C(e.uniqueKey, e, t);
45
+ return Q(e.uniqueKey, e, t);
46
46
  if (typeof e.value == "string")
47
47
  return e.value;
48
48
  if (typeof e.value == "number")
49
49
  return String(e.value);
50
50
  try {
51
- const n = JSON.stringify(e.value);
52
- if (n !== void 0)
53
- return n;
51
+ const r = JSON.stringify(e.value);
52
+ if (r !== void 0)
53
+ return r;
54
54
  } catch {
55
55
  }
56
56
  return String(t);
57
57
  }
58
- class _ {
58
+ class V {
59
59
  constructor() {
60
- w(this, "usedKeys", /* @__PURE__ */ new Map());
60
+ F(this, "usedKeys", /* @__PURE__ */ new Map());
61
61
  }
62
- fix(t, n) {
63
- let u = G(t, n);
62
+ fix(t, r) {
63
+ let u = X(t, r);
64
64
  for (; ; ) {
65
- let r = this.usedKeys.get(u);
66
- if (r === void 0) {
65
+ let n = this.usedKeys.get(u);
66
+ if (n === void 0) {
67
67
  this.usedKeys.set(u, 1);
68
68
  break;
69
69
  }
70
- this.usedKeys.set(u, ++r), u = `${u}(${r})`;
70
+ this.usedKeys.set(u, ++n), u = `${u}(${n})`;
71
71
  }
72
72
  return u;
73
73
  }
74
74
  }
75
- const Q = (e) => ({
75
+ const Z = (e) => ({
76
76
  value: e,
77
77
  text: String(e),
78
78
  disabled: !0,
79
- key: Z
80
- }), X = "3c9369b7-0a5e-46ea-93c2-e8b9fec67fdb", Z = "1a53f789-77f5-4ce6-a829-b00e563f1ee8";
81
- function ie({
79
+ key: te
80
+ }), ee = "3c9369b7-0a5e-46ea-93c2-e8b9fec67fdb", te = "1a53f789-77f5-4ce6-a829-b00e563f1ee8";
81
+ function oe({
82
82
  options: e,
83
83
  value: t,
84
- invalidValueOption: n = Q,
84
+ invalidValueOption: r = Z,
85
85
  onChange: u,
86
- placeholder: r,
86
+ placeholder: n,
87
87
  ...a
88
88
  }) {
89
- const f = t == null, o = S(() => t != null && e.some((s) => s.value == t), [e, t]), d = S(() => t == null || !n ? null : n(t), [n, t]), i = S(() => {
90
- if (o)
89
+ const f = t == null, l = S(() => t != null && e.some((s) => s.value == t), [e, t]), o = S(() => t == null || !r ? null : r(t), [r, t]), c = S(() => {
90
+ if (l)
91
91
  return e;
92
92
  const s = [...e];
93
- return f ? r != null && s.unshift({ text: r, hidden: !0, value: null, key: X }) : d && s.push(d), s;
94
- }, [o, e, f, d, r]), y = J((s) => {
95
- const g = s.target.selectedIndex, N = i[g];
93
+ return f ? n != null && s.unshift({ text: n, hidden: !0, value: null, key: ee }) : o && s.push(o), s;
94
+ }, [l, e, f, o, n]), x = B((s) => {
95
+ const I = s.target.selectedIndex, v = c[I];
96
96
  u == null || u({
97
- value: N.value,
97
+ value: v.value,
98
98
  // option: opt,
99
99
  // event: ev,
100
- index: g,
100
+ index: I,
101
101
  type: "change",
102
102
  timeStamp: s.timeStamp,
103
103
  target: s.target
104
104
  });
105
- }), c = h(null), l = D(() => {
106
- c.current && (c.current.selectedIndex < 0 || i[c.current.selectedIndex].value != t) && (c.current.selectedIndex = i.findIndex((s) => s.value == t));
107
- }, [i, t]), v = (s) => {
108
- c.current = s, l();
105
+ }), i = g(null), d = _(() => {
106
+ i.current && (i.current.selectedIndex < 0 || c[i.current.selectedIndex].value != t) && (i.current.selectedIndex = c.findIndex((s) => s.value == t));
107
+ }, [c, t]), b = (s) => {
108
+ i.current = s, d();
109
109
  };
110
110
  O(() => {
111
- l();
112
- }, [l]);
113
- const m = new _();
114
- return /* @__PURE__ */ x("select", { ...a, onChange: y, ref: v, children: i.map((s, g) => {
115
- const { value: N, text: ne, uniqueKey: re, ...A } = s, K = m.fix(s, g);
116
- return /* @__PURE__ */ q("option", { ...A, key: K, value: K }, s.text);
111
+ d();
112
+ }, [d]);
113
+ const m = new V();
114
+ return /* @__PURE__ */ N("select", { ...a, onChange: x, ref: b, children: c.map((s, I) => {
115
+ const { value: v, text: ie, uniqueKey: ce, ...R } = s, K = m.fix(s, I);
116
+ return /* @__PURE__ */ q("option", { ...R, key: K, value: K }, s.text);
117
117
  }) });
118
118
  }
119
- function ee(e) {
119
+ function re(e) {
120
120
  return e ? String(e).replace(/\s+/gu, " ").trim() : "";
121
121
  }
122
- function p(e, t = 2) {
122
+ function ne(e) {
123
+ try {
124
+ return e.toLocaleString("en-US", { useGrouping: !1, maximumFractionDigits: 20 });
125
+ } catch {
126
+ return e.toFixed(14).replace(/\.?0+$/, "");
127
+ }
128
+ }
129
+ function ue(e) {
130
+ return e ? e === "∞" || e === "+∞" ? Number.POSITIVE_INFINITY : e === "-∞" ? Number.NEGATIVE_INFINITY : Number.parseFloat(e) : Number.NaN;
131
+ }
132
+ function D(e) {
133
+ return Number.isNaN(e) ? "" : e === Number.POSITIVE_INFINITY ? "∞" : e === Number.NEGATIVE_INFINITY ? "-∞" : ne(e);
134
+ }
135
+ function se(e) {
136
+ return e ? D(Number.parseFloat(e)) : "";
137
+ }
138
+ function y(e, t = 2) {
123
139
  return String(e).padStart(t, "0");
124
140
  }
125
- function I(e) {
141
+ function h(e) {
126
142
  const t = new Date(e);
127
- let n = `${t.getFullYear()}-${p(t.getMonth() + 1)}-${p(t.getDate())}T${p(t.getHours())}:${p(t.getMinutes())}`;
128
- return (t.getSeconds() !== 0 || t.getMilliseconds() !== 0) && (n += ":" + p(t.getSeconds()), t.getMilliseconds() !== 0 && (n += "." + p(t.getMilliseconds(), 3))), n;
143
+ let r = `${t.getFullYear()}-${y(t.getMonth() + 1)}-${y(t.getDate())}T${y(t.getHours())}:${y(t.getMinutes())}`;
144
+ return (t.getSeconds() !== 0 || t.getMilliseconds() !== 0) && (r += ":" + y(t.getSeconds()), t.getMilliseconds() !== 0 && (r += "." + y(t.getMilliseconds(), 3))), r;
129
145
  }
130
- const te = R(function({ value: t = "", onPaste: n, onChange: u, onInput: r, onBlur: a, formatOnChange: f = B, ...o }, d) {
131
- const [i, y] = T(t), c = h(t), l = h(!1);
146
+ const A = T(function({ value: t = "", onPaste: r, onChange: u, onInput: n, onBlur: a, formatOnChange: f = W, ...l }, o) {
147
+ const [c, x] = w(t), i = g(t), d = g(!1);
132
148
  O(() => {
133
- y(t), l.current = !1, c.current = t;
149
+ x(t), d.current = !1, i.current = t;
134
150
  }, [t]);
135
- const v = {
151
+ const b = {
136
152
  type: "text",
137
- ...o,
138
- ref: d,
139
- value: i,
153
+ ...l,
154
+ ref: o,
155
+ value: c,
140
156
  onChange: (m) => {
141
- y(m.target.value);
157
+ x(m.target.value);
142
158
  },
143
159
  // TODO: fire a change event onPaste ?
144
160
  // formatOnPaste?
@@ -157,122 +173,137 @@ const te = R(function({ value: t = "", onPaste: n, onChange: u, onInput: r, onBl
157
173
  // // setCurrentValue(formatOnChange(ev.clipboardData.getData('text/plain')))
158
174
  // },
159
175
  onInput: (m) => {
160
- l.current = !0, r == null || r(m);
176
+ d.current = !0, n == null || n(m);
161
177
  },
162
178
  onBlur: (m) => {
163
- const s = f(i);
164
- l.current && (s !== c.current && (u == null || u({
179
+ const s = f(c);
180
+ d.current && (s !== i.current && (u == null || u({
165
181
  type: "change",
166
182
  value: s,
167
183
  timeStamp: m.timeStamp,
168
184
  target: m.target
169
- }), c.current = s), s !== m.target.value && y(s)), a == null || a(m);
185
+ }), i.current = s), s !== m.target.value && x(s)), a == null || a(m);
170
186
  }
171
187
  };
172
- return /* @__PURE__ */ x("input", { ...v });
188
+ return /* @__PURE__ */ N("input", { ...b });
173
189
  });
174
- function le({ formatOnChange: e = ee, ...t }) {
175
- return /* @__PURE__ */ x(te, { formatOnChange: e, ...t, type: "text" });
190
+ function de({ formatOnChange: e = re, ...t }) {
191
+ return /* @__PURE__ */ N(A, { formatOnChange: e, ...t, type: "text" });
176
192
  }
177
- const ae = R(function({
193
+ const me = T(function({
178
194
  onInput: t,
179
- style: n,
195
+ style: r,
180
196
  initialHeight: u = "auto",
181
- ...r
197
+ ...n
182
198
  }, a) {
183
- const f = h(null), [o, d] = T(u), i = D(() => {
184
- const c = f.current;
185
- if (!c)
199
+ const f = g(null), [l, o] = w(u), c = _(() => {
200
+ const i = f.current;
201
+ if (!i)
186
202
  return;
187
- c.style.height = u;
188
- const l = `${c.scrollHeight}px`;
189
- d(l), c.style.height = l;
203
+ i.style.height = u;
204
+ const d = `${i.scrollHeight}px`;
205
+ o(d), i.style.height = d;
190
206
  }, [u]);
191
- F(a, () => ({
207
+ z(a, () => ({
192
208
  element: f.current,
193
- adjustHeight: i
194
- }), [i]);
195
- const y = b((c) => {
196
- i(), t == null || t(c);
209
+ adjustHeight: c
210
+ }), [c]);
211
+ const x = p((i) => {
212
+ c(), t == null || t(i);
197
213
  });
198
- return z(() => {
199
- i();
200
- const c = f.current;
201
- if (!c)
214
+ return G(() => {
215
+ c();
216
+ const i = f.current;
217
+ if (!i)
202
218
  return;
203
- const l = new ResizeObserver((v) => {
204
- i();
219
+ const d = new ResizeObserver((b) => {
220
+ c();
205
221
  });
206
- return l.observe(c), () => {
207
- l.unobserve(c);
222
+ return d.observe(i), () => {
223
+ d.unobserve(i);
208
224
  };
209
- }, [i]), /* @__PURE__ */ x("textarea", { ...r, style: {
225
+ }, [c]), /* @__PURE__ */ N("textarea", { ...n, style: {
210
226
  overflow: "hidden",
211
227
  // these 2 styles aren't needed if the caller sets them in CSS.
212
228
  resize: "none",
213
- ...n,
214
- height: o
215
- }, onInput: y, ref: f });
229
+ ...r,
230
+ height: l
231
+ }, onInput: x, ref: f });
216
232
  });
217
- function fe(e) {
218
- const t = Y(), n = e.name ?? t, u = e.valueEquals ?? Object.is, r = e.options ?? [], a = new _(), f = b((o) => {
219
- const d = Number(o.target.value), i = r[d];
220
- i != null && e.onChange != null && e.onChange({
221
- value: i.value,
222
- index: d,
233
+ function Ne(e) {
234
+ const t = P(), r = e.name ?? t, u = e.valueEquals ?? Object.is, n = e.options ?? [], a = new V(), f = p((l) => {
235
+ const o = Number(l.target.value), c = n[o];
236
+ c != null && e.onChange != null && e.onChange({
237
+ value: c.value,
238
+ index: o,
223
239
  type: "change",
224
- timeStamp: o.timeStamp,
225
- target: o.target
240
+ timeStamp: l.timeStamp,
241
+ target: l.target
226
242
  });
227
243
  });
228
- return /* @__PURE__ */ x("ul", { className: e.className, children: r.map((o, d) => {
229
- const { value: i, text: y, key: c, itemClassName: l, labelClassName: v, inputClassName: m, textClassName: s, ...g } = o, N = a.fix(o, d);
230
- return e.value !== void 0 && (g.checked = u(i, e.value)), /* @__PURE__ */ x("li", { className: l, "aria-disabled": g.disabled, children: /* @__PURE__ */ k("label", { className: v, children: [
231
- /* @__PURE__ */ x("input", { ...g, className: m, value: d, onChange: f, name: n, type: "radio" }),
232
- /* @__PURE__ */ x("span", { className: s, children: y })
233
- ] }) }, N);
244
+ return /* @__PURE__ */ N("ul", { className: e.className, children: n.map((l, o) => {
245
+ const { value: c, text: x, key: i, itemClassName: d, labelClassName: b, inputClassName: m, textClassName: s, ...I } = l, v = a.fix(l, o);
246
+ return e.value !== void 0 && (I.checked = u(c, e.value)), /* @__PURE__ */ N("li", { className: d, "aria-disabled": I.disabled, children: /* @__PURE__ */ L("label", { className: b, children: [
247
+ /* @__PURE__ */ N("input", { ...I, className: m, value: o, onChange: f, name: r, type: "radio" }),
248
+ /* @__PURE__ */ N("span", { className: s, children: x })
249
+ ] }) }, v);
234
250
  }) });
235
251
  }
236
252
  function M() {
237
- return h(null);
253
+ return g(null);
238
254
  }
239
255
  function E(e) {
240
256
  e.current != null && (clearTimeout(e.current), e.current = null);
241
257
  }
242
- const oe = ({
258
+ const xe = ({
243
259
  value: e,
244
260
  onChange: t,
245
- debounce: n = 500,
261
+ debounce: r = 500,
246
262
  ...u
247
263
  }) => {
248
- const r = M(), a = M();
264
+ const n = M(), a = M();
249
265
  O(() => {
250
- E(a), r.current != null && (r.current.value = e);
266
+ E(a), n.current != null && (n.current.value = e);
251
267
  }, [e]);
252
268
  const f = () => {
253
- r.current != null && r.current.value !== e && (t == null || t({ value: r.current.value }));
269
+ n.current != null && n.current.value !== e && (t == null || t({ value: n.current.value }));
254
270
  };
255
- return /* @__PURE__ */ x("input", { ...u, ref: r, defaultValue: e, onInput: () => {
256
- E(a), a.current = setTimeout(f, n);
271
+ return /* @__PURE__ */ N("input", { ...u, ref: n, defaultValue: e, onInput: () => {
272
+ E(a), a.current = setTimeout(f, r);
257
273
  }, onBlur: () => {
258
274
  E(a), f();
259
275
  } });
260
276
  };
261
- function de({ onClick: e, ...t }) {
262
- return t.onClick = (n) => {
263
- n.preventDefault(), e == null || e(n);
264
- }, /* @__PURE__ */ x("button", { type: "button", ...t });
277
+ function Ie({ onClick: e, ...t }) {
278
+ return t.onClick = (r) => {
279
+ r.preventDefault(), e == null || e(r);
280
+ }, /* @__PURE__ */ N("button", { type: "button", ...t });
265
281
  }
266
- function me({ value: e, defaultValue: t, min: n, max: u, ...r }) {
267
- return e !== void 0 && (r.value = e === null ? "" : I(e)), t != null && (r.defaultValue = I(t)), n != null && (r.min = I(n)), u != null && (r.max = I(u)), /* @__PURE__ */ x("input", { type: "datetime-local", ...r });
282
+ function ge({ value: e, defaultValue: t, min: r, max: u, ...n }) {
283
+ return e !== void 0 && (n.value = e === null ? "" : h(e)), t != null && (n.defaultValue = h(t)), r != null && (n.min = h(r)), u != null && (n.max = h(u)), /* @__PURE__ */ N("input", { type: "datetime-local", ...n });
268
284
  }
285
+ const be = T(function({ placeholder: t, formatOnChange: r = se, onChange: u, value: n, ...a }, f) {
286
+ const l = {
287
+ inputMode: "decimal",
288
+ ...a,
289
+ formatOnChange: r,
290
+ type: "number"
291
+ };
292
+ return n !== void 0 && (l.value = D(n)), t != null && (l.placeholder = String(t)), u != null && (l.onChange = (o) => {
293
+ u({
294
+ ...o,
295
+ value: ue(o.value)
296
+ });
297
+ }), /* @__PURE__ */ N(A, { ...l, ref: f });
298
+ });
269
299
  export {
270
- de as ActionButton,
271
- me as DatetimeLocalInput,
272
- oe as DebouncedInput,
273
- te as Input,
274
- fe as RadioMenu,
275
- ie as Select,
276
- ae as TextArea,
277
- le as TextInput
300
+ Ie as ActionButton,
301
+ ge as DatetimeLocalInput,
302
+ xe as DebouncedInput,
303
+ A as Input,
304
+ be as NumberInput,
305
+ Ne as RadioMenu,
306
+ oe as Select,
307
+ me as TextArea,
308
+ de as TextInput
278
309
  };
@@ -1 +1 @@
1
- (function(f,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("react/jsx-runtime"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react"],c):(f=typeof globalThis<"u"?globalThis:f||self,c(f["@mpen/react-basic-inputs"]={},f.jsxRuntime,f.React))})(this,function(f,c,r){"use strict";var Q=Object.defineProperty;var X=(f,c,r)=>c in f?Q(f,c,{enumerable:!0,configurable:!0,writable:!0,value:r}):f[c]=r;var D=(f,c,r)=>(X(f,typeof c!="symbol"?c+"":c,r),r);const A=Object.freeze(()=>{});function j(e){return e}let I;typeof window<"u"?I=e=>{r.useDebugValue(e);const t=r.useRef(k);r.useInsertionEffect(()=>{t.current=e},[e]);const n=r.useRef(null);return n.current||(n.current=function(){return t.current.apply(this,arguments)}),n.current}:I=A;function _(e){return I(e)}function k(){throw new Error("INVALID_USE_EVENT_INVOCATION: the callback from useEvent cannot be invoked before the component has mounted.")}function V(){var e=r.useRef(!0);return e.current?(e.current=!1,!0):e.current}var E=function(e,t){var n=V();r.useEffect(function(){if(!n)return e()},t)};function $(e,...t){return typeof e=="function"?e(...t):e}function q(e,t){if(e.uniqueKey!=null)return $(e.uniqueKey,e,t);if(typeof e.value=="string")return e.value;if(typeof e.value=="number")return String(e.value);try{const n=JSON.stringify(e.value);if(n!==void 0)return n}catch{}return String(t)}class M{constructor(){D(this,"usedKeys",new Map)}fix(t,n){let s=q(t,n);for(;;){let u=this.usedKeys.get(s);if(u===void 0){this.usedKeys.set(s,1);break}this.usedKeys.set(s,++u),s=`${s}(${u})`}return s}}const L=e=>({value:e,text:String(e),disabled:!0,key:F}),H="3c9369b7-0a5e-46ea-93c2-e8b9fec67fdb",F="1a53f789-77f5-4ce6-a829-b00e563f1ee8";function z({options:e,value:t,invalidValueOption:n=L,onChange:s,placeholder:u,...d}){const y=t==null,m=r.useMemo(()=>t!=null&&e.some(i=>i.value==t),[e,t]),x=r.useMemo(()=>t==null||!n?null:n(t),[n,t]),a=r.useMemo(()=>{if(m)return e;const i=[...e];return y?u!=null&&i.unshift({text:u,hidden:!0,value:null,key:H}):x&&i.push(x),i},[m,e,y,x,u]),h=_(i=>{const p=i.target.selectedIndex,N=a[p];s==null||s({value:N.value,index:p,type:"change",timeStamp:i.timeStamp,target:i.target})}),l=r.useRef(null),o=r.useCallback(()=>{l.current&&(l.current.selectedIndex<0||a[l.current.selectedIndex].value!=t)&&(l.current.selectedIndex=a.findIndex(i=>i.value==t))},[a,t]),v=i=>{l.current=i,o()};E(()=>{o()},[o]);const g=new M;return c.jsx("select",{...d,onChange:h,ref:v,children:a.map((i,p)=>{const{value:N,text:Z,uniqueKey:R,...G}=i,w=g.fix(i,p);return r.createElement("option",{...G,key:w,value:w},i.text)})})}function Y(e){return e?String(e).replace(/\s+/gu," ").trim():""}function b(e,t=2){return String(e).padStart(t,"0")}function S(e){const t=new Date(e);let n=`${t.getFullYear()}-${b(t.getMonth()+1)}-${b(t.getDate())}T${b(t.getHours())}:${b(t.getMinutes())}`;return(t.getSeconds()!==0||t.getMilliseconds()!==0)&&(n+=":"+b(t.getSeconds()),t.getMilliseconds()!==0&&(n+="."+b(t.getMilliseconds(),3))),n}const T=r.forwardRef(function({value:t="",onPaste:n,onChange:s,onInput:u,onBlur:d,formatOnChange:y=j,...m},x){const[a,h]=r.useState(t),l=r.useRef(t),o=r.useRef(!1);E(()=>{h(t),o.current=!1,l.current=t},[t]);const v={type:"text",...m,ref:x,value:a,onChange:g=>{h(g.target.value)},onInput:g=>{o.current=!0,u==null||u(g)},onBlur:g=>{const i=y(a);o.current&&(i!==l.current&&(s==null||s({type:"change",value:i,timeStamp:g.timeStamp,target:g.target}),l.current=i),i!==g.target.value&&h(i)),d==null||d(g)}};return c.jsx("input",{...v})});function B({formatOnChange:e=Y,...t}){return c.jsx(T,{formatOnChange:e,...t,type:"text"})}const P=r.forwardRef(function({onInput:t,style:n,initialHeight:s="auto",...u},d){const y=r.useRef(null),[m,x]=r.useState(s),a=r.useCallback(()=>{const l=y.current;if(!l)return;l.style.height=s;const o=`${l.scrollHeight}px`;x(o),l.style.height=o},[s]);r.useImperativeHandle(d,()=>({element:y.current,adjustHeight:a}),[a]);const h=I(l=>{a(),t==null||t(l)});return r.useLayoutEffect(()=>{a();const l=y.current;if(!l)return;const o=new ResizeObserver(v=>{a()});return o.observe(l),()=>{o.unobserve(l)}},[a]),c.jsx("textarea",{...u,style:{overflow:"hidden",resize:"none",...n,height:m},onInput:h,ref:y})});function U(e){const t=r.useId(),n=e.name??t,s=e.valueEquals??Object.is,u=e.options??[],d=new M,y=I(m=>{const x=Number(m.target.value),a=u[x];a!=null&&e.onChange!=null&&e.onChange({value:a.value,index:x,type:"change",timeStamp:m.timeStamp,target:m.target})});return c.jsx("ul",{className:e.className,children:u.map((m,x)=>{const{value:a,text:h,key:l,itemClassName:o,labelClassName:v,inputClassName:g,textClassName:i,...p}=m,N=d.fix(m,x);return e.value!==void 0&&(p.checked=s(a,e.value)),c.jsx("li",{className:o,"aria-disabled":p.disabled,children:c.jsxs("label",{className:v,children:[c.jsx("input",{...p,className:g,value:x,onChange:y,name:n,type:"radio"}),c.jsx("span",{className:i,children:h})]})},N)})})}function K(){return r.useRef(null)}function O(e){e.current!=null&&(clearTimeout(e.current),e.current=null)}const C=({value:e,onChange:t,debounce:n=500,...s})=>{const u=K(),d=K();E(()=>{O(d),u.current!=null&&(u.current.value=e)},[e]);const y=()=>{u.current!=null&&u.current.value!==e&&(t==null||t({value:u.current.value}))};return c.jsx("input",{...s,ref:u,defaultValue:e,onInput:()=>{O(d),d.current=setTimeout(y,n)},onBlur:()=>{O(d),y()}})};function J({onClick:e,...t}){return t.onClick=n=>{n.preventDefault(),e==null||e(n)},c.jsx("button",{type:"button",...t})}function W({value:e,defaultValue:t,min:n,max:s,...u}){return e!==void 0&&(u.value=e===null?"":S(e)),t!=null&&(u.defaultValue=S(t)),n!=null&&(u.min=S(n)),s!=null&&(u.max=S(s)),c.jsx("input",{type:"datetime-local",...u})}f.ActionButton=J,f.DatetimeLocalInput=W,f.DebouncedInput=C,f.Input=T,f.RadioMenu=U,f.Select=z,f.TextArea=P,f.TextInput=B,Object.defineProperty(f,Symbol.toStringTag,{value:"Module"})});
1
+ (function(f,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("react/jsx-runtime"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react"],i):(f=typeof globalThis<"u"?globalThis:f||self,i(f["@mpen/react-basic-inputs"]={},f.jsxRuntime,f.React))})(this,function(f,i,u){"use strict";var te=Object.defineProperty;var ne=(f,i,u)=>i in f?te(f,i,{enumerable:!0,configurable:!0,writable:!0,value:u}):f[i]=u;var F=(f,i,u)=>(ne(f,typeof i!="symbol"?i+"":i,u),u);const _=Object.freeze(()=>{});function A(e){return e}let h;typeof window<"u"?h=e=>{u.useDebugValue(e);const t=u.useRef(j);u.useInsertionEffect(()=>{t.current=e},[e]);const n=u.useRef(null);return n.current||(n.current=function(){return t.current.apply(this,arguments)}),n.current}:h=_;function V(e){return h(e)}function j(){throw new Error("INVALID_USE_EVENT_INVOCATION: the callback from useEvent cannot be invoked before the component has mounted.")}function $(){var e=u.useRef(!0);return e.current?(e.current=!1,!0):e.current}var T=function(e,t){var n=$();u.useEffect(function(){if(!n)return e()},t)};function k(e,...t){return typeof e=="function"?e(...t):e}function L(e,t){if(e.uniqueKey!=null)return k(e.uniqueKey,e,t);if(typeof e.value=="string")return e.value;if(typeof e.value=="number")return String(e.value);try{const n=JSON.stringify(e.value);if(n!==void 0)return n}catch{}return String(t)}class M{constructor(){F(this,"usedKeys",new Map)}fix(t,n){let s=L(t,n);for(;;){let r=this.usedKeys.get(s);if(r===void 0){this.usedKeys.set(s,1);break}this.usedKeys.set(s,++r),s=`${s}(${r})`}return s}}const q=e=>({value:e,text:String(e),disabled:!0,key:H}),Y="3c9369b7-0a5e-46ea-93c2-e8b9fec67fdb",H="1a53f789-77f5-4ce6-a829-b00e563f1ee8";function z({options:e,value:t,invalidValueOption:n=q,onChange:s,placeholder:r,...d}){const m=t==null,a=u.useMemo(()=>t!=null&&e.some(c=>c.value==t),[e,t]),N=u.useMemo(()=>t==null||!n?null:n(t),[n,t]),o=u.useMemo(()=>{if(a)return e;const c=[...e];return m?r!=null&&c.unshift({text:r,hidden:!0,value:null,key:Y}):N&&c.push(N),c},[a,e,m,N,r]),y=V(c=>{const g=c.target.selectedIndex,v=o[g];s==null||s({value:v.value,index:g,type:"change",timeStamp:c.timeStamp,target:c.target})}),l=u.useRef(null),I=u.useCallback(()=>{l.current&&(l.current.selectedIndex<0||o[l.current.selectedIndex].value!=t)&&(l.current.selectedIndex=o.findIndex(c=>c.value==t))},[o,t]),x=c=>{l.current=c,I()};T(()=>{I()},[I]);const b=new M;return i.jsx("select",{...d,onChange:y,ref:x,children:o.map((c,g)=>{const{value:v,text:re,uniqueKey:ue,...ee}=c,D=b.fix(c,g);return u.createElement("option",{...ee,key:D,value:D},c.text)})})}function P(e){return e?String(e).replace(/\s+/gu," ").trim():""}function G(e){try{return e.toLocaleString("en-US",{useGrouping:!1,maximumFractionDigits:20})}catch{return e.toFixed(14).replace(/\.?0+$/,"")}}function U(e){return e?e==="∞"||e==="+∞"?Number.POSITIVE_INFINITY:e==="-∞"?Number.NEGATIVE_INFINITY:Number.parseFloat(e):Number.NaN}function w(e){return Number.isNaN(e)?"":e===Number.POSITIVE_INFINITY?"∞":e===Number.NEGATIVE_INFINITY?"-∞":G(e)}function B(e){return e?w(Number.parseFloat(e)):""}function p(e,t=2){return String(e).padStart(t,"0")}function S(e){const t=new Date(e);let n=`${t.getFullYear()}-${p(t.getMonth()+1)}-${p(t.getDate())}T${p(t.getHours())}:${p(t.getMinutes())}`;return(t.getSeconds()!==0||t.getMilliseconds()!==0)&&(n+=":"+p(t.getSeconds()),t.getMilliseconds()!==0&&(n+="."+p(t.getMilliseconds(),3))),n}const E=u.forwardRef(function({value:t="",onPaste:n,onChange:s,onInput:r,onBlur:d,formatOnChange:m=A,...a},N){const[o,y]=u.useState(t),l=u.useRef(t),I=u.useRef(!1);T(()=>{y(t),I.current=!1,l.current=t},[t]);const x={type:"text",...a,ref:N,value:o,onChange:b=>{y(b.target.value)},onInput:b=>{I.current=!0,r==null||r(b)},onBlur:b=>{const c=m(o);I.current&&(c!==l.current&&(s==null||s({type:"change",value:c,timeStamp:b.timeStamp,target:b.target}),l.current=c),c!==b.target.value&&y(c)),d==null||d(b)}};return i.jsx("input",{...x})});function W({formatOnChange:e=P,...t}){return i.jsx(E,{formatOnChange:e,...t,type:"text"})}const C=u.forwardRef(function({onInput:t,style:n,initialHeight:s="auto",...r},d){const m=u.useRef(null),[a,N]=u.useState(s),o=u.useCallback(()=>{const l=m.current;if(!l)return;l.style.height=s;const I=`${l.scrollHeight}px`;N(I),l.style.height=I},[s]);u.useImperativeHandle(d,()=>({element:m.current,adjustHeight:o}),[o]);const y=h(l=>{o(),t==null||t(l)});return u.useLayoutEffect(()=>{o();const l=m.current;if(!l)return;const I=new ResizeObserver(x=>{o()});return I.observe(l),()=>{I.unobserve(l)}},[o]),i.jsx("textarea",{...r,style:{overflow:"hidden",resize:"none",...n,height:a},onInput:y,ref:m})});function J(e){const t=u.useId(),n=e.name??t,s=e.valueEquals??Object.is,r=e.options??[],d=new M,m=h(a=>{const N=Number(a.target.value),o=r[N];o!=null&&e.onChange!=null&&e.onChange({value:o.value,index:N,type:"change",timeStamp:a.timeStamp,target:a.target})});return i.jsx("ul",{className:e.className,children:r.map((a,N)=>{const{value:o,text:y,key:l,itemClassName:I,labelClassName:x,inputClassName:b,textClassName:c,...g}=a,v=d.fix(a,N);return e.value!==void 0&&(g.checked=s(o,e.value)),i.jsx("li",{className:I,"aria-disabled":g.disabled,children:i.jsxs("label",{className:x,children:[i.jsx("input",{...g,className:b,value:N,onChange:m,name:n,type:"radio"}),i.jsx("span",{className:c,children:y})]})},v)})})}function K(){return u.useRef(null)}function O(e){e.current!=null&&(clearTimeout(e.current),e.current=null)}const Q=({value:e,onChange:t,debounce:n=500,...s})=>{const r=K(),d=K();T(()=>{O(d),r.current!=null&&(r.current.value=e)},[e]);const m=()=>{r.current!=null&&r.current.value!==e&&(t==null||t({value:r.current.value}))};return i.jsx("input",{...s,ref:r,defaultValue:e,onInput:()=>{O(d),d.current=setTimeout(m,n)},onBlur:()=>{O(d),m()}})};function X({onClick:e,...t}){return t.onClick=n=>{n.preventDefault(),e==null||e(n)},i.jsx("button",{type:"button",...t})}function Z({value:e,defaultValue:t,min:n,max:s,...r}){return e!==void 0&&(r.value=e===null?"":S(e)),t!=null&&(r.defaultValue=S(t)),n!=null&&(r.min=S(n)),s!=null&&(r.max=S(s)),i.jsx("input",{type:"datetime-local",...r})}const R=u.forwardRef(function({placeholder:t,formatOnChange:n=B,onChange:s,value:r,...d},m){const a={inputMode:"decimal",...d,formatOnChange:n,type:"number"};return r!==void 0&&(a.value=w(r)),t!=null&&(a.placeholder=String(t)),s!=null&&(a.onChange=N=>{s({...N,value:U(N.value)})}),i.jsx(E,{...a,ref:m})});f.ActionButton=X,f.DatetimeLocalInput=Z,f.DebouncedInput=Q,f.Input=E,f.NumberInput=R,f.RadioMenu=J,f.Select=z,f.TextArea=C,f.TextInput=W,Object.defineProperty(f,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mpen/react-basic-inputs",
3
3
  "private": false,
4
- "version": "0.2.9",
4
+ "version": "0.2.10",
5
5
  "type": "module",
6
6
  "files": ["dist"],
7
7
  "exports": {