@agility/plenum-ui 2.1.17 → 2.1.19-rc2

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