@ttoss/forms 0.15.12 → 0.15.14

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.
package/README.md CHANGED
@@ -117,4 +117,35 @@ return (
117
117
  );
118
118
  ```
119
119
 
120
+ When your Select options depends on fetched values, the manual defaultValue setting is required.
121
+
122
+ ```tsx
123
+ const RenderForm = () => {
124
+ const formMethods = useForm();
125
+ const { resetField } = formMethods;
126
+ const [formOptions, setFormOptions] = useState<
127
+ {
128
+ value: string;
129
+ label: string;
130
+ }[]
131
+ >([]);
132
+
133
+ useEffect(() => {
134
+ // some fetch operation here
135
+
136
+ setFormOptions(RADIO_OPTIONS);
137
+
138
+ // fetch are side effects, so, if the options depends on fetch and have a default value, the field should be reseted in the effect
139
+ resetField('car', { defaultValue: 'Ferrari' });
140
+ }, []);
141
+
142
+ return (
143
+ <Form {...formMethods} onSubmit={onSubmit}>
144
+ <FormFieldSelect name="car" label="Cars" options={formOptions} />
145
+ <Button type="submit">Submit</Button>
146
+ </Form>
147
+ );
148
+ };
149
+ ```
150
+
120
151
  > NOTE: You can also use yup and all of API from react-hook-form importing `import { yup, useForm } from @ttoss/forms`
package/dist/esm/index.js CHANGED
@@ -270,14 +270,15 @@ var checkDefaultValue = (options, defaultValue, placeholder) => {
270
270
  });
271
271
  if (placeholder && hasEmptyValue) return "";
272
272
  if (placeholder && !hasEmptyValue) {
273
- options.push({
274
- label: "",
273
+ options.unshift({
274
+ label: placeholder,
275
275
  value: ""
276
276
  });
277
277
  return "";
278
278
  }
279
279
  if (!placeholder && defaultValue) return defaultValue;
280
- return options[0].value;
280
+ if (options.length === 0) return "";
281
+ return options?.[0]?.value;
281
282
  };
282
283
  var FormFieldSelect = ({
283
284
  label,
package/dist/index.js CHANGED
@@ -321,14 +321,15 @@ var checkDefaultValue = (options, defaultValue, placeholder) => {
321
321
  });
322
322
  if (placeholder && hasEmptyValue) return "";
323
323
  if (placeholder && !hasEmptyValue) {
324
- options.push({
325
- label: "",
324
+ options.unshift({
325
+ label: placeholder,
326
326
  value: ""
327
327
  });
328
328
  return "";
329
329
  }
330
330
  if (!placeholder && defaultValue) return defaultValue;
331
- return options[0].value;
331
+ if (options.length === 0) return "";
332
+ return options?.[0]?.value;
332
333
  };
333
334
  var FormFieldSelect = ({
334
335
  label,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/forms",
3
- "version": "0.15.12",
3
+ "version": "0.15.14",
4
4
  "license": "UNLICENSED",
5
5
  "author": "ttoss",
6
6
  "contributors": [
@@ -31,7 +31,7 @@
31
31
  "devDependencies": {
32
32
  "@ttoss/config": "^1.29.5",
33
33
  "@ttoss/test-utils": "^1.21.5",
34
- "@ttoss/ui": "^1.34.0",
34
+ "@ttoss/ui": "^1.34.1",
35
35
  "@types/jest": "^29.5.0",
36
36
  "@types/react": "^18.0.37",
37
37
  "jest": "^29.5.0",
@@ -44,5 +44,5 @@
44
44
  "publishConfig": {
45
45
  "access": "public"
46
46
  },
47
- "gitHead": "85c81ad0d34207a3bdaa8cb907619f8e70837094"
47
+ "gitHead": "b473ec0322fbe1a459d81339aab3de60d6bebc9a"
48
48
  }
@@ -22,14 +22,15 @@ const checkDefaultValue = (
22
22
 
23
23
  if (placeholder && hasEmptyValue) return '';
24
24
  if (placeholder && !hasEmptyValue) {
25
- options.push({
26
- label: '',
25
+ options.unshift({
26
+ label: placeholder,
27
27
  value: '',
28
28
  });
29
29
  return '';
30
30
  }
31
31
  if (!placeholder && defaultValue) return defaultValue;
32
- return options[0].value;
32
+ if (options.length === 0) return '';
33
+ return options?.[0]?.value;
33
34
  };
34
35
 
35
36
  export const FormFieldSelect = <