Caslin React
A tool library for making use Caslin in React easier.
English | 中文
Installation
npm install @caslin/feature @caslin/react --save
Getting start
Assume that a feature.js
file already exists, which exposes the feature instance generated by FeatureBuilder.define().
1. <Can> component
The attributes is the same as calling style of @caslin/feature. When the checking result is true, the children of the <Can>
component is rendered, you could pass a fallback
render prop as fallback element.
import { Can } from '@caslin/react';
import feature from './feature';
<Can env="featEnv1" action="create" subject="Article" feature={feature}>
{() => <button onClick={this.createArticle.bind(this)}>Create Article</button>}
</Can>
<Can not env="featEnv1" action="create" subject="Article" feature={feature}>
{() => <button onClick={this.createArticle.bind(this)}>Create Article</button>}
</Can>
<Can passThrough env="featEnv1" action="create" subject="Article" feature={feature}>
{(match) => <button disabled={match} onClick={this.createArticle.bind(this)}>Create Article</button>}
</Can>
<Can env="featEnv1" action="create" subject="Article" feature={feature}>
<button onClick={this.createArticle.bind(this)}>Create Article</button>
</Can>
<Can env="featEnv1" action="create" subject="Article" feature={feature} fallback={<div>You don't have permissioin to create Article.</div>}>
<button>Create Article</button>
</Can>
2. <Env> component
In general case, when this component is used, the feature.setEnv() is called and the current default environment is set, you could also pass a fallback
render prop as fallback element.
import { Env } from '@caslin/react';
import feature from './feature';
<Env is="featEnv1" feature={feature}>
{() => <div>feature env 1</div>}
</Env>
<Env not="featEnv1" feature={feature}>
{() => <div>feature env 2</div>}
</Env>
<Env in={['featEnv1', 'featEnv2']} feature={feature}>
{() => <div>feature env 1</div>}
</Env>
<Env notIn={['featEnv2', 'featEnv3']} feature={feature}>
{() => <div>feature env 1</div>}
</Env>
<Env passThrough is="featEnv1" feature={feature}>
{(match) => <div>{match ? 'is' : 'not'} env 1</div>}
</Env>
<Env is="featEnv1" feature={feature} fallback={<div>Current env is not featureEnv1.</div>}>
{() => <div>feature env 1</div>}
</Env>
3. Pass defined "feature" to avoid having to pass it every time
createCheckerBoundTo()
Pass feature
as a parameter to createCheckerBoundTo()
to get the higher-order component.
import { createCheckerBoundTo } from '@caslin/react';
import feature from './feature';
export default createCheckerBoundTo(feature);
You can now use the Can>
and <Env>
directly, instead of passing feature
again:
import featUtil from './featChecker';
const { Can, Env } = featUtil;
<Can action="create" subject="Article">
{() => <button onClick={this.createArticle.bind(this)}>Create Article</button>}
</Can>
<Env is="featEnv1">
{() => <div>feature env 1</div>}
</Env>
createContextualChecker()
Pass Context.Consumer
as a parameter to createContextualChecker()
to get the higher-order component.
import React from 'react';
import { createContextualChecker } from '@caslin/react';
import feature from './feature';
export const FeatContext = React.createContext(feature);
export default createContextualChecker(FeatContext.Consumer);
import feature from './feature';
import { FeatContext } from './featChecker';
...
<FeatContext.Provider value={feature}>
{
</FeatContext.Provider>
...
import featUtil from './featChecker';
const { Can, Env } = featUtil;
<Can action="create" subject="Article">
{() => <button onClick={this.createArticle.bind(this)}>Create Article</button>}
</Can>
<Env is="featEnv1">
{() => <div>feature env 1</div>}
</Env>
License
MIT License