@visns-studio/visns-components 4.10.23 → 4.10.24

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
@@ -77,7 +77,7 @@
77
77
  "react-dom": "^17.0.0 || ^18.0.0"
78
78
  },
79
79
  "name": "@visns-studio/visns-components",
80
- "version": "4.10.23",
80
+ "version": "4.10.24",
81
81
  "description": "Various packages to assist in the development of our Custom Applications.",
82
82
  "main": "src/index.js",
83
83
  "files": [
@@ -1030,6 +1030,16 @@ function Field({
1030
1030
  }
1031
1031
  dataOptions={dataOptions}
1032
1032
  style={style}
1033
+ isCreatable={
1034
+ settings.hasOwnProperty('isCreatable')
1035
+ ? settings.isCreatable
1036
+ : false
1037
+ }
1038
+ creatableConfig={
1039
+ settings.hasOwnProperty('creatableConfig')
1040
+ ? settings.creatableConfig
1041
+ : {}
1042
+ }
1033
1043
  />
1034
1044
  {settings.create && (
1035
1045
  <button
@@ -1,80 +1,73 @@
1
1
  import React, { useEffect, useState } from 'react';
2
+ import CustomFetch from './Fetch';
2
3
  import SelectList from './SelectList';
3
4
  import Select, { createFilter } from 'react-select';
5
+ import CreatableSelect from 'react-select/creatable';
4
6
  import styles from './styles/AsyncSelect.module.scss';
5
7
 
6
8
  function MultiSelect({
7
9
  className,
8
- inputValue,
10
+ inputValue = [], // Default to an empty array
9
11
  multi,
10
12
  onChange,
11
- options,
13
+ options = [], // Default to an empty array
12
14
  placeholder,
13
15
  settings,
14
16
  style,
17
+ isCreatable = false,
18
+ creatableConfig = {},
15
19
  }) {
16
20
  const [selectOptions, setSelectOptions] = useState([]);
17
21
  const [selectValue, setSelectValue] = useState([]);
18
22
 
19
23
  useEffect(() => {
20
- let _options = [];
21
-
22
- if (options.length > 0) {
23
- options.forEach((a) => {
24
- let labelValue =
25
- a.label || a.name || a.description || 'Unknown';
26
- let _optionItem = {
27
- value: a.id,
28
- label: labelValue,
29
- };
30
-
31
- Object.keys(a).forEach((b) => {
32
- if (b !== 'id' && b !== 'label') {
33
- _optionItem = {
34
- ..._optionItem,
35
- [b]: a[b],
36
- };
37
- }
38
- });
39
-
40
- _options.push(_optionItem);
41
- });
42
-
43
- setSelectOptions(_options);
44
- }
24
+ const _options = Array.isArray(options)
25
+ ? options.map((a) => ({
26
+ value: a.id,
27
+ label: a.label || a.name || a.description || 'Unknown',
28
+ ...a,
29
+ }))
30
+ : [];
31
+ setSelectOptions(_options);
45
32
  }, [options]);
46
33
 
47
34
  useEffect(() => {
48
- let _values = [];
49
-
50
- if (inputValue.length > 0) {
51
- inputValue.forEach((a) => {
52
- let labelValue =
53
- a.label || a.name || a.description || 'Unknown';
54
- let _valueItems = {
55
- value: a.id,
56
- label: labelValue,
57
- };
35
+ const _values = Array.isArray(inputValue)
36
+ ? inputValue.map((a) => ({
37
+ value: a.id,
38
+ label: a.label || a.name || a.description || 'Unknown',
39
+ ...a,
40
+ }))
41
+ : [];
42
+ setSelectValue(_values);
43
+ }, [inputValue]);
58
44
 
59
- Object.keys(a).forEach((b) => {
60
- if (b !== 'id' && b !== 'label') {
61
- _valueItems = {
62
- ..._valueItems,
63
- [b]: a[b],
64
- };
65
- }
66
- });
45
+ const handleCreate = async (inputValue) => {
46
+ if (creatableConfig.url && creatableConfig.method) {
47
+ const res = await CustomFetch(
48
+ creatableConfig.url,
49
+ creatableConfig.method,
50
+ { label: inputValue }
51
+ );
67
52
 
68
- _values.push(_valueItems);
69
- });
53
+ if (res && res.data) {
54
+ const newOption = {
55
+ value: res.data.id,
56
+ label: res.data.label || inputValue,
57
+ ...res.data,
58
+ };
70
59
 
71
- setSelectValue(_values);
60
+ setSelectOptions((prevOptions) => [...prevOptions, newOption]);
61
+ setSelectValue((prevValues) => [...prevValues, newOption]);
62
+ }
72
63
  }
73
- }, [inputValue]);
64
+ };
65
+
66
+ const SelectComponent = isCreatable ? CreatableSelect : Select;
74
67
 
75
68
  return (
76
- <Select
77
- closeMenuOnSelect={multi ? false : true}
69
+ <SelectComponent
70
+ closeMenuOnSelect={!multi}
78
71
  isClearable
79
72
  isSearchable
80
73
  isMulti={multi}
@@ -101,6 +94,7 @@ function MultiSelect({
101
94
  }),
102
95
  }}
103
96
  value={selectValue}
97
+ onCreateOption={handleCreate}
104
98
  placeholder={placeholder ? placeholder : 'Select...'}
105
99
  />
106
100
  );