@hortiview/shared-components 0.0.4804 → 0.0.4870

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,13 +1,16 @@
1
- import { FieldValues, Path } from 'react-hook-form';
1
+ import { FieldPath, FieldValues, Path, RegisterOptions } from 'react-hook-form';
2
2
  type DatePickerProps<T extends FieldValues> = {
3
3
  /** Name of the property that this date picker represents. */
4
4
  propertyName: Path<T>;
5
5
  /** Label to be displayed with the date picker. */
6
6
  label: string;
7
- /** Determines if this input is mandatory. */
8
- required?: boolean;
9
- /** Custom text to show when the required field is not filled. */
10
- requiredText?: string;
7
+ /**
8
+ * the rules for the picker
9
+ * @default undefined
10
+ * @example { required: { value: true, message: 'This field is required' } }
11
+ * @example { validate: (value) => value === 'admin' }
12
+ */
13
+ rules?: Omit<RegisterOptions<T, FieldPath<T>>, 'disabled' | 'valueAsNumber' | 'valueAsDate' | 'setValueAs'> | undefined;
11
14
  /** ClassName */
12
15
  className?: string;
13
16
  /** Add the number of years to subtract from the current date as bottom of range */
@@ -23,14 +26,13 @@ type DatePickerProps<T extends FieldValues> = {
23
26
  *
24
27
  * @param propertyName - the name of the property this date picker maps to in the form.
25
28
  * @param label - label displayed along with the date picker.
26
- * @param required - determines if the date input is required.
27
- * @param requiredText - text to be shown if the required date is not selected.
28
29
  * @param className - class name to be passed if provided.
29
30
  * @param minRangeYear - number of years to subtract from the current date as bottom of range
30
31
  * @param maxRangeYear - number of years to add to the current date as top of range
31
32
  * @param locale - locale to be used for the date picker.
33
+ * @param rules - rules for the date picker. default _undefined_ example: _{ required: { value: true, message: 'This field is required' } }_
32
34
  *
33
35
  * @returns A JSX element that renders a date picker form input.
34
36
  */
35
- export declare const FormDatePicker: <T extends FieldValues>({ propertyName, label, required, requiredText, className, minRangeYear, maxRangeYear, locale, }: DatePickerProps<T>) => import("react/jsx-runtime").JSX.Element;
37
+ export declare const FormDatePicker: <T extends FieldValues>({ propertyName, label, className, minRangeYear, maxRangeYear, locale, rules, }: DatePickerProps<T>) => import("react/jsx-runtime").JSX.Element;
36
38
  export {};
@@ -1,87 +1,80 @@
1
1
  import "../../../assets/FormDatePicker.css";
2
- import { jsx as l } from "react/jsx-runtime";
3
- import { Datepicker as F } from "@element/react-components";
4
- import { useState as T, useEffect as k, useMemo as c } from "react";
5
- import { useFormContext as z, Controller as I } from "react-hook-form";
6
- const O = "_datePickerContainer_zxe48_1", Q = "_invalid_zxe48_5", Y = "_fromPickerText_zxe48_9", d = {
7
- datePickerContainer: O,
8
- invalid: Q,
9
- fromPickerText: Y
10
- }, H = ({
2
+ import { jsx as s } from "react/jsx-runtime";
3
+ import { Datepicker as S } from "@element/react-components";
4
+ import { useMemo as r } from "react";
5
+ import { useFormContext as h, Controller as q } from "react-hook-form";
6
+ import { g as F } from "../../../get-Dyz8NMrE.js";
7
+ const z = "_datePickerContainer_zxe48_1", w = "_invalid_zxe48_5", H = "_fromPickerText_zxe48_9", c = {
8
+ datePickerContainer: z,
9
+ invalid: w,
10
+ fromPickerText: H
11
+ }, I = ({
11
12
  propertyName: e,
12
13
  label: x,
13
- required: n = !1,
14
- requiredText: o,
15
- className: P,
16
- minRangeYear: f,
17
- maxRangeYear: m,
18
- locale: D
14
+ className: k,
15
+ minRangeYear: l,
16
+ maxRangeYear: d,
17
+ locale: P,
18
+ rules: i
19
19
  }) => {
20
20
  const {
21
21
  control: g,
22
- formState: { errors: t },
23
- watch: S,
24
- getFieldState: a
25
- } = z(), [_, u] = T(), v = S(e);
26
- k(() => {
27
- u(/* @__PURE__ */ new Date());
28
- }, []), k(() => {
29
- u(new Date(v));
30
- }, [v]);
31
- const C = c(
32
- () => (/* @__PURE__ */ new Date()).getFullYear() - (f ?? 95),
33
- [f]
34
- ), h = c(
35
- () => (/* @__PURE__ */ new Date()).getFullYear() + (m ?? 15),
36
- [m]
37
- ), w = c(() => {
38
- const s = () => {
22
+ formState: { errors: t, isValidating: m }
23
+ } = h(), f = r(
24
+ () => i?.required?.value,
25
+ [i]
26
+ ), u = r(
27
+ () => i?.required?.message,
28
+ [i]
29
+ ), v = r(
30
+ () => (/* @__PURE__ */ new Date()).getFullYear() - (l ?? 95),
31
+ [l]
32
+ ), T = r(
33
+ () => (/* @__PURE__ */ new Date()).getFullYear() + (d ?? 15),
34
+ [d]
35
+ ), n = r(
36
+ () => !m && F(t, e) !== void 0,
37
+ [t, e, m]
38
+ ), _ = r(() => ({
39
+ valid: !n,
40
+ helperTextPersistent: n || f,
41
+ helperText: (() => {
39
42
  if (t && t[e]) {
40
- const r = t[e]?.message;
41
- return r || "Invalid input";
43
+ const o = t[e]?.message;
44
+ return o || "Invalid input";
42
45
  }
43
- return o;
44
- };
45
- return {
46
- valid: !a(e).invalid,
47
- helperTextPersistent: t !== void 0 || n,
48
- helperText: s(),
49
- className: `${d.fromPickerText} ${a(e).invalid ? d.invalid : ""}`
50
- };
51
- }, [t, o, e, n, a]);
52
- return /* @__PURE__ */ l(
53
- I,
46
+ return u;
47
+ })(),
48
+ className: `${c.fromPickerText} ${n ? c.invalid : ""}`
49
+ }), [t, u, e, f, n]);
50
+ return /* @__PURE__ */ s(
51
+ q,
54
52
  {
55
53
  name: e,
56
- rules: {
57
- required: n ? {
58
- value: n,
59
- message: o
60
- } : void 0
61
- },
54
+ rules: i,
62
55
  control: g,
63
- render: ({ field: { onChange: s, ...r } }) => /* @__PURE__ */ l("div", { className: d.datePickerContainer, children: /* @__PURE__ */ l(
64
- F,
56
+ render: ({ field: { ref: C, onChange: o, value: p, ...D } }) => /* @__PURE__ */ s("div", { className: c.datePickerContainer, children: /* @__PURE__ */ s(
57
+ S,
65
58
  {
66
- ...r,
67
- className: P ?? "",
68
- value: _,
59
+ ...D,
60
+ className: k ?? "",
61
+ value: p,
69
62
  variant: "outlined",
70
63
  label: x,
71
- format: (i) => i ? i.toLocaleDateString(D ?? "en") : "",
64
+ format: (a) => a ? a.toLocaleDateString(P ?? "en") : "",
72
65
  disableClearing: !0,
73
- onChange: (i) => {
74
- s(i?.toISOString());
66
+ onSelect: (a) => {
67
+ o(a);
75
68
  },
76
- minQuickSelectYear: C,
77
- maxQuickSelectYear: h,
69
+ minQuickSelectYear: v,
70
+ maxQuickSelectYear: T,
78
71
  readOnly: !0,
79
- textfieldProps: w
72
+ textfieldProps: _
80
73
  }
81
74
  ) })
82
75
  }
83
76
  );
84
77
  };
85
78
  export {
86
- H as FormDatePicker
79
+ I as FormDatePicker
87
80
  };
@@ -1,15 +1,15 @@
1
- import { jsx as o } from "react/jsx-runtime";
2
- import { r as n, s as a, f as l } from "../../../react.esm-BeDwcQWb.js";
3
- import { FormDatePicker as s } from "./FormDatePicker.js";
4
- import { v as e, d as m, t as c, g as r } from "../../../vi.JYQecGiw-BbUbJcT8.js";
5
- const f = e.fn();
1
+ import { jsx as n } from "react/jsx-runtime";
2
+ import { r as s, s as a, f as m } from "../../../react.esm-BeDwcQWb.js";
3
+ import { FormDatePicker as c } from "./FormDatePicker.js";
4
+ import { v as e, d as f, t as l, g as r } from "../../../vi.JYQecGiw-BbUbJcT8.js";
5
+ const i = e.fn();
6
6
  e.mock("react-hook-form", () => ({
7
7
  ...e.importActual("react-hook-form"),
8
8
  Controller: ({
9
9
  render: t
10
10
  }) => t({
11
11
  field: {
12
- onChange: f
12
+ onChange: i
13
13
  }
14
14
  }),
15
15
  useFormContext: () => ({
@@ -39,18 +39,18 @@ e.mock("react-hook-form", () => ({
39
39
  getFieldState: e.fn().mockReturnValue({ invalid: !1 })
40
40
  })
41
41
  }));
42
- m("FormDatePicker Test", () => {
43
- c("render FormDatePicker", () => {
44
- n(
45
- /* @__PURE__ */ o(s, { propertyName: "birthday", label: "user.date-of-birth", maxRangeYear: 0 })
42
+ f("FormDatePicker Test", () => {
43
+ l("render FormDatePicker", () => {
44
+ s(
45
+ /* @__PURE__ */ n(c, { propertyName: "birthday", label: "user.date-of-birth", maxRangeYear: 0 })
46
46
  );
47
47
  const t = a.getByRole("textbox");
48
- r(t).toBeInTheDocument(), r(t).toBeInstanceOf(HTMLInputElement), r(["7.8.2024", "8/7/2024"]).toContainEqual(t.value), r(a.getByText("user.date-of-birth")).toBeInTheDocument();
49
- }), c("change FormDatePicker value", () => {
50
- n(
51
- /* @__PURE__ */ o(s, { propertyName: "birthday", label: "user.date-of-birth", maxRangeYear: 0 })
48
+ r(t).toBeInTheDocument(), r(t).toBeInstanceOf(HTMLInputElement), r(a.getByText("user.date-of-birth")).toBeInTheDocument();
49
+ }), l("change FormDatePicker value", () => {
50
+ s(
51
+ /* @__PURE__ */ n(c, { propertyName: "birthday", label: "user.date-of-birth", maxRangeYear: 0 })
52
52
  );
53
- const t = a.getByRole("textbox");
54
- l.change(t, { target: { value: "1.1.24" } });
53
+ const t = a.getByRole("textbox"), o = /* @__PURE__ */ new Date();
54
+ m.change(t, { target: { value: o } }), r(t.getAttribute("value")).toBe(o.toString());
55
55
  });
56
56
  });
@@ -1,9 +1,10 @@
1
1
  /// <reference types="react" />
2
- import { FieldValues, Path } from 'react-hook-form';
2
+ import { SelectProps } from '@element/react-components';
3
+ import { FieldPath, FieldValues, Path, RegisterOptions } from 'react-hook-form';
3
4
  /**
4
5
  * a generic FormSelector property type. the generic T represents the interface, which is used in the form, like {@link CreateOrganizationDto}
5
6
  */
6
- type FormSelectProps<T extends FieldValues> = {
7
+ type FormSelectProps<T extends FieldValues> = Omit<SelectProps, 'className' | 'clearable' | 'disabled' | 'helperText' | 'helperTextPersistent' | 'hidden' | 'hoisted' | 'label' | 'menuMaxHeight' | 'multi' | 'multiSelect' | 'noOptionsMessage' | 'onChange' | 'options' | 'searchable' | 'textKey' | 'valid' | 'value' | 'valueKey' | 'variant'> & {
7
8
  /** the current character count */
8
9
  propertyName: Path<T>;
9
10
  /** the label of the select */
@@ -12,10 +13,6 @@ type FormSelectProps<T extends FieldValues> = {
12
13
  disabled?: boolean;
13
14
  /** if the select should be hidden */
14
15
  hidden?: boolean;
15
- /** if the select is required */
16
- required?: boolean;
17
- /** the text which is shown if the select is required and not filled */
18
- requiredText?: string;
19
16
  /** if true the select dropdown is rendered outside of the parent. Useful for when the select needs to render in a parent with "overflow: hidden" and the dropdown will cut on the component border. */
20
17
  hoisted?: boolean;
21
18
  /** the key of the value attribute of the select, default: value */
@@ -30,6 +27,13 @@ type FormSelectProps<T extends FieldValues> = {
30
27
  clearable?: boolean;
31
28
  /** a message if no options a present */
32
29
  noOptionsMessage?: string;
30
+ /**
31
+ * the rules for the picker
32
+ * @default undefined
33
+ * @example { required: { value: true, message: 'This field is required' } }
34
+ * @example { validate: (value) => value === 'admin' }
35
+ */
36
+ rules?: Omit<RegisterOptions<T, FieldPath<T>>, 'disabled' | 'valueAsNumber' | 'valueAsDate' | 'setValueAs'> | undefined;
33
37
  };
34
38
  /**
35
39
  * the base option type of the selector, you can also use your own custom objects, by defining the {@link FormSelectProps} valueKey and textKey
@@ -50,12 +54,11 @@ export type FormSelectOption = {
50
54
  * @param options the options of the select
51
55
  * @param noOptionsMessage a message if no options a present
52
56
  * @param propertyName the name of the property in the form
53
- * @param required if the select is required
54
- * @param requiredText the text which is shown if the select is required and not filled
55
57
  * @param textKey the key of the text attribute of the select, default: text
56
58
  * @param valueKey the key of the value attribute of the select, default: value
59
+ * @param rules the rules for the select example _{ required: { value: true, message: 'This field is required' } }_
57
60
  *
58
61
  * @returns
59
62
  */
60
- export declare const FormSelect: <T extends FieldValues>({ propertyName, label, required, requiredText, options, multi, disabled, hoisted, hidden, valueKey, textKey, clearable, noOptionsMessage, }: FormSelectProps<T>) => import("react/jsx-runtime").JSX.Element;
63
+ export declare const FormSelect: <T extends FieldValues>({ propertyName, label, options, multi, disabled, hoisted, hidden, valueKey, textKey, clearable, noOptionsMessage, rules, ...props }: FormSelectProps<T>) => import("react/jsx-runtime").JSX.Element;
61
64
  export {};
@@ -1,73 +1,85 @@
1
1
  import "../../../assets/FormSelect.css";
2
- import { jsx as o, Fragment as H } from "react/jsx-runtime";
2
+ import { jsx as i, Fragment as I } from "react/jsx-runtime";
3
3
  import { Select as P } from "@element/react-components";
4
- import { g as S } from "../../../get-Dyz8NMrE.js";
5
- import { useState as V, useMemo as k, useEffect as w } from "react";
6
- import { useFormContext as z, Controller as u } from "react-hook-form";
7
- const A = "_invalid_1vo1h_1", B = "_formSelect_1vo1h_5", D = "_formSelectContainer_1vo1h_9", f = {
8
- invalid: A,
9
- formSelect: B,
10
- formSelectContainer: D
11
- }, U = ({
12
- propertyName: t,
13
- label: _,
14
- required: c = !1,
15
- requiredText: h,
16
- options: l,
17
- multi: s = !1,
18
- disabled: x = !1,
4
+ import { g as V } from "../../../get-Dyz8NMrE.js";
5
+ import { useMemo as m } from "react";
6
+ import { useFormContext as k, Controller as _ } from "react-hook-form";
7
+ const w = "_invalid_1vo1h_1", z = "_formSelect_1vo1h_5", A = "_formSelectContainer_1vo1h_9", d = {
8
+ invalid: w,
9
+ formSelect: z,
10
+ formSelectContainer: A
11
+ }, Q = ({
12
+ propertyName: r,
13
+ label: g,
14
+ options: s,
15
+ multi: l = !1,
16
+ disabled: h = !1,
19
17
  hoisted: v = !1,
20
18
  hidden: C = !1,
21
- valueKey: r = "value",
22
- textKey: g = "text",
23
- clearable: F = !1,
24
- noOptionsMessage: E = ""
19
+ valueKey: t = "value",
20
+ textKey: q = "text",
21
+ clearable: T = !1,
22
+ noOptionsMessage: F = "",
23
+ rules: n,
24
+ ...H
25
25
  }) => {
26
26
  const {
27
- control: m,
28
- formState: { errors: n }
29
- } = z(), [a, M] = V(!0), T = k(() => S(n, t) !== void 0, [n, t]);
30
- return w(() => {
31
- M(S(n, t) === void 0);
32
- }, [n, T, t]), C ? /* @__PURE__ */ o(u, { name: t, control: m, render: () => /* @__PURE__ */ o(H, {}) }) : /* @__PURE__ */ o("div", { className: f.formSelectContainer, children: /* @__PURE__ */ o(
33
- u,
27
+ control: u,
28
+ formState: { errors: o, isValidating: S }
29
+ } = k(), c = m(
30
+ () => !S && V(o, r) !== void 0,
31
+ [o, r, S]
32
+ ), M = m(
33
+ () => n?.required?.value,
34
+ [n]
35
+ ), $ = m(
36
+ () => n?.required?.message,
37
+ [n]
38
+ ), b = () => {
39
+ if (o && o[r]) {
40
+ const a = o[r]?.message;
41
+ return a || "Invalid input";
42
+ }
43
+ return $;
44
+ };
45
+ return C ? /* @__PURE__ */ i(_, { name: r, control: u, render: () => /* @__PURE__ */ i(I, {}) }) : /* @__PURE__ */ i("div", { className: d.formSelectContainer, children: /* @__PURE__ */ i(
46
+ _,
34
47
  {
35
- name: t,
36
- rules: {
37
- required: c
38
- },
39
- control: m,
40
- render: ({ field: { ref: G, onChange: i, value: d, ...$ } }) => {
41
- const b = s ? l?.filter(
42
- (e) => d?.includes(e[r])
43
- ) : l?.find((e) => e[r] === d);
44
- return /* @__PURE__ */ o(
48
+ name: r,
49
+ rules: n,
50
+ control: u,
51
+ render: ({ field: { ref: a, onChange: f, value: x, ...j } }) => {
52
+ const p = l ? s?.filter(
53
+ (e) => x?.includes(e[t])
54
+ ) : s?.find((e) => e[t] === x);
55
+ return /* @__PURE__ */ i(
45
56
  P,
46
57
  {
47
- ...$,
48
- value: b,
49
- label: _,
58
+ ...j,
59
+ ...H,
60
+ value: p,
61
+ label: g,
50
62
  variant: "outlined",
51
- helperText: h,
52
- helperTextPersistent: c,
53
- options: l,
54
- valid: a,
55
- multiSelect: s,
56
- className: `${f.formSelect} ${a ? "" : f.invalid}`,
57
- disabled: x,
58
- valueKey: r,
59
- textKey: g,
63
+ helperText: b(),
64
+ helperTextPersistent: M || c,
65
+ options: s,
66
+ valid: !c,
67
+ multiSelect: l,
68
+ className: `${d.formSelect} ${c ? d.invalid : ""}`,
69
+ disabled: h,
70
+ valueKey: t,
71
+ textKey: q,
60
72
  onChange: (e) => {
61
73
  if (e == null)
62
- return i(e);
63
- if (s)
64
- return i(e.map((j) => j[r]));
65
- i(e[r]);
74
+ return f(e);
75
+ if (l)
76
+ return f(e.map((E) => E[t]));
77
+ f(e[t]);
66
78
  },
67
79
  hoisted: v,
68
- noOptionsMessage: E,
80
+ noOptionsMessage: F,
69
81
  searchable: !0,
70
- clearable: F,
82
+ clearable: T,
71
83
  menuMaxHeight: "15rem"
72
84
  }
73
85
  );
@@ -76,5 +88,5 @@ const A = "_invalid_1vo1h_1", B = "_formSelect_1vo1h_5", D = "_formSelectContain
76
88
  ) });
77
89
  };
78
90
  export {
79
- U as FormSelect
91
+ Q as FormSelect
80
92
  };
@@ -1,14 +1,16 @@
1
- import { jsx as a } from "react/jsx-runtime";
2
- import { r as s, s as r } from "../../../react.esm-BeDwcQWb.js";
1
+ import { jsx as s } from "react/jsx-runtime";
2
+ import { r as a, s as r } from "../../../react.esm-BeDwcQWb.js";
3
3
  import { FormSelect as c } from "./FormSelect.js";
4
4
  import { v as e, d as l, t as u, g as t } from "../../../vi.JYQecGiw-BbUbJcT8.js";
5
- const i = e.fn();
5
+ const m = e.fn();
6
6
  e.mock("react-hook-form", () => ({
7
7
  ...e.importActual("react-hook-form"),
8
- Controller: ({ render: o }) => o({
8
+ Controller: ({
9
+ render: o
10
+ }) => o({
9
11
  field: {
10
12
  ref: void 0,
11
- onChange: i,
13
+ onChange: m,
12
14
  value: "3"
13
15
  }
14
16
  }),
@@ -45,13 +47,12 @@ l("FormSelect Test", () => {
45
47
  { id: "3", value: "Afar", description: "" }
46
48
  ];
47
49
  u("render FormSelect", () => {
48
- s(
49
- /* @__PURE__ */ a(
50
+ a(
51
+ /* @__PURE__ */ s(
50
52
  c,
51
53
  {
52
54
  label: "address.country",
53
- required: !0,
54
- requiredText: "required",
55
+ rules: { required: { value: !0, message: "required" } },
55
56
  propertyName: "countryId",
56
57
  options: o,
57
58
  valueKey: "id",
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hortiview/shared-components",
3
3
  "description": "This is a shared component library. It should used in the HortiView platform and its modules.",
4
- "version": "0.0.4804",
4
+ "version": "0.0.4870",
5
5
  "type": "module",
6
6
  "repository": "https://dev.azure.com/sdundc/HV%20Platform/_git/HortiView-Frontend-Shared",
7
7
  "author": "Falk Menge <falk.menge.ext@bayer.com>",