diff --git a/packages/connect-react/CHANGELOG.md b/packages/connect-react/CHANGELOG.md index f648366d74be0..af8ac173e64a3 100644 --- a/packages/connect-react/CHANGELOG.md +++ b/packages/connect-react/CHANGELOG.md @@ -1,6 +1,12 @@ # Changelog +# [1.0.0-preview.24] - 2025-01-24 + +- Fix the bug where inputting multiple strings into an array prop would merge the strings into one +- Fix custom string input for remote options +- Fix the reloading of a previously selected remote option when re-rendering the form component + # [1.0.0-preview.23] - 2025-01-22 - Show the prop label instead of the value after selecting from a dropdown for string array props diff --git a/packages/connect-react/examples/nextjs/package-lock.json b/packages/connect-react/examples/nextjs/package-lock.json index 2190193a49061..9d9ef59557722 100644 --- a/packages/connect-react/examples/nextjs/package-lock.json +++ b/packages/connect-react/examples/nextjs/package-lock.json @@ -23,7 +23,7 @@ }, "../..": { "name": "@pipedream/connect-react", - "version": "1.0.0-preview.22", + "version": "1.0.0-preview.24", "license": "MIT", "dependencies": { "@pipedream/sdk": "workspace:^", diff --git a/packages/connect-react/package.json b/packages/connect-react/package.json index 15dfa2f466b1b..b4c52d6571bd7 100644 --- a/packages/connect-react/package.json +++ b/packages/connect-react/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/connect-react", - "version": "1.0.0-preview.23", + "version": "1.0.0-preview.24", "description": "Pipedream Connect library for React", "files": [ "dist" diff --git a/packages/connect-react/src/components/ControlSelect.tsx b/packages/connect-react/src/components/ControlSelect.tsx index 97092e79876c8..b7f1be6ae704d 100644 --- a/packages/connect-react/src/components/ControlSelect.tsx +++ b/packages/connect-react/src/components/ControlSelect.tsx @@ -1,4 +1,7 @@ -import { useMemo } from "react"; +import { + useEffect, + useMemo, useState, +} from "react"; import Select, { Props as ReactSelectProps, components, } from "react-select"; @@ -28,6 +31,26 @@ export function ControlSelect({ const { select, theme, } = useCustomize(); + const [ + selectOptions, + setSelectOptions, + ] = useState(options); + const [ + rawValue, + setRawValue, + ] = useState(value); + + useEffect(() => { + setSelectOptions(options) + }, [ + options, + ]) + + useEffect(() => { + setRawValue(value) + }, [ + value, + ]) const baseSelectProps: BaseReactSelectProps = { styles: { @@ -40,7 +63,7 @@ export function ControlSelect({ }; const selectValue = useMemo(() => { - let ret = value; + let ret = rawValue; if (ret != null) { if (Array.isArray(ret)) { // if simple, make lv (XXX combine this with other place this happens) @@ -51,7 +74,7 @@ export function ControlSelect({ label: o, value: o, } - for (const item of options) { + for (const item of selectOptions) { if (item.value === o) { obj = item; break; @@ -62,14 +85,19 @@ export function ControlSelect({ ret = lvs; } } else if (typeof ret !== "object") { - const lvOptions = options?.[0] && typeof options[0] === "object"; + const lvOptions = selectOptions?.[0] && typeof selectOptions[0] === "object"; if (lvOptions) { - for (const item of options) { - if (item.value === value) { + for (const item of selectOptions) { + if (item.value === rawValue) { ret = item; break; } } + } else { + ret = { + label: rawValue, + value: rawValue, + } } } else if (ret.__lv) { ret = ret.__lv @@ -77,8 +105,8 @@ export function ControlSelect({ } return ret; }, [ - value, - options, + rawValue, + selectOptions, ]); const LoadMore = ({ @@ -105,12 +133,78 @@ export function ControlSelect({ } const handleCreate = (inputValue: string) => { - options.unshift({ - label: inputValue, - value: inputValue, - }) + const createOption = (input: unknown) => { + if (typeof input === "object") return input + return { + label: input, + value: input, + } + } + const newOption = createOption(inputValue) + let newRawValue = newOption + const newSelectOptions = selectOptions + ? [ + newOption, + ...selectOptions, + ] + : [ + newOption, + ] + setSelectOptions(newSelectOptions); + if (prop.type.endsWith("[]")) { + if (Array.isArray(rawValue)) { + newRawValue = [ + ...rawValue.map(createOption), + newOption, + ] + } else { + newRawValue = [ + newOption, + ] + } + } + setRawValue(newRawValue) + handleChange(newRawValue) }; + const handleChange = (o: unknown) => { + if (o) { + if (Array.isArray(o)) { + if (typeof o[0] === "object" && "value" in o[0]) { + const vs = []; + for (const _o of o) { + if (prop.withLabel) { + vs.push(_o); + } else { + vs.push(_o.value); + } + } + onChange(vs); + } else { + onChange(o); + } + } else if (typeof o === "object" && "value" in o) { + if (prop.withLabel) { + onChange({ + __lv: o, + }); + } else { + onChange(o.value); + } + } else { + throw new Error("unhandled option type"); // TODO + } + } else { + onChange(undefined); + } + } + + const additionalProps = { + onCreateOption: prop.remoteOptions + ? handleCreate + : undefined, + } + const MaybeCreatableSelect = isCreatable ? CreatableSelect : Select; @@ -118,45 +212,15 @@ export function ControlSelect({ { - if (o) { - if (Array.isArray(o)) { - if (typeof o[0] === "object" && "value" in o[0]) { - const vs = []; - for (const _o of o) { - if (prop.withLabel) { - vs.push(_o); - } else { - vs.push(_o.value); - } - } - onChange(vs); - } else { - onChange(o); - } - } else if (typeof o === "object" && "value" in o) { - if (prop.withLabel) { - onChange({ - __lv: o, - }); - } else { - onChange(o.value); - } - } else { - throw new Error("unhandled option type"); // TODO - } - } else { - onChange(undefined); - } - }} + {...additionalProps} + onChange={handleChange} /> ); } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3183cb0fdb21a..09d98effc5dfb 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9279,8 +9279,7 @@ importers: specifier: 3.0.1 version: 3.0.1 - components/seen: - specifiers: {} + components/seen: {} components/segment: dependencies: