From f62683e528a95bc124e742ad700079db48498b3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Pfeuffer?= Date: Tue, 30 Jun 2020 15:20:59 +0200 Subject: [PATCH] Create split and replace component --- .../src/SplitAndReplace.stories.tsx | 59 +++++++++++++++++++ scm-ui/ui-components/src/SplitAndReplace.tsx | 52 ++++++++++++++++ .../src}/textSplitAndReplace.test.ts | 0 .../src}/textSplitAndReplace.ts | 0 4 files changed, 111 insertions(+) create mode 100644 scm-ui/ui-components/src/SplitAndReplace.stories.tsx create mode 100644 scm-ui/ui-components/src/SplitAndReplace.tsx rename scm-ui/{ui-webapp/src/repos/components/changesets => ui-components/src}/textSplitAndReplace.test.ts (100%) rename scm-ui/{ui-webapp/src/repos/components/changesets => ui-components/src}/textSplitAndReplace.ts (100%) diff --git a/scm-ui/ui-components/src/SplitAndReplace.stories.tsx b/scm-ui/ui-components/src/SplitAndReplace.stories.tsx new file mode 100644 index 0000000000..a49f760c87 --- /dev/null +++ b/scm-ui/ui-components/src/SplitAndReplace.stories.tsx @@ -0,0 +1,59 @@ +/* + * MIT License + * + * Copyright (c) 2020-present Cloudogu GmbH and Contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +import React from "react"; +import { storiesOf } from "@storybook/react"; +import SplitAndReplace from "./SplitAndReplace"; +import { Icon } from "@scm-manager/ui-components/src"; + +storiesOf("SplitAndReplace", module).add("Simple replacement", () => { + const replacements = [ + { + textToReplace: "'", + replacement: , + replaceAll: true + }, + { + textToReplace: "`", + replacement: , + replaceAll: true + }, + { + replacement:
 
, + textToReplace: " ", + replaceAll: true + } + ]; + return ( + <> +
+
+ + ); +}); diff --git a/scm-ui/ui-components/src/SplitAndReplace.tsx b/scm-ui/ui-components/src/SplitAndReplace.tsx new file mode 100644 index 0000000000..213e9ff5d5 --- /dev/null +++ b/scm-ui/ui-components/src/SplitAndReplace.tsx @@ -0,0 +1,52 @@ +/* + * MIT License + * + * Copyright (c) 2020-present Cloudogu GmbH and Contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +import React, { FC, ReactNode } from "react"; +import textSplitAndReplace from "./textSplitAndReplace"; + +type Replacement = { + textToReplace: string; + replacement: ReactNode; + replaceAll: boolean; +}; + +type Props = { + text: string; + replacements: Replacement[]; +}; + +type PartToReplace = { + start: number; + length: number; + replacement: ReactNode; +}; + +const SplitAndReplace: FC = ({ text, replacements }) => { + const parts = textSplitAndReplace(text, replacements, s =>
{s}
); + if (parts.length === 0) { + return <>{parts[0]}; + } + return
{parts}
; +}; + +export default SplitAndReplace; diff --git a/scm-ui/ui-webapp/src/repos/components/changesets/textSplitAndReplace.test.ts b/scm-ui/ui-components/src/textSplitAndReplace.test.ts similarity index 100% rename from scm-ui/ui-webapp/src/repos/components/changesets/textSplitAndReplace.test.ts rename to scm-ui/ui-components/src/textSplitAndReplace.test.ts diff --git a/scm-ui/ui-webapp/src/repos/components/changesets/textSplitAndReplace.ts b/scm-ui/ui-components/src/textSplitAndReplace.ts similarity index 100% rename from scm-ui/ui-webapp/src/repos/components/changesets/textSplitAndReplace.ts rename to scm-ui/ui-components/src/textSplitAndReplace.ts