Skip to content

Commit ffafc49

Browse files
committed
Improve value prop so it can either be a primitive string value or an Option
1 parent d4973b5 commit ffafc49

File tree

4 files changed

+89
-4
lines changed

4 files changed

+89
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@programmer_network/yail",
3-
"version": "1.0.27",
3+
"version": "1.0.28",
44
"description": "Programmer Network's official UI library for React",
55
"author": "Aleksandar Grbic - (https://programmer.network)",
66
"publishConfig": {

src/Components/Inputs/Select/Select.stories.tsx

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,45 @@ SingleSelect.args = {
4747
value: "foo"
4848
};
4949

50+
export const SingleSelectWithStringValue = (args: ISelectProps) => {
51+
const [input, setInput] = useState<
52+
Record<
53+
string,
54+
MultiValue<Option | undefined> | SingleValue<Option | undefined | string>
55+
>
56+
>({
57+
foo: "foo"
58+
});
59+
60+
useEffect(() => {
61+
console.log(input);
62+
}, [input]);
63+
64+
return <Select {...args} name='foo' value={input.foo} onChange={setInput} />;
65+
};
66+
67+
SingleSelectWithStringValue.args = {
68+
name: "single-select",
69+
onChange: action("onChange"),
70+
options: [
71+
{ value: "foo", label: "Foo" },
72+
{ value: "bar", label: "Bar" },
73+
{ value: "baz", label: "Baz" }
74+
],
75+
value: "foo"
76+
};
77+
5078
export const MultiSelect = (args: ISelectProps) => {
5179
const [input, setInput] = useState<
5280
Record<
5381
string,
5482
MultiValue<Option | undefined> | SingleValue<Option | undefined>
5583
>
5684
>({
57-
foo: []
85+
foo: [
86+
{ value: "foo", label: "foo" },
87+
{ value: "bar", label: "bar" }
88+
]
5889
});
5990

6091
useEffect(() => {
@@ -73,3 +104,31 @@ MultiSelect.args = {
73104
{ value: "baz", label: "Baz" }
74105
]
75106
};
107+
108+
export const MultiSelectWithStringArray = (args: ISelectProps) => {
109+
const [input, setInput] = useState<
110+
Record<
111+
string,
112+
| MultiValue<Option | undefined>
113+
| SingleValue<Option | undefined | string[]>
114+
>
115+
>({
116+
foo: ["foo", "bar"]
117+
});
118+
119+
useEffect(() => {
120+
console.log(input);
121+
}, [input]);
122+
123+
return <Select {...args} value={input.foo} onChange={setInput} />;
124+
};
125+
126+
MultiSelectWithStringArray.args = {
127+
name: "foo",
128+
isMulti: true,
129+
options: [
130+
{ value: "foo", label: "Foo" },
131+
{ value: "bar", label: "Bar" },
132+
{ value: "baz", label: "Baz" }
133+
]
134+
};

src/Components/Inputs/Select/index.tsx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,32 @@ const Select: FC<ISelectProps> = props => {
2121
return props.onChange({ [props.name]: option });
2222
};
2323

24+
const getValue = () => {
25+
if (!props.value) {
26+
return;
27+
}
28+
29+
if (isMulti && Array.isArray(props.value)) {
30+
const values = props.value.map(value => {
31+
if (typeof value === "object" && value !== null && "value" in value) {
32+
return options.find(
33+
option => option.value === (value as Option)?.value
34+
);
35+
} else {
36+
return options.find(option => option.value === value);
37+
}
38+
});
39+
40+
return values.filter(Boolean).length ? values : undefined;
41+
}
42+
43+
return options.find(
44+
option =>
45+
option.value === props.value ||
46+
option.value === (props.value as Option)?.value
47+
);
48+
};
49+
2450
return (
2551
<div className={classNames(props.inputWrapperClassName)}>
2652
{max || label || hint ? (
@@ -39,7 +65,7 @@ const Select: FC<ISelectProps> = props => {
3965
closeMenuOnSelect={isMulti ? false : true}
4066
isMulti={isMulti}
4167
defaultValue={defaultValue}
42-
value={props.value}
68+
value={getValue()}
4369
styles={styles()}
4470
className='pn-select-container text-sm'
4571
classNamePrefix='pn-select'

src/Components/Inputs/Select/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface ISelectProps {
99
name: string;
1010
type?: string;
1111
required?: boolean;
12-
value: PropsValue<Option | undefined>;
12+
value: PropsValue<Option | undefined> | string | string[];
1313
defaultValue?: PropsValue<Option | undefined>;
1414
error?: string;
1515
hint?: string;

0 commit comments

Comments
 (0)