From a7bb67f36b3bae40d898b7980184fc1fe2551b46 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Mon, 16 Aug 2021 14:54:50 +0200 Subject: [PATCH] Fix branch selector display revision if selected instead of first branch (#1767) The select component displays the first option if the value is not part of the options. This behaviour breaks the BranchSelector which uses Select since version 2.22.0 (before it used the deprecated DropDown component). The BranchSelector has to display the revision if one is selected, which is not part of the options. This change will add a addValueToOptions property to the Select component and restore the old behaviour. --- gradle/changelog/branch_selector.yaml | 2 + scm-ui/ui-components/src/BranchSelector.tsx | 1 + .../src/__snapshots__/storyshots.test.ts.snap | 75 +++++++++++++++++++ .../src/forms/Select.stories.tsx | 74 +++++++++++++++--- scm-ui/ui-components/src/forms/Select.tsx | 10 ++- 5 files changed, 152 insertions(+), 10 deletions(-) create mode 100644 gradle/changelog/branch_selector.yaml diff --git a/gradle/changelog/branch_selector.yaml b/gradle/changelog/branch_selector.yaml new file mode 100644 index 0000000000..89b6c9b1bb --- /dev/null +++ b/gradle/changelog/branch_selector.yaml @@ -0,0 +1,2 @@ +- type: Fixed + description: Branch selector display revision if selected instead of first branch ([#1767](https://github.com/scm-manager/scm-manager/pull/1767)) diff --git a/scm-ui/ui-components/src/BranchSelector.tsx b/scm-ui/ui-components/src/BranchSelector.tsx index 27cd0a414c..062221ce57 100644 --- a/scm-ui/ui-components/src/BranchSelector.tsx +++ b/scm-ui/ui-components/src/BranchSelector.tsx @@ -64,6 +64,7 @@ const BranchSelector: FC = ({ branches, onSelectBranch, selectedBranch, l onChange={(branch) => onSelectBranch(branches.filter((b) => b.name === branch)[0])} disabled={!!disabled} value={selectedBranch} + addValueToOptions={true} /> diff --git a/scm-ui/ui-components/src/__snapshots__/storyshots.test.ts.snap b/scm-ui/ui-components/src/__snapshots__/storyshots.test.ts.snap index d07a1eef3f..b7e41bfa8c 100644 --- a/scm-ui/ui-components/src/__snapshots__/storyshots.test.ts.snap +++ b/scm-ui/ui-components/src/__snapshots__/storyshots.test.ts.snap @@ -56524,6 +56524,41 @@ exports[`Storyshots Forms|Radio With HelpText 1`] = ` `; +exports[`Storyshots Forms|Select Add no existing value 1`] = ` +Array [ +
+ +
+ +
+
, +
+ uscss-prometheus +
, +] +`; + exports[`Storyshots Forms|Select Legacy Events 1`] = ` Array [
+ +
+ +
+
, +
+ razor-crest +
, +] +`; + exports[`Storyshots Forms|Select ReactHookForm 1`] = `
{ { { @@ -145,8 +145,64 @@ const LegacyEvents: FC = () => { ); }; +const AddNoExistingValue: FC = () => { + const [value, setValue] = useState("uscss-prometheus"); + + return ( + <> + +
{value}
+ + ); +}; + storiesOf("Forms|Select", module) - .addDecorator(storyFn => {storyFn()}) + .addDecorator((storyFn) => {storyFn()}) + .add("Add no existing value", () => ) .add("Ref", () => ) .add("Legacy Events", () => ) - .add("ReactHookForm", () => ); + .add("ReactHookForm", () => ) + .add("Preselect option", () => ) +; diff --git a/scm-ui/ui-components/src/forms/Select.tsx b/scm-ui/ui-components/src/forms/Select.tsx index 06d9a3cea1..dbf25b3b29 100644 --- a/scm-ui/ui-components/src/forms/Select.tsx +++ b/scm-ui/ui-components/src/forms/Select.tsx @@ -45,6 +45,7 @@ type BaseProps = { defaultValue?: string; readOnly?: boolean; className?: string; + addValueToOptions?: boolean; }; const InnerSelect: FC> = ({ @@ -58,10 +59,17 @@ const InnerSelect: FC> = ({ testId, readOnly, className, + options, + addValueToOptions, ...props }) => { const field = useInnerRef(props.innerRef); + let opts = options; + if (value && addValueToOptions && options.some((o) => o.value === value)) { + opts = [{ label: value, value }, ...options]; + } + const handleInput = (event: ChangeEvent) => { if (props.onChange) { if (isUsingRef(props)) { @@ -113,7 +121,7 @@ const InnerSelect: FC> = ({ disabled={disabled} {...createAttributesForTesting(testId)} > - {props.options.map((opt) => { + {opts.map((opt) => { return (