@agility/plenum-ui 2.1.19-rc2 → 2.1.19-rc3

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.
@@ -4,13 +4,21 @@ export interface ISimpleSelectOptions {
4
4
  value: string;
5
5
  }
6
6
  export interface ISelectProps {
7
+ /** Label */
7
8
  label?: string;
9
+ /** Select ID prop */
8
10
  id?: string;
11
+ /** Select name prop */
9
12
  name?: string;
13
+ /** List of options to display in the select menu */
10
14
  options: ISimpleSelectOptions[];
15
+ /** Select name prop */
11
16
  onChange?(value: string): void;
17
+ /** Select disabled state */
12
18
  isDisabled?: boolean;
19
+ /** Select error state */
13
20
  isError?: boolean;
21
+ /** Select required state */
14
22
  isRequired?: boolean;
15
23
  value?: string;
16
24
  className?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agility/plenum-ui",
3
- "version": "2.1.19-rc2",
3
+ "version": "2.1.19-rc3",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -8,20 +8,27 @@ export interface ISimpleSelectOptions {
8
8
  value: string;
9
9
  }
10
10
  export interface ISelectProps {
11
+ /** Label */
11
12
  label?: string;
13
+ /** Select ID prop */
12
14
  id?: string;
15
+ /** Select name prop */
13
16
  name?: string;
17
+ /** List of options to display in the select menu */
14
18
  options: ISimpleSelectOptions[];
19
+ /** Select name prop */
15
20
  onChange?(value: string): void;
21
+ /** Select disabled state */
16
22
  isDisabled?: boolean;
23
+ /** Select error state */
17
24
  isError?: boolean;
25
+ /** Select required state */
18
26
  isRequired?: boolean;
19
27
  value?: string;
20
28
  className?: string;
21
29
  onFocus?: () => void;
22
30
  onBlur?: () => void;
23
31
  }
24
-
25
32
  const Select: React.FC<ISelectProps> = ({
26
33
  label,
27
34
  id,
@@ -36,31 +43,28 @@ const Select: React.FC<ISelectProps> = ({
36
43
  onFocus,
37
44
  onBlur
38
45
  }) => {
39
- const getValidVal = (val: string | undefined) => {
40
- const validValue = val && val !== "" ? val : options[0]?.value ?? "select an option";
41
- console.log("valid Val", validValue);
42
- return validValue;
43
- };
44
- const [selectedOption, setSelectedOption] = useState<string>(getValidVal(value));
46
+ const [selectedOption, setSelectedOption] = useState<string>(value || options[0].value);
45
47
  const uniqueID = useId();
46
48
  if (!id) id = `select-${uniqueID}`;
47
49
  if (!name) name = id;
48
50
 
49
51
  useEffect(() => {
50
- // Explicitly handle falsy values
51
- if (value !== undefined) {
52
+ // handle the case where value could be "" or false
53
+ if (value === "") {
54
+ const matchedOption = options.find((option) => option.value === "");
55
+ setSelectedOption(matchedOption?.value || options[0].value);
56
+ return;
57
+ }
58
+ if (value !== undefined && value !== null) {
52
59
  setSelectedOption(value);
53
60
  }
54
- }, [value]);
61
+ }, [options, value]);
55
62
 
56
63
  const handleChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
57
64
  const targetValue = e.target.value;
58
- if (typeof onChange === "function") {
59
- onChange(targetValue);
60
- }
65
+ typeof onChange == "function" && onChange(targetValue);
61
66
  setSelectedOption(targetValue);
62
67
  };
63
-
64
68
  const wrapperStyle = cn("group", { "opacity-50": isDisabled });
65
69
  return (
66
70
  <div className={wrapperStyle}>
@@ -91,11 +95,13 @@ const Select: React.FC<ISelectProps> = ({
91
95
  onFocus={onFocus}
92
96
  onBlur={onBlur}
93
97
  >
94
- {options.map(({ value, label }) => (
95
- <option key={value} value={value}>
96
- {label}
97
- </option>
98
- ))}
98
+ {options.map(({ value, label }) => {
99
+ return (
100
+ <option key={value} value={value}>
101
+ {label}
102
+ </option>
103
+ );
104
+ })}
99
105
  </select>
100
106
  </div>
101
107
  );