@etsoo/materialui 1.0.36 → 1.0.37

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.
@@ -1,6 +1,16 @@
1
- /// <reference types="react" />
2
1
  import { DataTypes, IdDefaultType, LabelDefaultType, ListType } from '@etsoo/shared';
3
2
  import { FormControlProps } from '@mui/material';
3
+ import React from 'react';
4
+ /**
5
+ * OptionGroup methods ref
6
+ */
7
+ export interface OptionGroupRef {
8
+ /**
9
+ * Disable specific items with their ids
10
+ * @param ids Ids
11
+ */
12
+ disable(ids: unknown[]): void;
13
+ }
4
14
  /**
5
15
  * OptionGroup props
6
16
  */
@@ -25,6 +35,10 @@ export declare type OptionGroupProps<T extends object, D extends DataTypes.Keys<
25
35
  * Label field
26
36
  */
27
37
  labelField?: L;
38
+ /**
39
+ * Methods
40
+ */
41
+ mRef?: React.Ref<OptionGroupRef>;
28
42
  /**
29
43
  * Multiple choose item
30
44
  */
@@ -8,7 +8,7 @@ import React from 'react';
8
8
  */
9
9
  export function OptionGroup(props) {
10
10
  // Destruct
11
- const { getOptionLabel, defaultValue, idField = 'id', label, labelField = 'label', multiple = false, name, onValueChange, options, readOnly, row, size, ...rest } = props;
11
+ const { getOptionLabel, defaultValue, idField = 'id', label, labelField = 'label', multiple = false, mRef, name, onValueChange, options, readOnly, row, size, ...rest } = props;
12
12
  // Get option value
13
13
  // D type should be the source id type
14
14
  const getOptionValue = (option) => {
@@ -23,6 +23,8 @@ export function OptionGroup(props) {
23
23
  : Array.isArray(defaultValue)
24
24
  ? defaultValue
25
25
  : [defaultValue]);
26
+ // Disabled ids
27
+ const [disabledIds, setDisabledIds] = React.useState();
26
28
  // Item checked
27
29
  const itemChecked = (option) => {
28
30
  // Value
@@ -31,12 +33,19 @@ export function OptionGroup(props) {
31
33
  return false;
32
34
  return values.includes(value);
33
35
  };
36
+ React.useImperativeHandle(mRef, () => ({
37
+ disable(ids) {
38
+ setDisabledIds(ids);
39
+ }
40
+ }));
34
41
  // First item value
35
42
  const firstOptionValue = getOptionValue(options[0]);
36
43
  // Items
37
44
  const list = options.map((option) => {
45
+ // Value
46
+ const ov = getOptionValue(option);
38
47
  // Control
39
- const control = multiple ? (React.createElement(Checkbox, { name: name, readOnly: readOnly, size: size, checked: itemChecked(option), onChange: (event) => {
48
+ const control = multiple ? (React.createElement(Checkbox, { name: name, readOnly: readOnly, size: size, checked: itemChecked(option), disabled: disabledIds === null || disabledIds === void 0 ? void 0 : disabledIds.includes(ov), onChange: (event) => {
40
49
  if (firstOptionValue == null)
41
50
  return;
42
51
  const typeValue = Utils.parseString(event.target.value, firstOptionValue);
@@ -55,7 +64,7 @@ export function OptionGroup(props) {
55
64
  if (onValueChange)
56
65
  onValueChange(changedValues);
57
66
  setValues(changedValues);
58
- } })) : (React.createElement(Radio, { size: size, readOnly: readOnly }));
67
+ } })) : (React.createElement(Radio, { disabled: disabledIds === null || disabledIds === void 0 ? void 0 : disabledIds.includes(ov), size: size, readOnly: readOnly }));
59
68
  // Label
60
69
  const label = getOptionLabel == null
61
70
  ? `${option[labelField]}`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/materialui",
3
- "version": "1.0.36",
3
+ "version": "1.0.37",
4
4
  "description": "TypeScript Material-UI Implementation",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -17,6 +17,17 @@ import {
17
17
  } from '@mui/material';
18
18
  import React from 'react';
19
19
 
20
+ /**
21
+ * OptionGroup methods ref
22
+ */
23
+ export interface OptionGroupRef {
24
+ /**
25
+ * Disable specific items with their ids
26
+ * @param ids Ids
27
+ */
28
+ disable(ids: unknown[]): void;
29
+ }
30
+
20
31
  /**
21
32
  * OptionGroup props
22
33
  */
@@ -50,6 +61,11 @@ export type OptionGroupProps<
50
61
  */
51
62
  labelField?: L;
52
63
 
64
+ /**
65
+ * Methods
66
+ */
67
+ mRef?: React.Ref<OptionGroupRef>;
68
+
53
69
  /**
54
70
  * Multiple choose item
55
71
  */
@@ -99,6 +115,7 @@ export function OptionGroup<
99
115
  label,
100
116
  labelField = 'label' as L,
101
117
  multiple = false,
118
+ mRef,
102
119
  name,
103
120
  onValueChange,
104
121
  options,
@@ -125,6 +142,9 @@ export function OptionGroup<
125
142
  : [defaultValue]
126
143
  );
127
144
 
145
+ // Disabled ids
146
+ const [disabledIds, setDisabledIds] = React.useState<unknown[]>();
147
+
128
148
  // Item checked
129
149
  const itemChecked = (option: T) => {
130
150
  // Value
@@ -134,11 +154,20 @@ export function OptionGroup<
134
154
  return values.includes(value);
135
155
  };
136
156
 
157
+ React.useImperativeHandle(mRef, () => ({
158
+ disable(ids: unknown[]) {
159
+ setDisabledIds(ids);
160
+ }
161
+ }));
162
+
137
163
  // First item value
138
164
  const firstOptionValue = getOptionValue(options[0]);
139
165
 
140
166
  // Items
141
167
  const list = options.map((option) => {
168
+ // Value
169
+ const ov = getOptionValue(option);
170
+
142
171
  // Control
143
172
  const control = multiple ? (
144
173
  <Checkbox
@@ -146,6 +175,7 @@ export function OptionGroup<
146
175
  readOnly={readOnly}
147
176
  size={size}
148
177
  checked={itemChecked(option)}
178
+ disabled={disabledIds?.includes(ov)}
149
179
  onChange={(event) => {
150
180
  if (firstOptionValue == null) return;
151
181
 
@@ -171,7 +201,11 @@ export function OptionGroup<
171
201
  }}
172
202
  />
173
203
  ) : (
174
- <Radio size={size} readOnly={readOnly} />
204
+ <Radio
205
+ disabled={disabledIds?.includes(ov)}
206
+ size={size}
207
+ readOnly={readOnly}
208
+ />
175
209
  );
176
210
 
177
211
  // Label