Skip to content

Commit 9b478e3

Browse files
authored
Biz/dj 2825 remote options gets unset when loading configuredprops (#15401)
* Refactor ControlSelect to fix custom string input * Fix custom string input state update * Populate prev value of remote options on page load * Pass prevValues to RemoteOptionsContainer * Update package * Pass prevValues to RemoteOptionsContainer * Ensure propsNeedConfiguring is updated after field registration * Remove checkPropsNeedConfiguring call * Cleanup * Cleanup * Update version and changelog * Commit pnpm-lock.yaml * Remove unused prevValue
1 parent 7f5e223 commit 9b478e3

File tree

4 files changed

+117
-47
lines changed

4 files changed

+117
-47
lines changed

packages/connect-react/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<!-- markdownlint-disable MD024 -->
22
# Changelog
33

4+
# [1.0.0-preview.24] - 2025-01-24
5+
6+
- Fix the bug where inputting multiple strings into an array prop would merge the strings into one
7+
- Fix custom string input for remote options
8+
- Fix the reloading of a previously selected remote option when re-rendering the form component
9+
410
# [1.0.0-preview.23] - 2025-01-22
511

612
- Show the prop label instead of the value after selecting from a dropdown for string array props

packages/connect-react/examples/nextjs/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/connect-react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/connect-react",
3-
"version": "1.0.0-preview.23",
3+
"version": "1.0.0-preview.24",
44
"description": "Pipedream Connect library for React",
55
"files": [
66
"dist"

packages/connect-react/src/components/ControlSelect.tsx

Lines changed: 109 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { useMemo } from "react";
1+
import {
2+
useEffect,
3+
useMemo, useState,
4+
} from "react";
25
import Select, {
36
Props as ReactSelectProps, components,
47
} from "react-select";
@@ -28,6 +31,26 @@ export function ControlSelect<T>({
2831
const {
2932
select, theme,
3033
} = useCustomize();
34+
const [
35+
selectOptions,
36+
setSelectOptions,
37+
] = useState(options);
38+
const [
39+
rawValue,
40+
setRawValue,
41+
] = useState(value);
42+
43+
useEffect(() => {
44+
setSelectOptions(options)
45+
}, [
46+
options,
47+
])
48+
49+
useEffect(() => {
50+
setRawValue(value)
51+
}, [
52+
value,
53+
])
3154

3255
const baseSelectProps: BaseReactSelectProps<never, never, never> = {
3356
styles: {
@@ -40,7 +63,7 @@ export function ControlSelect<T>({
4063
};
4164

4265
const selectValue = useMemo(() => {
43-
let ret = value;
66+
let ret = rawValue;
4467
if (ret != null) {
4568
if (Array.isArray(ret)) {
4669
// if simple, make lv (XXX combine this with other place this happens)
@@ -51,7 +74,7 @@ export function ControlSelect<T>({
5174
label: o,
5275
value: o,
5376
}
54-
for (const item of options) {
77+
for (const item of selectOptions) {
5578
if (item.value === o) {
5679
obj = item;
5780
break;
@@ -62,23 +85,28 @@ export function ControlSelect<T>({
6285
ret = lvs;
6386
}
6487
} else if (typeof ret !== "object") {
65-
const lvOptions = options?.[0] && typeof options[0] === "object";
88+
const lvOptions = selectOptions?.[0] && typeof selectOptions[0] === "object";
6689
if (lvOptions) {
67-
for (const item of options) {
68-
if (item.value === value) {
90+
for (const item of selectOptions) {
91+
if (item.value === rawValue) {
6992
ret = item;
7093
break;
7194
}
7295
}
96+
} else {
97+
ret = {
98+
label: rawValue,
99+
value: rawValue,
100+
}
73101
}
74102
} else if (ret.__lv) {
75103
ret = ret.__lv
76104
}
77105
}
78106
return ret;
79107
}, [
80-
value,
81-
options,
108+
rawValue,
109+
selectOptions,
82110
]);
83111

84112
const LoadMore = ({
@@ -105,58 +133,94 @@ export function ControlSelect<T>({
105133
}
106134

107135
const handleCreate = (inputValue: string) => {
108-
options.unshift({
109-
label: inputValue,
110-
value: inputValue,
111-
})
136+
const createOption = (input: unknown) => {
137+
if (typeof input === "object") return input
138+
return {
139+
label: input,
140+
value: input,
141+
}
142+
}
143+
const newOption = createOption(inputValue)
144+
let newRawValue = newOption
145+
const newSelectOptions = selectOptions
146+
? [
147+
newOption,
148+
...selectOptions,
149+
]
150+
: [
151+
newOption,
152+
]
153+
setSelectOptions(newSelectOptions);
154+
if (prop.type.endsWith("[]")) {
155+
if (Array.isArray(rawValue)) {
156+
newRawValue = [
157+
...rawValue.map(createOption),
158+
newOption,
159+
]
160+
} else {
161+
newRawValue = [
162+
newOption,
163+
]
164+
}
165+
}
166+
setRawValue(newRawValue)
167+
handleChange(newRawValue)
112168
};
113169

170+
const handleChange = (o: unknown) => {
171+
if (o) {
172+
if (Array.isArray(o)) {
173+
if (typeof o[0] === "object" && "value" in o[0]) {
174+
const vs = [];
175+
for (const _o of o) {
176+
if (prop.withLabel) {
177+
vs.push(_o);
178+
} else {
179+
vs.push(_o.value);
180+
}
181+
}
182+
onChange(vs);
183+
} else {
184+
onChange(o);
185+
}
186+
} else if (typeof o === "object" && "value" in o) {
187+
if (prop.withLabel) {
188+
onChange({
189+
__lv: o,
190+
});
191+
} else {
192+
onChange(o.value);
193+
}
194+
} else {
195+
throw new Error("unhandled option type"); // TODO
196+
}
197+
} else {
198+
onChange(undefined);
199+
}
200+
}
201+
202+
const additionalProps = {
203+
onCreateOption: prop.remoteOptions
204+
? handleCreate
205+
: undefined,
206+
}
207+
114208
const MaybeCreatableSelect = isCreatable
115209
? CreatableSelect
116210
: Select;
117211
return (
118212
<MaybeCreatableSelect
119213
inputId={id}
120214
instanceId={id}
121-
options={options}
215+
options={selectOptions}
122216
value={selectValue}
123217
isMulti={prop.type.endsWith("[]")}
124218
isClearable={true}
125219
required={!prop.optional}
126220
{...props}
127221
{...selectProps}
128-
onCreateOption={handleCreate}
129-
onChange={(o) => {
130-
if (o) {
131-
if (Array.isArray(o)) {
132-
if (typeof o[0] === "object" && "value" in o[0]) {
133-
const vs = [];
134-
for (const _o of o) {
135-
if (prop.withLabel) {
136-
vs.push(_o);
137-
} else {
138-
vs.push(_o.value);
139-
}
140-
}
141-
onChange(vs);
142-
} else {
143-
onChange(o);
144-
}
145-
} else if (typeof o === "object" && "value" in o) {
146-
if (prop.withLabel) {
147-
onChange({
148-
__lv: o,
149-
});
150-
} else {
151-
onChange(o.value);
152-
}
153-
} else {
154-
throw new Error("unhandled option type"); // TODO
155-
}
156-
} else {
157-
onChange(undefined);
158-
}
159-
}}
222+
{...additionalProps}
223+
onChange={handleChange}
160224
/>
161225
);
162226
}

0 commit comments

Comments
 (0)