Skip to content

Commit

Permalink
fix(bara): new operators and change hook function name (#20)
Browse files Browse the repository at this point in the history
fix(bara): new operators and change hook function name
  • Loading branch information
nampdn committed Apr 26, 2019
2 parents 79e6aa0 + 9a606e0 commit 319bff7
Show file tree
Hide file tree
Showing 30 changed files with 763 additions and 456 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"src/lib"
],
"dependencies": {
"bara": "2.4.0",
"react-native-web": "0.11.2"
"bara": "^2.4.0",
"react-native-web": "^0.11.2"
},
"devDependencies": {
"@semantic-release/changelog": "3.0.2",
Expand All @@ -27,7 +27,6 @@
"@types/react": "16.8.14",
"@types/react-dom": "16.8.4",
"@types/react-native": "0.57.49",
"@typescript-eslint/typescript-estree": "1.7.0",
"react": "16.8.6",
"react-dom": "16.8.6",
"react-scripts": "2.1.8",
Expand All @@ -40,7 +39,8 @@
},
"peerDependencies": {
"react": ">=16.8",
"react-dom": ">=16.8"
"react-dom": ">=16.8",
"bara": ">=2.4"
},
"scripts": {
"compile": "rm -rf dist && yarn compile:tsc",
Expand Down
14 changes: 13 additions & 1 deletion renovate.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
{
"extends": [
"config:base"
]
],
"packageRules": [
{
"packageName": ["bara"],
"groupName": "bara packages",
"pin": false,
"automerge": true
}
],
"automerge": true,
"major": {
"automerge": false
}
}
3 changes: 2 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const styles = StyleSheet.create({
view: {
flex: 1,
flexDirection: 'row',
backgroundColor: '#963899',
},
button: {
margin: 5,
Expand All @@ -34,7 +35,7 @@ const App = () => {

return (
<BaraProvider>
<View style={styles.view}>
<View name="main-container" style={styles.view}>
<Text name="version">Version: {version}</Text>
<View style={styles.button}>
<Touchable name="welcome-button">
Expand Down
6 changes: 6 additions & 0 deletions src/examples/components/WelcomeText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ export const WelcomeText = () => {
<Touchable name="change-welcome-message" onPress={changeMessage}>
<Text>{message}</Text>
</Touchable>
<Text name="pressable-text" hasOnPress>
Press me! I'm a special Text
</Text>
<Text name="not-pressable-text">
Do not Press me! I'm a Text without any special
</Text>
</View>
)
}
40 changes: 23 additions & 17 deletions src/examples/features/welcome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import {
nameOfText,
nameOfTouchable,
nameOfTouchableOpacity,
useTextPress,
useTouchableOpacityPress,
useTouchablePress,
nameOfView,
whenTextPress,
whenTouchableOpacityPress,
whenTouchablePress,
whenViewLayout,
} from '../../lib'

export function welcomeTrigger() {
Expand All @@ -19,24 +21,28 @@ export function welcomeTrigger() {
})
})

useTouchablePress(
{
nameOf: nameOfTouchable('welcome-button'),
},
({ name }) => {
setBarnState('welcome', `You (${name}) are already welcomed!`)
},
)
whenViewLayout(nameOfView('main-container'))(data => {
// tslint:disbale-next-line
console.log('Main container is updating its layout', data)
})

useTouchableOpacityPress(
{
nameOf: nameOfTouchableOpacity('greet-button'),
},
({ name }) => {
alert(`${name} is PRESSED! YAY !!!`)
whenTouchablePress(nameOfTouchable('welcome-button'))(({ name }) => {
setBarnState('welcome', `You (${name}) are already welcomed!`)
})

whenTouchableOpacityPress(nameOfTouchableOpacity('greet-button'))(
(data: any) => {
alert(`${data.name} is PRESSED! YAY !!!`)
// tslint:disbale-next-line
console.log('greet-button', data)
},
)

whenTextPress(nameOfText('pressable-text'))((data: any) => {
// tslint:disbale-next-line
console.log('pressable-text', data)
})

useTimerElapsed(5, () => {
setBarnState('welcome', `Who are you?`)
})
Expand Down
10 changes: 2 additions & 8 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ import { register, useBarnStream, useInitStream } from 'bara'
import App from './App'
import './index.css'
import {
useComponentsStream,
useReactApp,
useTextStream,
useTouchableOpacityStream,
useTouchableStream,
useViewStream,
} from './lib'

import { welcomeTrigger } from './examples/features/welcome'
Expand All @@ -18,10 +15,7 @@ const BaraApp = () => {
welcome: 'Welcome to Bara React App!',
})
useReactApp({ name: 'bara-app', App })
useViewStream()
useTouchableStream()
useTouchableOpacityStream()
useTextStream()
useComponentsStream({TouchableOpacity: true})
welcomeTrigger()
}

Expand Down
41 changes: 36 additions & 5 deletions src/lib/exports/Text/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,50 @@ import { BaraBaseComponentProps } from '../../models'

export interface TextProps extends TextPropsOriginal, BaraBaseComponentProps {
children?: ReactNode
hasOnPress?: boolean
hasOnLongPress?: boolean
}

export const Text = React.forwardRef(
({ onPress: _onPress, ...props }: TextProps, ref: any) => {
(
{
name,
kind,
onPress: _onPress,
onLongPress: _onLongPress,
hasOnPress,
hasOnLongPress,
...props
}: TextProps,
ref: any,
) => {
const context = useBaraContext()
const onPress: typeof _onPress = e => {
context.components.text.onPress({ name, ...props })
const onPress: typeof _onPress = event => {
event.persist()
context.components.text.onPress({ name, kind, event, ...props })
if (_onPress) {
_onPress(e)
_onPress(event)
}
}

return <TextOriginal {...props} onPress={onPress} ref={ref} />
const onLongPress: typeof _onLongPress = event => {
context.components.text.onLongPress({ name, kind, event, ...props })
if (_onLongPress) {
_onLongPress(event)
}
}

const pressableProps: any = {}

if (hasOnPress) {
pressableProps.onPress = onPress
}

if (hasOnLongPress) {
pressableProps.onLongPress = onLongPress
}

return <TextOriginal {...props} {...pressableProps} ref={ref} />
},
)

Expand Down
7 changes: 4 additions & 3 deletions src/lib/exports/Text/condition.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BaraReactText, TextPressEventFilter } from './event'
import { kindOf, nameOf } from '../../functions/common'

export const nameOfText = (name: string) => (triggeringEvent: BaraReactText): boolean =>
name ? name === triggeringEvent.name : true
export const nameOfText = nameOf

export const kindOfText = kindOf
6 changes: 3 additions & 3 deletions src/lib/exports/Text/event.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createEventType, useEvent } from 'bara'

export interface BaraReactText {
import { BaraBaseComponentProps } from '../../models'

export interface BaraReactText extends BaraBaseComponentProps {
name?: string
className?: string
ref?: any
id?: any
}

export const ON_TEXT_PRESS = createEventType('ON_TEXT_PRESS')
Expand Down
48 changes: 42 additions & 6 deletions src/lib/exports/Text/hook.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,45 @@
import { TextPressEventFilter } from './event'
import {
ActionPipe,
BaraEventPayload,
ConditionPipe,
createPipe,
useAction,
useCondition,
useEvent,
useTrigger,
} from 'bara'

import { BaraReactText, useTextLongPressEvent, useTextPressEvent } from './event'
import { TextPressCallback, useTextPressTrigger } from './trigger'

export function useTextPress(
eventFilter: TextPressEventFilter,
callback: TextPressCallback,
) {
return useTextPressTrigger(eventFilter, callback)
export const whenTextPress = (
...conditions: Array<ConditionPipe<BaraReactText>>
) => (...actions: Array<ActionPipe<BaraReactText>>) => {
const piper = (
data: BaraReactText,
payload: BaraEventPayload<BaraReactText>,
) => {
createPipe(data, payload)(...(conditions as any))(...actions)
}
useTrigger<BaraReactText>(() => {
const event = useTextPressEvent()
const action = useAction(piper)
return { event, action }
})
}

export const whenTextLongPress = (
...conditions: Array<ConditionPipe<BaraReactText>>
) => (...actions: Array<ActionPipe<BaraReactText>>) => {
const piper = (
data: BaraReactText,
payload: BaraEventPayload<BaraReactText>,
) => {
createPipe(data, payload)(...(conditions as any))(...actions)
}
useTrigger<BaraReactText>(() => {
const event = useTextLongPressEvent()
const action = useAction(piper)
return { event, action }
})
}
53 changes: 43 additions & 10 deletions src/lib/exports/Touchable/Touchable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,66 @@ export const TouchableBase = React.forwardRef(
{
TouchableComponent,
onPress: _onPress,
onPressIn: _onPressIn,
onPressOut: _onPressOut,
onLongPress: _onLongPress,
name,
...props
}: TouchableBaseProps,
ref: any,
) => {
const context = useBaraContext()

const onPress: typeof _onPress = e => {
context.components.touchable.onPress({ name, ...props })
context.components.touchable.onPress({ name, ...props, event: e })
if (_onPress) {
_onPress(e)
}
}
return <TouchableComponent {...props} ref={ref} onPress={onPress} />
},
)

export const Touchable = React.forwardRef(
(props: TouchableProps, ref: any) => {
const onPressIn: typeof _onPressIn = e => {
context.components.touchable.onPressIn({ name, ...props, event: e })
if (_onPressIn) {
_onPressIn(e)
}
}

const onPressOut: typeof _onPressOut = e => {
context.components.touchable.onPressOut({ name, ...props, event: e })
if (_onPressOut) {
_onPressOut(e)
}
}

const onLongPress: typeof _onLongPress = e => {
context.components.touchable.onLongPress({ name, ...props, event: e })
if (_onLongPress) {
_onLongPress(e)
}
}

return (
<TouchableBase
ref={ref}
TouchableComponent={TouchableOpacity}
activeOpacity={1}
<TouchableComponent
{...props}
ref={ref}
onPress={onPress}
onPressIn={onPressIn}
onPressOut={onPressOut}
onLongPress={onLongPress}
/>
)
},
)

export const Touchable = React.forwardRef((props: TouchableProps, ref: any) => {
return (
<TouchableBase
ref={ref}
TouchableComponent={TouchableOpacity}
activeOpacity={1}
{...props}
/>
)
})

export type Touchable = TouchableOpacity
9 changes: 3 additions & 6 deletions src/lib/exports/Touchable/condition.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { BaraReactTouchable, TouchableEventFilter } from './event'
import { kindOf, nameOf } from '../../functions/common'

export const nameOfTouchable = (name: string) => (triggeringEvent: BaraReactTouchable): boolean =>
name ? name === triggeringEvent.name : true
export const nameOfTouchable = nameOf

export const classOfTouchable = (className: string) => (
triggeringEvent: BaraReactTouchable,
): boolean => (className ? className === triggeringEvent.className : true)
export const kindOfTouchable = kindOf
6 changes: 3 additions & 3 deletions src/lib/exports/Touchable/event.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createEventType, useEvent } from 'bara'

export interface BaraReactTouchable {
import { BaraBaseComponentProps } from '../../models'

export interface BaraReactTouchable extends BaraBaseComponentProps {
name?: string
className?: string
ref?: any
id?: string
}

export const ON_TOUCHABLE_PRESS = createEventType('ON_TOUCHABLE_PRESSED')
Expand Down
Loading

0 comments on commit 319bff7

Please sign in to comment.