1.13.10 • Published 6 years ago

@bearer/react v1.13.10

Weekly downloads
88
License
MIT
Repository
github
Last release
6 years ago

@bearer/react

React tooling for bearer.sh components

fromBearer

This componet takes html tag name of a componet as well as optional type information to produce a react component that can we re-used throughout an application.

export const MyText = fromBearer<{text?:string}>('bearer-my-text')

render() {
  <MyText text='hello world'/>
}

Output events can be handled by simply adding the eventname as a prop on the component and adding a basic handler

<Share 
  bearer-uuid-feature-shared={this.onShared}
/>  

For these tags to work correctly they must have a parent BearerProvider

BearerProvider

This component maintains a shared state for a group of components as well as adding the bearer script tags to the page. This tag can be added at any level of the application as is convenient for the implementation

Example Use

we will use this scenario for our examples:

First on the preview page obtain a setup-id from the setup componets so we do not need to include these in our page. Next lets define our components in a constants file:

bearer.ts

import { fromBearer } from '@bearer/react'

export const ChannelSelect = fromBearer('bearer-6d29c4-share-slack-beta-4-channel-action')
export const Share = fromBearer<{message?:string, text?:string}>('bearer-6d29c4-share-slack-beta-4-feature-action')
export const SlackConnect = fromBearer('bearer-6d29c4-share-slack-beta-4-connect-action')

We can include the BearerProvider at any level but in this example lets use it all together in the same component

slack-share-component.tsx

import { BearerProvider } from '@bearer/react'
import {SlackConnect, ChannelSelect, Share} from '../../constants/bearer'

export default class SlackShareSetup extends React.Component{
  public render(){
    const intialContext = {
        'setup-id': 'd30d9d50-0307-11e9-a3e1-ff7ca347ebae',
    }
    return (
      <BearerProvider
        clientId='a3efd4de0d7fbf1b54ecd29291e790e0a3edece0ee5db29e99'
        initialContext={intialContext}
      >
        <SlackConnect />
        <ChannelSelect />
        <Share 
          message="hello world!" 
          text="Test!" 
          bearer-6d29c4-share-slack-beta-4-feature-shared={this.onShared}
        />   
      </BearerProvider>
    )
  }
}

This allows to share messages but what if we want to persist the users setup information. @bearer/react provides two methods to acomplish this.

Using onUpdate

BearerProvider has an onUpdatecallback we can hook into which is called every time data changes within the provider

slack-share-component.tsx

import { BearerProvider } from '@bearer/react'
import {SlackConnect, ChannelSelect, Share} from '../../constants/bearer'

export default class SlackShareSetup extends React.Component{
  public render(){
    const intialContext = {
        'setup-id': 'd30d9d50-0307-11e9-a3e1-ff7ca347ebae',
    }
    return (
      <BearerProvider
        clientId='a3efd4de0d7fbf1b54ecd29291e790e0a3edece0ee5db29e99'
        initialContext={intialContext}
        onUpdate={(data:any)=>{
          this.setState({data})
        }}
      >
        <SlackConnect />
        <ChannelSelect />
        <Share 
          message="hello world!" 
          text="Test!" 
          bearer-6d29c4-share-slack-beta-4-feature-shared={this.onShared}
        />  
        <button onClick={this.handleSave}>Save Setup</button> 
      </BearerProvider>
      
    )
  }
  private handleSave = () => (
    console.log('handleSave', this.state.data)
  )
}

using a context consumer

Internally BearerProvider uses the react context API. For a more advaned but flexable method we can access the consumer directly via BearerContext and then use the currently set details as we wish.

slack-share-component.tsx

import { BearerProvider, BearerContext } from '@bearer/react'
import {SlackConnect, ChannelSelect, Share} from '../../constants/bearer'

export default class SlackShareSetup extends React.Component{
  public render(){
    const intialContext = {
        'setup-id': 'd30d9d50-0307-11e9-a3e1-ff7ca347ebae',
    }
    return (
      <BearerProvider
        clientId='a3efd4de0d7fbf1b54ecd29291e790e0a3edece0ee5db29e99'
        initialContext={intialContext}
      >
        <SlackConnect />
        <ChannelSelect />
        <Share 
          message="hello world!" 
          text="Test!" 
          bearer-6d29c4-share-slack-beta-4-feature-shared={this.onShared}
        />
        <BearerContext.Consumer>
          {
            (context)=> (
                <button onClick={this.handleSave(context.state)}>Save Setup</button>
            )
          }
        </BearerContext.Consumer> 
      </BearerProvider>
    )
  }
  private handleSave = (data: any) => (
    () => {
      console.log('handleSave', data)
    }
  )
}
2.2.2

6 years ago

2.1.0

6 years ago

2.0.0

6 years ago

1.13.10

6 years ago

1.13.9

6 years ago

1.13.8

6 years ago

1.13.5

6 years ago

1.13.4

6 years ago

1.9.0

6 years ago

1.8.0

6 years ago

1.7.1-canary.2

6 years ago

1.8.0-canary.2

6 years ago

1.7.0

6 years ago

1.6.8

6 years ago

1.6.5

6 years ago

1.6.4

6 years ago

1.6.2-canary-4.1

6 years ago

1.6.1

6 years ago

1.4.2-canary.17

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.115.1

6 years ago

0.115.0

6 years ago

0.114.1

6 years ago

0.113.0

6 years ago

0.112.0

6 years ago

0.111.0-canary.9

6 years ago

0.111.0

6 years ago

0.111.0-canary.2

6 years ago

0.110.0

6 years ago

0.109.0

6 years ago

0.108.0

6 years ago

0.107.3

6 years ago

0.107.1

6 years ago

0.107.0

6 years ago

0.107.0-canary.1

6 years ago

0.107.0-canary.0

6 years ago

0.107.0-canary.6

6 years ago

0.106.0

6 years ago

0.106.0-canary.9

6 years ago

0.106.0-canary.5

6 years ago

0.106.0-canary.1

6 years ago

0.105.0

6 years ago

0.104.0

6 years ago

0.103.0

6 years ago

0.102.0

6 years ago

0.101.1

6 years ago

0.101.0

6 years ago

0.100.1-next.40

6 years ago

0.100.1-next.35

6 years ago

0.100.1-next.48

6 years ago

0.100.1-next.47

6 years ago

0.100.1-next.27

6 years ago

0.100.1-next.25

6 years ago

0.100.1-next.21

6 years ago

0.100.1-next.23

6 years ago

0.100.1-next.17

6 years ago

0.100.1-next.15

6 years ago

0.100.1-next.1

6 years ago

0.100.0

6 years ago

0.99.2

6 years ago

0.99.1

6 years ago

0.99.0

6 years ago

0.98.0

6 years ago

0.97.3

6 years ago

0.97.2

6 years ago

0.97.0

6 years ago

0.96.2-next.13

6 years ago

0.96.1

6 years ago

0.96.0

6 years ago

0.95.1

6 years ago

0.95.0

6 years ago

0.94.1

6 years ago

0.94.0

6 years ago

0.93.0

6 years ago

0.92.2

6 years ago

0.92.1

6 years ago

0.92.0

6 years ago

0.91.5

6 years ago

0.91.0

6 years ago

0.90.2

6 years ago

0.90.1

6 years ago

0.90.0

6 years ago

0.89.0

6 years ago

0.88.0

6 years ago

0.87.0

6 years ago

0.86.0

7 years ago

0.85.4

7 years ago

0.85.0

7 years ago

0.84.2

7 years ago

0.84.1

7 years ago

0.84.0

7 years ago

0.83.4

7 years ago

0.83.3

7 years ago

0.83.0

7 years ago

0.82.0

7 years ago

0.81.7

7 years ago