@visns-studio/visns-components 5.5.0 → 5.5.2

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/package.json CHANGED
@@ -81,7 +81,7 @@
81
81
  "react-dom": "^17.0.0 || ^18.0.0"
82
82
  },
83
83
  "name": "@visns-studio/visns-components",
84
- "version": "5.5.0",
84
+ "version": "5.5.2",
85
85
  "description": "Various packages to assist in the development of our Custom Applications.",
86
86
  "main": "src/index.js",
87
87
  "files": [
@@ -123,16 +123,30 @@ function Field({
123
123
  if (settings.type === 'text' && settings.url) {
124
124
  try {
125
125
  const res = await CustomFetch(settings.url, 'POST', {});
126
+
126
127
  if (res.data) {
127
- setAutoValue(res.data);
128
+ // If settings.autoValueKey exists, use res.data[settings.autoValueKey], otherwise use res.data directly
129
+ const valueToUse = settings.autoValueKey
130
+ ? res.data[settings.autoValueKey]
131
+ : res.data;
132
+
133
+ setAutoValue(valueToUse);
128
134
  // Simulate an onChange event to update the form data
129
135
  const simulatedEvent = {
130
136
  target: {
131
137
  dataset: { name: settings.id },
132
- value: res.data,
138
+ value: valueToUse,
133
139
  },
134
140
  };
135
141
  onChange(simulatedEvent);
142
+
143
+ // Also directly update the form data to ensure it's synchronized
144
+ if (setFormData) {
145
+ setFormData((prevState) => ({
146
+ ...prevState,
147
+ [settings.id]: valueToUse,
148
+ }));
149
+ }
136
150
  }
137
151
  } catch (error) {
138
152
  console.error('Error fetching auto value:', error);
@@ -141,7 +155,7 @@ function Field({
141
155
  };
142
156
 
143
157
  fetchAutoValue();
144
- }, [settings.url, settings.type, settings.id]);
158
+ }, [settings.url, settings.type, settings.id, settings.autoValueKey]);
145
159
 
146
160
  useEffect(() => {
147
161
  const fetchOptions = () => {
@@ -1720,6 +1734,7 @@ function Field({
1720
1734
  <input
1721
1735
  type={settings.type}
1722
1736
  data-name={settings.id}
1737
+ data-autovalue={autoValue || ''}
1723
1738
  className={inputClass[settings.id]}
1724
1739
  placeholder=" "
1725
1740
  onChange={onChange}
@@ -641,8 +641,16 @@ function Form({
641
641
 
642
642
  // Check if the item is required
643
643
  if (item.required) {
644
- // Check if formData[item.id] is null, undefined, or an empty string
645
- if (!formData[item.id]) {
644
+ // Find the corresponding Field component to check for auto values
645
+ const fieldElement = document.querySelector(
646
+ `[data-name="${item.id}"]`
647
+ );
648
+ const autoValue = fieldElement
649
+ ? fieldElement.getAttribute('data-autovalue')
650
+ : null;
651
+
652
+ // Check if formData[item.id] is null, undefined, or an empty string AND there's no auto value
653
+ if (!formData[item.id] && !autoValue) {
646
654
  // Check for required_check dependency
647
655
  if (
648
656
  item.required_check &&
@@ -673,6 +681,22 @@ function Form({
673
681
  setInputClass(_inputClass);
674
682
  }
675
683
 
684
+ // Before validation, update formData with any auto values from the form fields
685
+ const formFields = document.querySelectorAll('[data-name]');
686
+ let updatedFormData = { ...formData };
687
+
688
+ formFields.forEach((field) => {
689
+ const fieldName = field.getAttribute('data-name');
690
+ const autoValue = field.getAttribute('data-autovalue');
691
+
692
+ if (fieldName && autoValue && !updatedFormData[fieldName]) {
693
+ updatedFormData[fieldName] = autoValue;
694
+ }
695
+ });
696
+
697
+ // Update the form data state with the auto values
698
+ setFormData(updatedFormData);
699
+
676
700
  if (validation) {
677
701
  toast.error(<div>{parse(validation)}</div>);
678
702
  } else {
@@ -836,16 +860,19 @@ function Form({
836
860
  }
837
861
 
838
862
  // Transform keys with dots into nested objects
839
- const payload = Object.keys(formData).reduce((acc, key) => {
840
- if (key.includes('.')) {
841
- const [root, sub] = key.split('.');
842
- if (!acc[root]) acc[root] = {};
843
- acc[root][sub] = formData[key];
844
- } else {
845
- acc[key] = formData[key];
846
- }
847
- return acc;
848
- }, {});
863
+ const payload = Object.keys(updatedFormData).reduce(
864
+ (acc, key) => {
865
+ if (key.includes('.')) {
866
+ const [root, sub] = key.split('.');
867
+ if (!acc[root]) acc[root] = {};
868
+ acc[root][sub] = updatedFormData[key];
869
+ } else {
870
+ acc[key] = updatedFormData[key];
871
+ }
872
+ return acc;
873
+ },
874
+ {}
875
+ );
849
876
 
850
877
  const res = await CustomFetch(url, method, payload);
851
878
 
@@ -1273,21 +1300,32 @@ function Form({
1273
1300
  const showConditions = field.show || [];
1274
1301
 
1275
1302
  showConditions.forEach((showObject) => {
1276
- const { id, value } = showObject;
1303
+ const { id, value, self } = showObject;
1277
1304
 
1278
- if (id !== '' && value.length > 0) {
1305
+ if (id != '' && value.length > 0) {
1279
1306
  const targetField = formSettings.fields.find(
1280
1307
  (f) => f.id === id
1281
1308
  );
1282
1309
 
1283
1310
  if (targetField) {
1284
- const valueChecker = formData[field.id];
1311
+ let valueChecker = null;
1312
+ if (self) {
1313
+ valueChecker = formData[targetField.id];
1314
+ } else {
1315
+ valueChecker = formData[field.id];
1316
+ }
1285
1317
 
1286
- if (valueChecker) {
1287
- const shouldShow =
1288
- valueChecker && value.includes(valueChecker);
1318
+ if (
1319
+ valueChecker !== undefined &&
1320
+ valueChecker !== null
1321
+ ) {
1322
+ const shouldShow = value.includes(valueChecker);
1289
1323
 
1290
- targetField.hide = !shouldShow;
1324
+ if (self) {
1325
+ field.hide = !shouldShow;
1326
+ } else {
1327
+ targetField.hide = !shouldShow;
1328
+ }
1291
1329
 
1292
1330
  if (updateForm) {
1293
1331
  updateForm((prevState) => ({