Skip to content

Commit

Permalink
feat(Text): use new Bara hook piper
Browse files Browse the repository at this point in the history
  • Loading branch information
nampdn committed Apr 26, 2019
1 parent 7844f5a commit f7bc046
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 13 deletions.
32 changes: 30 additions & 2 deletions src/lib/exports/Text/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,21 @@ 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) => {
(
{
onPress: _onPress,
onLongPress: _onLongPress,
hasOnPress,
hasOnLongPress,
...props
}: TextProps,
ref: any,
) => {
const context = useBaraContext()
const onPress: typeof _onPress = e => {
context.components.text.onPress({ name, ...props })
Expand All @@ -21,7 +32,24 @@ export const Text = React.forwardRef(
}
}

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

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
4 changes: 2 additions & 2 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'

import { BaraBaseComponentProps } from '../../models'

export interface BaraReactText {
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 }
})
}
1 change: 1 addition & 0 deletions src/lib/models/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface BaraBaseComponentProps {
data?: any
name?: string
kind?: string
event?: any
}

export type Omit<T, K> = Pick<T, Exclude<keyof T, K>>

0 comments on commit f7bc046

Please sign in to comment.