dynamic-mui 1.1.1 → 1.1.3

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.
@@ -30,29 +30,52 @@ const getValue = function () {
30
30
  let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
31
31
  let defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
32
32
  let isMultiple = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
33
- if (isMultiple) {
34
- let separator = ',';
35
- if (defaultValue !== null && defaultValue !== void 0 && defaultValue.includes(',')) separator = ',';else if (defaultValue !== null && defaultValue !== void 0 && defaultValue.includes(';')) separator = ';';
36
- const dValueSet = new Set(defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue.split(separator));
37
- return options === null || options === void 0 ? void 0 : options.filter(_ref => {
33
+ let separator = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : ',';
34
+ try {
35
+ const safeOptions = Array.isArray(options) ? options : [];
36
+ if (isMultiple) {
37
+ let values = [];
38
+ if (Array.isArray(defaultValue)) {
39
+ values = defaultValue;
40
+ } else if (typeof defaultValue === 'string' && defaultValue.trim() !== '') {
41
+ let tempSeparator = separator;
42
+ if (defaultValue.includes(',')) tempSeparator = ',';else if (defaultValue.includes(';')) tempSeparator = ';';
43
+ values = defaultValue.split(tempSeparator).map(v => v.trim());
44
+ }
45
+ const dValueSet = new Set(values);
46
+ return safeOptions.filter(_ref => {
47
+ let {
48
+ value
49
+ } = _ref;
50
+ return dValueSet.has(value);
51
+ });
52
+ }
53
+ if (Array.isArray(defaultValue)) {
54
+ const first = defaultValue[0];
55
+ return safeOptions.find(_ref2 => {
56
+ let {
57
+ value
58
+ } = _ref2;
59
+ return value === first;
60
+ });
61
+ }
62
+ return safeOptions.find(_ref3 => {
38
63
  let {
39
64
  value
40
- } = _ref;
41
- return dValueSet === null || dValueSet === void 0 ? void 0 : dValueSet.has(value);
65
+ } = _ref3;
66
+ return value === defaultValue;
42
67
  });
68
+ } catch (err) {
69
+ // eslint-disable-next-line no-console
70
+ console.error('getValue error:', err);
71
+ return isMultiple ? [] : null;
43
72
  }
44
- return options.find(_ref2 => {
45
- let {
46
- value
47
- } = _ref2;
48
- return value === defaultValue;
49
- });
50
73
  };
51
- function Select(_ref3) {
74
+ function Select(_ref4) {
52
75
  let {
53
76
  attributes,
54
77
  onChange
55
- } = _ref3;
78
+ } = _ref4;
56
79
  const {
57
80
  MuiAttributes = {},
58
81
  options = [],
@@ -60,7 +83,7 @@ function Select(_ref3) {
60
83
  id = '',
61
84
  InputProps = {}
62
85
  } = attributes;
63
- const [value, setValue] = _react.default.useState((attributes === null || attributes === void 0 ? void 0 : attributes.value) && getValue(options, attributes === null || attributes === void 0 ? void 0 : attributes.value, MuiAttributes.multiple));
86
+ const [value, setValue] = _react.default.useState((attributes === null || attributes === void 0 ? void 0 : attributes.value) && getValue(options, attributes === null || attributes === void 0 ? void 0 : attributes.value, MuiAttributes.multiple, attributes === null || attributes === void 0 ? void 0 : attributes.separator));
64
87
  const getMuiAttributes = () => {
65
88
  if (MuiAttributes.multiple) {
66
89
  MuiAttributes.renderOption = function (props) {
@@ -83,20 +106,30 @@ function Select(_ref3) {
83
106
  };
84
107
  const extractValue = option => (option === null || option === void 0 ? void 0 : option.value) || (option === null || option === void 0 ? void 0 : option.title) || (option === null || option === void 0 ? void 0 : option.label);
85
108
  const onChangeEvent = (0, _react.useCallback)((event, newValue) => {
109
+ var _attributes$separator;
86
110
  setValue(newValue);
111
+
112
+ // Use attributes?.separator, default to ';'
113
+ const separator = (_attributes$separator = attributes === null || attributes === void 0 ? void 0 : attributes.separator) !== null && _attributes$separator !== void 0 ? _attributes$separator : ';';
87
114
  if (newValue) {
88
115
  const data = MuiAttributes.multiple ? newValue.map(extractValue) : extractValue(newValue);
116
+ const customValue = MuiAttributes.multiple ? data.join(separator) // <-- use custom separator here
117
+ : data;
89
118
  onChange({
90
119
  id,
91
- value: MuiAttributes.multiple ? data.toString() : data,
120
+ value: customValue,
92
121
  option: newValue
93
122
  });
94
- } else onChange({
95
- id,
96
- value: '',
97
- option: newValue
98
- });
99
- }, []);
123
+ } else {
124
+ onChange({
125
+ id,
126
+ value: '',
127
+ option: newValue
128
+ });
129
+ }
130
+ }, [id, onChange, attributes === null || attributes === void 0 ? void 0 : attributes.separator,
131
+ // so changes to separator update the callback
132
+ MuiAttributes.multiple, extractValue]);
100
133
  return /*#__PURE__*/_react.default.createElement(_Autocomplete.default, _extends({
101
134
  disablePortal: false
102
135
  }, getMuiAttributes(), {
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "files": {
3
- "main.js": "/DinakaranS/-dynamic-mui/build/bundle.d0a39e98.js",
3
+ "main.js": "/DinakaranS/-dynamic-mui/build/bundle.33388aca.js",
4
4
  "index.html": "/DinakaranS/-dynamic-mui/index.html"
5
5
  },
6
6
  "entrypoints": [
7
- "build/bundle.d0a39e98.js"
7
+ "build/bundle.33388aca.js"
8
8
  ]
9
9
  }