@etsoo/materialui 1.6.19 → 1.6.20

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.
@@ -32,6 +32,7 @@ function ComboBoxMultiple(props) {
32
32
  const { search = false, autoAddBlankItem = search, idField = "id", idValue, idValues, inputError, inputHelperText, inputMargin, inputOnChange, inputRequired, inputReset, inputVariant, defaultValue, label, labelField = "label", loadData, onLoadData, name, inputAutoComplete = "off", options, dataReadonly = true, readOnly, onChange, openOnFocus = true, value, disableCloseOnSelect = true, renderOption = ({ key, ...restProps }, option, { selected }) => ((0, jsx_runtime_1.jsxs)("li", { ...restProps, children: [(0, jsx_runtime_1.jsx)(Checkbox_1.default, { icon: icon, checkedIcon: checkedIcon, style: { marginRight: 8 }, checked: selected }), `${option[labelField]}`] }, key)), getOptionLabel = (option) => `${option[labelField]}`, getOptionKey = (option) => `${option[idField]}`, sx = { minWidth: "150px" }, noOptionsText = labels?.noOptions, loadingText = labels?.loading, disabled, ...rest } = props;
33
33
  // Value input ref
34
34
  const inputRef = react_1.default.createRef();
35
+ const localRef = react_1.default.useRef(undefined);
35
36
  // Options state
36
37
  const [localOptions, setOptions] = react_1.default.useState(options ?? []);
37
38
  const isMounted = react_1.default.useRef(true);
@@ -44,6 +45,11 @@ function ComboBoxMultiple(props) {
44
45
  // State
45
46
  // null for controlled
46
47
  const [stateValue, setStateValue] = react_1.default.useState(null);
48
+ const selectedCount = stateValue?.length ?? 0;
49
+ react_1.default.useEffect(() => {
50
+ if (localRef.current && inputRequired)
51
+ localRef.current.required = selectedCount === 0;
52
+ }, [inputRequired, selectedCount]);
47
53
  react_1.default.useEffect(() => {
48
54
  const localValue = idValue != null
49
55
  ? localOptions.filter((o) => o[idField] === idValue)
@@ -58,11 +64,6 @@ function ComboBoxMultiple(props) {
58
64
  Object.assign(params, { readOnly });
59
65
  }
60
66
  Object.assign(params.inputProps, { "data-reset": inputReset });
61
- params.inputProps.onInvalid = (event) => {
62
- if (inputRequired && stateValue != null && stateValue.length > 0) {
63
- event.preventDefault();
64
- }
65
- };
66
67
  if (dataReadonly) {
67
68
  params.inputProps.onKeyDown = (event) => {
68
69
  if (shared_1.Keyboard.isTypingContent(event.key)) {
@@ -124,5 +125,5 @@ function ComboBoxMultiple(props) {
124
125
  // Custom
125
126
  if (onChange != null)
126
127
  onChange(event, value, reason, details);
127
- }, openOnFocus: openOnFocus, sx: sx, renderInput: (params) => search ? ((0, jsx_runtime_1.jsx)(SearchField_1.SearchField, { ...addReadOnly(params), label: label, name: name + "Input", margin: inputMargin, variant: inputVariant, required: inputRequired, error: inputError, helperText: inputHelperText })) : ((0, jsx_runtime_1.jsx)(InputField_1.InputField, { ...addReadOnly(params), label: label, name: name + "Input", margin: inputMargin, variant: inputVariant, required: inputRequired, error: inputError, helperText: inputHelperText })), options: localOptions, renderOption: renderOption, noOptionsText: noOptionsText, loadingText: loadingText, ...rest })] }));
128
+ }, openOnFocus: openOnFocus, sx: sx, renderInput: (params) => search ? ((0, jsx_runtime_1.jsx)(SearchField_1.SearchField, { ...addReadOnly(params), label: label, name: name + "Input", margin: inputMargin, variant: inputVariant, required: inputRequired, error: inputError, helperText: inputHelperText, inputRef: localRef })) : ((0, jsx_runtime_1.jsx)(InputField_1.InputField, { ...addReadOnly(params), label: label, name: name + "Input", margin: inputMargin, variant: inputVariant, required: inputRequired, error: inputError, helperText: inputHelperText, inputRef: localRef })), options: localOptions, renderOption: renderOption, noOptionsText: noOptionsText, loadingText: loadingText, ...rest })] }));
128
129
  }
@@ -26,6 +26,7 @@ export function ComboBoxMultiple(props) {
26
26
  const { search = false, autoAddBlankItem = search, idField = "id", idValue, idValues, inputError, inputHelperText, inputMargin, inputOnChange, inputRequired, inputReset, inputVariant, defaultValue, label, labelField = "label", loadData, onLoadData, name, inputAutoComplete = "off", options, dataReadonly = true, readOnly, onChange, openOnFocus = true, value, disableCloseOnSelect = true, renderOption = ({ key, ...restProps }, option, { selected }) => (_jsxs("li", { ...restProps, children: [_jsx(Checkbox, { icon: icon, checkedIcon: checkedIcon, style: { marginRight: 8 }, checked: selected }), `${option[labelField]}`] }, key)), getOptionLabel = (option) => `${option[labelField]}`, getOptionKey = (option) => `${option[idField]}`, sx = { minWidth: "150px" }, noOptionsText = labels?.noOptions, loadingText = labels?.loading, disabled, ...rest } = props;
27
27
  // Value input ref
28
28
  const inputRef = React.createRef();
29
+ const localRef = React.useRef(undefined);
29
30
  // Options state
30
31
  const [localOptions, setOptions] = React.useState(options ?? []);
31
32
  const isMounted = React.useRef(true);
@@ -38,6 +39,11 @@ export function ComboBoxMultiple(props) {
38
39
  // State
39
40
  // null for controlled
40
41
  const [stateValue, setStateValue] = React.useState(null);
42
+ const selectedCount = stateValue?.length ?? 0;
43
+ React.useEffect(() => {
44
+ if (localRef.current && inputRequired)
45
+ localRef.current.required = selectedCount === 0;
46
+ }, [inputRequired, selectedCount]);
41
47
  React.useEffect(() => {
42
48
  const localValue = idValue != null
43
49
  ? localOptions.filter((o) => o[idField] === idValue)
@@ -52,11 +58,6 @@ export function ComboBoxMultiple(props) {
52
58
  Object.assign(params, { readOnly });
53
59
  }
54
60
  Object.assign(params.inputProps, { "data-reset": inputReset });
55
- params.inputProps.onInvalid = (event) => {
56
- if (inputRequired && stateValue != null && stateValue.length > 0) {
57
- event.preventDefault();
58
- }
59
- };
60
61
  if (dataReadonly) {
61
62
  params.inputProps.onKeyDown = (event) => {
62
63
  if (Keyboard.isTypingContent(event.key)) {
@@ -118,5 +119,5 @@ export function ComboBoxMultiple(props) {
118
119
  // Custom
119
120
  if (onChange != null)
120
121
  onChange(event, value, reason, details);
121
- }, openOnFocus: openOnFocus, sx: sx, renderInput: (params) => search ? (_jsx(SearchField, { ...addReadOnly(params), label: label, name: name + "Input", margin: inputMargin, variant: inputVariant, required: inputRequired, error: inputError, helperText: inputHelperText })) : (_jsx(InputField, { ...addReadOnly(params), label: label, name: name + "Input", margin: inputMargin, variant: inputVariant, required: inputRequired, error: inputError, helperText: inputHelperText })), options: localOptions, renderOption: renderOption, noOptionsText: noOptionsText, loadingText: loadingText, ...rest })] }));
122
+ }, openOnFocus: openOnFocus, sx: sx, renderInput: (params) => search ? (_jsx(SearchField, { ...addReadOnly(params), label: label, name: name + "Input", margin: inputMargin, variant: inputVariant, required: inputRequired, error: inputError, helperText: inputHelperText, inputRef: localRef })) : (_jsx(InputField, { ...addReadOnly(params), label: label, name: name + "Input", margin: inputMargin, variant: inputVariant, required: inputRequired, error: inputError, helperText: inputHelperText, inputRef: localRef })), options: localOptions, renderOption: renderOption, noOptionsText: noOptionsText, loadingText: loadingText, ...rest })] }));
122
123
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/materialui",
3
- "version": "1.6.19",
3
+ "version": "1.6.20",
4
4
  "description": "TypeScript Material-UI Implementation",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -132,6 +132,7 @@ export function ComboBoxMultiple<
132
132
 
133
133
  // Value input ref
134
134
  const inputRef = React.createRef<HTMLInputElement>();
135
+ const localRef = React.useRef<HTMLInputElement>(undefined);
135
136
 
136
137
  // Options state
137
138
  const [localOptions, setOptions] = React.useState(options ?? []);
@@ -148,6 +149,12 @@ export function ComboBoxMultiple<
148
149
  // null for controlled
149
150
  const [stateValue, setStateValue] = React.useState<T[] | null>(null);
150
151
 
152
+ const selectedCount = stateValue?.length ?? 0;
153
+ React.useEffect(() => {
154
+ if (localRef.current && inputRequired)
155
+ localRef.current.required = selectedCount === 0;
156
+ }, [inputRequired, selectedCount]);
157
+
151
158
  React.useEffect(() => {
152
159
  const localValue: T[] | null | undefined =
153
160
  idValue != null
@@ -167,12 +174,6 @@ export function ComboBoxMultiple<
167
174
 
168
175
  Object.assign(params.inputProps, { "data-reset": inputReset });
169
176
 
170
- params.inputProps.onInvalid = (event) => {
171
- if (inputRequired && stateValue != null && stateValue.length > 0) {
172
- event.preventDefault();
173
- }
174
- };
175
-
176
177
  if (dataReadonly) {
177
178
  params.inputProps.onKeyDown = (event) => {
178
179
  if (Keyboard.isTypingContent(event.key)) {
@@ -279,6 +280,7 @@ export function ComboBoxMultiple<
279
280
  required={inputRequired}
280
281
  error={inputError}
281
282
  helperText={inputHelperText}
283
+ inputRef={localRef}
282
284
  />
283
285
  ) : (
284
286
  <InputField
@@ -290,6 +292,7 @@ export function ComboBoxMultiple<
290
292
  required={inputRequired}
291
293
  error={inputError}
292
294
  helperText={inputHelperText}
295
+ inputRef={localRef}
293
296
  />
294
297
  )
295
298
  }