From 99a3d4fed13384d7a9ef8b68e0f5ea1ad8b2f37c Mon Sep 17 00:00:00 2001 From: wooferzfg Date: Wed, 22 Jan 2020 20:14:02 -0600 Subject: [PATCH] Extract shared logic between KeyValue and Text to KeyValueGeneric --- src/css/KeyValue.scss | 4 ++ src/css/Text.scss | 23 ---------- src/layout/KeyValue.tsx | 63 +++++--------------------- src/layout/KeyValueGeneric.tsx | 82 ++++++++++++++++++++++++++++++++++ src/layout/Text.tsx | 72 ++++++++++------------------- 5 files changed, 121 insertions(+), 123 deletions(-) delete mode 100644 src/css/Text.scss create mode 100644 src/layout/KeyValueGeneric.tsx diff --git a/src/css/KeyValue.scss b/src/css/KeyValue.scss index 3be4923..2514178 100644 --- a/src/css/KeyValue.scss +++ b/src/css/KeyValue.scss @@ -13,6 +13,10 @@ line-height: $two-row-line-height; } + &.key-value-center .key-value-key { + text-align: center; + } + table { width: 100%; } diff --git a/src/css/Text.scss b/src/css/Text.scss deleted file mode 100644 index ae1a6cb..0000000 --- a/src/css/Text.scss +++ /dev/null @@ -1,23 +0,0 @@ -@import 'variables'; - -.text-component { - height: $default-component-height; - line-height: $default-component-line-height; - padding: 0px 6px; - overflow: hidden; - - .text-component-text { - width: 100%; - white-space: nowrap; - overflow: hidden; - } - - .center-text { - text-align: -webkit-center; - text-align: center; - } - - > table { - width: 100%; - } -} diff --git a/src/layout/KeyValue.tsx b/src/layout/KeyValue.tsx index 8419816..dc8a224 100644 --- a/src/layout/KeyValue.tsx +++ b/src/layout/KeyValue.tsx @@ -1,7 +1,6 @@ import * as React from "react"; import * as LiveSplit from "../livesplit-core"; -import { colorToCss, gradientToCss } from "../util/ColorUtil"; -import { map } from "../util/OptionUtil"; +import KeyValueGeneric, { KeyValueDisplay } from "./KeyValueGeneric"; import "../css/KeyValue.scss"; @@ -9,54 +8,16 @@ export interface Props { state: LiveSplit.KeyValueComponentStateJson } export default class KeyValue extends React.Component { public render() { - const keyCell = - {this.props.state.key} - ; - - const valueCell = - {this.props.state.value} - ; - - let keyValueRows; - if (this.props.state.display_two_rows) { - keyValueRows = <> - - {keyCell} - - - {valueCell} - - ; - } else { - keyValueRows = - {keyCell} - {valueCell} - ; - } - - return ( -
- - - {keyValueRows} - -
-
- ); + return ; } } diff --git a/src/layout/KeyValueGeneric.tsx b/src/layout/KeyValueGeneric.tsx new file mode 100644 index 0000000..87556e2 --- /dev/null +++ b/src/layout/KeyValueGeneric.tsx @@ -0,0 +1,82 @@ +import * as React from "react"; +import * as LiveSplit from "../livesplit-core"; + +import { colorToCss, gradientToCss } from "../util/ColorUtil"; +import { map } from "../util/OptionUtil"; + +import "../css/KeyValue.scss"; + +export enum KeyValueDisplay { + Center, + SplitOneRow, + SplitTwoRows, +} + +export interface Props { + display: KeyValueDisplay, + keyColor: LiveSplit.Color | null, + keyText: string, + valueColor: LiveSplit.Color | null, + valueText: string | null, + wrapperBackground: LiveSplit.Gradient, +} + +export default class KeyValueGeneric extends React.Component { + public render() { + const keyCell = + {this.props.keyText} + ; + + const valueCell = + {this.props.valueText} + ; + + let keyValueRows, wrapperClassName; + if (this.props.display == KeyValueDisplay.Center) { + keyValueRows = {keyCell}; + wrapperClassName = "key-value-center"; + } + else if (this.props.display == KeyValueDisplay.SplitTwoRows) { + keyValueRows = <> + + {keyCell} + + + {valueCell} + + ; + wrapperClassName = "key-value-two-rows"; + } else { + keyValueRows = + {keyCell} + {valueCell} + ; + wrapperClassName = ""; + } + + return ( +
+ + + {keyValueRows} + +
+
+ ); + } +} diff --git a/src/layout/Text.tsx b/src/layout/Text.tsx index 4392afd..f6552d9 100644 --- a/src/layout/Text.tsx +++ b/src/layout/Text.tsx @@ -1,9 +1,7 @@ import * as React from "react"; import * as LiveSplit from "../livesplit-core"; -import { gradientToCss, colorToCss } from "../util/ColorUtil"; -import { map } from "../util/OptionUtil"; +import KeyValueGeneric, { KeyValueDisplay } from "./KeyValueGeneric"; -import "../css/Text.scss"; import "../css/KeyValue.scss"; export interface Props { state: LiveSplit.TextComponentStateJson } @@ -14,53 +12,29 @@ export default class Text extends React.Component { } public render() { - const { background, text } = this.props.state; + const { text } = this.props.state; - const rendered = "Center" in text ? ( - - - {text.Center} - - - ) : ( - - - {text.Split[0]} - - - {text.Split[1]} - - - ); + if ("Center" in text) { + return ; + } - return ( -
- - - {rendered} - -
-
- ); + return ; } }