@nr1e/qwik-ui 2.0.6 → 2.0.8

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.
@@ -16,6 +16,8 @@ const TextField = qwik.component$((props) => {
16
16
  }
17
17
  }
18
18
  const touched = qwik.useSignal(false);
19
+ const originalTriggerResetValue = props.triggerReset?.value;
20
+ const originalTriggerValidationValue = props.triggerValidation?.value;
19
21
  qwik.useTask$(({ track }) => {
20
22
  if (props.error && typeof props.error !== "string") {
21
23
  track(() => error.value);
@@ -67,9 +69,11 @@ const TextField = qwik.component$((props) => {
67
69
  qwik.useTask$(async ({ track }) => {
68
70
  if (props.triggerReset) {
69
71
  track(() => props.triggerReset?.value);
70
- value.value = originalValue;
71
- error.value = void 0;
72
- touched.value = false;
72
+ if (originalTriggerResetValue !== props.triggerReset.value) {
73
+ value.value = originalValue;
74
+ error.value = void 0;
75
+ touched.value = false;
76
+ }
73
77
  }
74
78
  });
75
79
  const validate = qwik.$(async () => {
@@ -91,16 +95,22 @@ const TextField = qwik.component$((props) => {
91
95
  return;
92
96
  }
93
97
  }
98
+ if (value.value !== result.output) {
99
+ value.value = result.output;
100
+ }
94
101
  }
95
102
  if (props.validate$) {
96
103
  const result = await props.validate$(value.value);
97
- if (result) {
104
+ if (!result.success) {
98
105
  if (props.valid) {
99
106
  props.valid.value = false;
100
107
  }
101
- error.value = result;
108
+ error.value = result.error;
102
109
  return;
103
110
  }
111
+ if (value.value !== result.output) {
112
+ value.value = result.output;
113
+ }
104
114
  }
105
115
  if (props.valid) {
106
116
  props.valid.value = true;
@@ -110,8 +120,10 @@ const TextField = qwik.component$((props) => {
110
120
  qwik.useTask$(async ({ track }) => {
111
121
  if (props.triggerValidation) {
112
122
  track(() => props.triggerValidation?.value);
113
- touched.value = true;
114
- await validate();
123
+ if (props.triggerValidation.value !== originalTriggerValidationValue) {
124
+ touched.value = true;
125
+ await validate();
126
+ }
115
127
  }
116
128
  });
117
129
  return /* @__PURE__ */ jsxRuntime.jsxs("div", {
@@ -134,7 +146,7 @@ const TextField = qwik.component$((props) => {
134
146
  name: "left"
135
147
  }),
136
148
  /* @__PURE__ */ jsxRuntime.jsx("input", {
137
- type: "text",
149
+ type: props.type || "text",
138
150
  ...props.id && {
139
151
  id: props.id
140
152
  },
@@ -153,6 +165,7 @@ const TextField = qwik.component$((props) => {
153
165
  touched.value = true;
154
166
  value.value = target.value;
155
167
  await validate();
168
+ target.value = value.value ?? "";
156
169
  if (props.onBlur$) {
157
170
  props.onBlur$(e, target.value, error);
158
171
  }
@@ -14,6 +14,8 @@ const TextField = component$((props) => {
14
14
  }
15
15
  }
16
16
  const touched = useSignal(false);
17
+ const originalTriggerResetValue = props.triggerReset?.value;
18
+ const originalTriggerValidationValue = props.triggerValidation?.value;
17
19
  useTask$(({ track }) => {
18
20
  if (props.error && typeof props.error !== "string") {
19
21
  track(() => error.value);
@@ -65,9 +67,11 @@ const TextField = component$((props) => {
65
67
  useTask$(async ({ track }) => {
66
68
  if (props.triggerReset) {
67
69
  track(() => props.triggerReset?.value);
68
- value.value = originalValue;
69
- error.value = void 0;
70
- touched.value = false;
70
+ if (originalTriggerResetValue !== props.triggerReset.value) {
71
+ value.value = originalValue;
72
+ error.value = void 0;
73
+ touched.value = false;
74
+ }
71
75
  }
72
76
  });
73
77
  const validate = $(async () => {
@@ -89,16 +93,22 @@ const TextField = component$((props) => {
89
93
  return;
90
94
  }
91
95
  }
96
+ if (value.value !== result.output) {
97
+ value.value = result.output;
98
+ }
92
99
  }
93
100
  if (props.validate$) {
94
101
  const result = await props.validate$(value.value);
95
- if (result) {
102
+ if (!result.success) {
96
103
  if (props.valid) {
97
104
  props.valid.value = false;
98
105
  }
99
- error.value = result;
106
+ error.value = result.error;
100
107
  return;
101
108
  }
109
+ if (value.value !== result.output) {
110
+ value.value = result.output;
111
+ }
102
112
  }
103
113
  if (props.valid) {
104
114
  props.valid.value = true;
@@ -108,8 +118,10 @@ const TextField = component$((props) => {
108
118
  useTask$(async ({ track }) => {
109
119
  if (props.triggerValidation) {
110
120
  track(() => props.triggerValidation?.value);
111
- touched.value = true;
112
- await validate();
121
+ if (props.triggerValidation.value !== originalTriggerValidationValue) {
122
+ touched.value = true;
123
+ await validate();
124
+ }
113
125
  }
114
126
  });
115
127
  return /* @__PURE__ */ jsxs("div", {
@@ -132,7 +144,7 @@ const TextField = component$((props) => {
132
144
  name: "left"
133
145
  }),
134
146
  /* @__PURE__ */ jsx("input", {
135
- type: "text",
147
+ type: props.type || "text",
136
148
  ...props.id && {
137
149
  id: props.id
138
150
  },
@@ -151,6 +163,7 @@ const TextField = component$((props) => {
151
163
  touched.value = true;
152
164
  value.value = target.value;
153
165
  await validate();
166
+ target.value = value.value ?? "";
154
167
  if (props.onBlur$) {
155
168
  props.onBlur$(e, target.value, error);
156
169
  }
@@ -1,7 +1,32 @@
1
1
  import { QRL, Signal } from '@builder.io/qwik';
2
2
  import * as v from 'valibot';
3
+ /**
4
+ * The type of the input field.
5
+ */
6
+ export type TextFieldType = 'text' | 'date' | 'email' | 'number' | 'tel' | 'url';
7
+ /**
8
+ * The result of validating a form input.
9
+ */
10
+ export type TextFieldValidationResult = {
11
+ /**
12
+ * True if validation was successful. False if validation failed.
13
+ */
14
+ success: boolean;
15
+ /**
16
+ * The error message if validation failed.
17
+ */
18
+ error?: string;
19
+ /**
20
+ * The normalized value of the input after successful validation.
21
+ */
22
+ output: string | null | undefined;
23
+ };
24
+ /**
25
+ * Properties for the TextField component.
26
+ */
3
27
  export interface TextFieldProps {
4
28
  id?: string;
29
+ type?: TextFieldType;
5
30
  label?: string;
6
31
  name?: string;
7
32
  value?: string | null | Signal<string | null | undefined>;
@@ -10,19 +35,47 @@ export interface TextFieldProps {
10
35
  maxLength?: number;
11
36
  required?: boolean;
12
37
  disabled?: boolean | Signal<boolean>;
38
+ /**
39
+ * Called when the input loses focus.
40
+ */
13
41
  onBlur$?: QRL<(event: FocusEvent, value: string, error: Signal<string | undefined>) => void>;
42
+ /**
43
+ * Called when the input value changes.
44
+ */
14
45
  onInput$?: QRL<(event: InputEvent, value: string, error: Signal<string | undefined>) => void>;
46
+ /**
47
+ * Called on blur or input events
48
+ */
15
49
  onEvent$?: QRL<(type: 'blur' | 'input', event: FocusEvent | InputEvent, value: string, error: Signal<string | undefined>) => void>;
16
- validate$?: QRL<(value: string | null | undefined) => string | undefined>;
50
+ /**
51
+ * Validates the form input. This is called on input and blur events.
52
+ */
53
+ validate$?: QRL<(value: string | null | undefined) => TextFieldValidationResult>;
54
+ /**
55
+ * Called to validate the form input. This is called on input and blur events
56
+ * and is an alternative to the validate$ prop.
57
+ */
17
58
  schema$?: QRL<() => v.BaseSchema<any, any, any>>;
59
+ /**
60
+ * An optional signal that indicates whether the input is valid.
61
+ */
18
62
  valid?: Signal<undefined | boolean>;
19
63
  /**
20
64
  * Increment the value of this signal to reset the input to its original value.
21
65
  */
22
66
  triggerReset?: Signal<number>;
23
67
  /**
24
- * Increment the value of this signal to force validation to execute.
68
+ * Increment the value of this signal to force validation to execute. Be
69
+ * aware that this will also cause the input to be touched, which will
70
+ * display the error message if one exists.
25
71
  */
26
72
  triggerValidation?: Signal<number>;
27
73
  }
74
+ /**
75
+ * A standardized text input field meant to be used independently or with Qwik
76
+ * Modular Forms.
77
+ *
78
+ * Be aware that the normalized value of the input from validation is reflected
79
+ * back into the text field automatically on blur.
80
+ */
28
81
  export declare const TextField: import("@builder.io/qwik").Component<TextFieldProps>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nr1e/qwik-ui",
3
- "version": "2.0.6",
3
+ "version": "2.0.8",
4
4
  "description": "NR1E Qwik UI Library",
5
5
  "author": "NR1E, Inc.",
6
6
  "publishConfig": {