Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #86 from infinitered/style-arrays
Browse files Browse the repository at this point in the history
Allow style arrays in HOCs (WIP)
  • Loading branch information
morgandonze authored Oct 5, 2018
2 parents a85251f + c469790 commit 1b45dbc
Show file tree
Hide file tree
Showing 20 changed files with 1,270 additions and 127 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ node_modules
testgrounds
IntegrationTest
integration_test
ios/Pods
4 changes: 2 additions & 2 deletions boilerplate/src/views/shared/button/button.props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ export interface ButtonProps extends TouchableOpacityProperties {
/**
* An optional style override useful for padding & margin.
*/
style?: ViewStyle
style?: ViewStyle | ViewStyle[]

/**
* An optional style override useful for the button text.
*/
textStyle?: TextStyle
textStyle?: TextStyle | TextStyle[]

/**
* One of the different types of text presets.
Expand Down
20 changes: 20 additions & 0 deletions boilerplate/src/views/shared/button/button.story.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import * as React from "react"
import { ViewStyle } from "react-native"
import { storiesOf } from "@storybook/react-native"
import { StoryScreen, Story, UseCase } from "../../../../storybook/views"
import { Button } from "./"

const buttonStyleArray: ViewStyle[] = [
{paddingVertical: 100},
{borderRadius: 0},
]

const buttonTextStyleArray: TextStyle[] = [
{fontSize: 20},
{color: "#a511dc"},
]

storiesOf("Button", module)
.addDecorator(fn => <StoryScreen>{fn()}</StoryScreen>)
.add("Style Presets", () => (
Expand All @@ -13,5 +24,14 @@ storiesOf("Button", module)
<UseCase text="Disabled" usage="The disabled behaviour of the primary button.">
<Button text="Click It" preset="primary" onPress={() => window.alert("pressed")} disabled />
</UseCase>
<UseCase text="Array Style" usage="Button with array style" >
<Button
text="Click It"
preset="primary"
onPress={() => window.alert("pressed")}
style={buttonStyleArray}
textStyle={buttonTextStyleArray}
/>
</UseCase>
</Story>
))
20 changes: 18 additions & 2 deletions boilerplate/src/views/shared/button/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TouchableOpacity } from "react-native"
import { Text } from "../text"
import { viewPresets, textPresets } from "./button.presets"
import { ButtonProps } from "./button.props"
import { reduce } from "ramda"

/**
* For your text displaying needs.
Expand All @@ -17,8 +18,23 @@ export function Button(props: ButtonProps) {
const viewPresetToUse = viewPresets[preset] || viewPresets.primary
const textPresetToUse = textPresets[preset] || textPresets.primary

const viewStyle = { ...viewPresetToUse, ...styleOverride }
const textStyle = { ...textPresetToUse, ...textStyleOverride }
let viewStyle
if (Array.isArray(styleOverride)) {
viewStyle = reduce((acc,term) => {
return { ...acc, ...term }
}, viewPresetToUse, styleOverride)
} else {
viewStyle = { ...viewPresetToUse, ...styleOverride }
}

let textStyle
if (Array.isArray(textStyleOverride)) {
textStyle = reduce((acc,term) => {
return { ...acc, ...term }
}, textPresetToUse, textStyleOverride)
} else {
textStyle = { ...textPresetToUse, ...textStyleOverride }
}

const content = children || <Text tx={tx} text={text} style={textStyle} />

Expand Down
6 changes: 3 additions & 3 deletions boilerplate/src/views/shared/checkbox/checkbox.props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ export interface CheckboxProps {
/**
* Additional container style. Useful for margins.
*/
style?: ViewStyle
style?: ViewStyle | ViewStyle[]

/**
* Additional outline style.
*/
outlineStyle?: ViewStyle
outlineStyle?: ViewStyle | ViewStyle[]

/**
* Additional fill style. Only visible when checked.
*/
fillStyle?: ViewStyle
fillStyle?: ViewStyle | ViewStyle[]

/**
* Is the checkbox checked?
Expand Down
32 changes: 31 additions & 1 deletion boilerplate/src/views/shared/checkbox/checkbox.story.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import * as React from "react"
import { View } from "react-native"
import { View, ViewStyle } from "react-native"
import { storiesOf } from "@storybook/react-native"
import { StoryScreen, Story, UseCase } from "../../../../storybook/views"
import { Checkbox } from "./"
import { Toggle } from "react-powerplug"

const arrayStyle: ViewStyle[] = [
{paddingVertical: 40},
{alignSelf: "flex-end"},
]
const arrayOutlineStyle: ViewStyle[] =
[
{borderColor: "#b443c9"},
{borderWidth: 25},
]
const arrayFillStyle: ViewStyle[] = [
{backgroundColor: "#55e0ff"},
]

storiesOf("Checkbox", module)
.addDecorator(fn => <StoryScreen>{fn()}</StoryScreen>)
.add("Behaviour", () => (
Expand Down Expand Up @@ -91,5 +104,22 @@ storiesOf("Checkbox", module)
)}
</Toggle>
</UseCase>

<UseCase text="Array style" usage="Use array styles">
<Toggle initial={false}>
{({ on, toggle }) => (
<View>
<Checkbox
text="Check it twice"
value={on}
onToggle={toggle}
style={arrayStyle}
outlineStyle={arrayOutlineStyle}
fillStyle={arrayFillStyle}
/>
</View>
)}
</Toggle>
</UseCase>
</Story>
))
31 changes: 28 additions & 3 deletions boilerplate/src/views/shared/checkbox/checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TouchableOpacity, TextStyle, ViewStyle, View } from "react-native"
import { Text } from "../text"
import { color, spacing } from "../../../theme"
import { CheckboxProps } from "./checkbox.props"
import { reduce } from "ramda"

const ROOT: ViewStyle = {
flexDirection: "row",
Expand Down Expand Up @@ -32,9 +33,33 @@ const LABEL: TextStyle = { paddingLeft: spacing[2] }

export function Checkbox(props: CheckboxProps) {
const numberOfLines = props.multiline ? 0 : 1
const rootStyle = { ...ROOT, ...props.style } as ViewStyle
const outlineStyle = { ...OUTLINE, ...props.outlineStyle } as ViewStyle
const fillStyle = { ...FILL, ...props.fillStyle } as ViewStyle

let rootStyle
if (Array.isArray(props.style)) {
rootStyle = reduce((acc,term) => {
return { ...acc, ...term }
}, ROOT, props.style)
} else {
rootStyle = { ...ROOT, ...props.style } as ViewStyle
}

let outlineStyle
if (Array.isArray(props.outlineStyle)) {
outlineStyle = reduce((acc,term) => {
return { ...acc, ...term }
}, OUTLINE, props.outlineStyle)
} else {
outlineStyle = { ...OUTLINE, ...props.outlineStyle } as ViewStyle
}

let fillStyle
if (Array.isArray(props.fillStyle)) {
fillStyle = reduce((acc,term) => {
return { ...acc, ...term }
}, FILL, props.fillStyle)
} else {
fillStyle = { ...FILL, ...props.fillStyle } as ViewStyle
}
const onPress = props.onToggle ? () => props.onToggle && props.onToggle(!props.value) : null

return (
Expand Down
2 changes: 1 addition & 1 deletion boilerplate/src/views/shared/form-row/form-row.props.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface FormRowProps {
/**
* Override the container style... useful for margins and padding.
*/
style?: ViewStyle
style?: ViewStyle | ViewStyle[]

/**
* The type of border.
Expand Down
14 changes: 14 additions & 0 deletions boilerplate/src/views/shared/form-row/form-row.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import { StoryScreen, Story, UseCase } from "../../../../storybook/views"
import { FormRow } from "./form-row"
import { Text } from "../text"
import { color } from "../../../theme/color"
import { ViewStyle } from "react-native"

const TEXT_STYLE_OVERRIDE = {
color: color.storybookTextColor,
}
const arrayStyle: ViewStyle[] = [
{borderWidth: 5},
{borderColor: "#32cd32"},
]

storiesOf("FormRow", module)
.addDecorator(fn => <StoryScreen>{fn()}</StoryScreen>)
Expand Down Expand Up @@ -86,3 +91,12 @@ storiesOf("FormRow", module)
</UseCase>
</Story>
))
.add("Styling", () => (
<Story>
<UseCase text="Style array" usage="Form row with an array of styles">
<FormRow preset="soloStraight" style={arrayStyle}>
<Text style={TEXT_STYLE_OVERRIDE}>Array style.</Text>
</FormRow>
</UseCase>
</Story>
))
17 changes: 13 additions & 4 deletions boilerplate/src/views/shared/form-row/form-row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@ import * as React from "react"
import { View } from "react-native"
import { PRESETS } from "./form-row.presets"
import { FormRowProps } from "./form-row.props"
import { reduce } from "ramda"

/**
* A horizontal container component used to hold a row of a form.
*/
export function FormRow(props: FormRowProps) {
let viewStyle
if (Array.isArray(props.style)) {
viewStyle = reduce((acc,term) => {
return { ...acc, ...term }
}, PRESETS[props.preset], props.style)
} else {
viewStyle = {
...PRESETS[props.preset],
...props.style,
}
}
return (
<View
style={{
...PRESETS[props.preset],
...props.style,
}}
style={viewStyle}
>
{props.children}
</View>
Expand Down
10 changes: 5 additions & 5 deletions boilerplate/src/views/shared/switch/switch.props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ export interface SwitchProps {
/**
* A style override to apply to the container. Useful for margins and paddings.
*/
style?: ViewStyle
style?: ViewStyle | ViewStyle[]

/**
* Additional track styling when on.
*/
trackOnStyle?: ViewStyle
trackOnStyle?: ViewStyle | ViewStyle[]

/**
* Additional track styling when off.
*/
trackOffStyle?: ViewStyle
trackOffStyle?: ViewStyle | ViewStyle[]

/**
* Additional thumb styling when on.
*/
thumbOnStyle?: ViewStyle
thumbOnStyle?: ViewStyle | ViewStyle[]

/**
* Additional thumb styling when off.
*/
thumbOffStyle?: ViewStyle
thumbOffStyle?: ViewStyle | ViewStyle[]
}
68 changes: 67 additions & 1 deletion boilerplate/src/views/shared/switch/switch.story.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,51 @@
import * as React from "react"
import { View } from "react-native"
import { View, ViewStyle } from "react-native"
import { storiesOf } from "@storybook/react-native"
import { StoryScreen, Story, UseCase } from "../../../../storybook/views"
import { Toggle } from "react-powerplug"
import { Switch } from "."

const styleArray: ViewStyle[] = [
{borderColor: "#686868"},
]

const trackOffStyle: ViewStyle[] = [
{backgroundColor: "#686868"},
{
height: 80,
borderRadius: 0,
},
]
const trackOnStyle: ViewStyle[] = [
{
backgroundColor: "#b1008e",
borderColor: "#686868",
},
{
height: 80,
borderRadius: 0,
},

]
const thumbOffStyle: ViewStyle[] = [
{
backgroundColor: "#b1008e",
borderColor: "#686868",
},
{
height: 80,
borderRadius: 0,
},
]
const thumbOnStyle: ViewStyle[] = [
{backgroundColor: "#f0c"},
{
height: 80,
borderRadius: 0,
borderColor: "#686868",
},
]

storiesOf("Switch", module)
.addDecorator(fn => <StoryScreen>{fn()}</StoryScreen>)
.add("Behaviour", () => (
Expand Down Expand Up @@ -40,5 +81,30 @@ storiesOf("Switch", module)
)}
</Toggle>
</UseCase>

<UseCase text="Style array" usage="This either.">
<Toggle initial={false}>
{({ on, toggle }) => (
<View>
<Switch
style={styleArray}

trackOffStyle={trackOffStyle}
trackOnStyle={trackOnStyle}
thumbOffStyle={thumbOffStyle}
thumbOnStyle={thumbOnStyle}

// trackOnStyle={{ backgroundColor: "green", borderColor: "black" }}
// trackOffStyle={{ backgroundColor: "red", borderColor: "maroon" }}
// thumbOnStyle={{ backgroundColor: "cyan" }}
// thumbOffStyle={{ backgroundColor: "pink" }}

value={on}
onToggle={toggle}
/>
</View>
)}
</Toggle>
</UseCase>
</Story>
))
Loading

0 comments on commit 1b45dbc

Please sign in to comment.