0.0.9 • Published 2 years ago

react-guards v0.0.9

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

react-guards

Read this in other languages: 简体中文

Build Status NPM Version Downloads license minizipped

The guard component can control whether the component can be displayed according to the permissions.

API

Component API

PropertyDescriptionTypeDefault
targettarget valuestring|string[]|Promise|() => Promisenull
currentcurrent valuestring|string[]|'*'null
errComponentDisplay component on errorReactComponentnull
loadingComponentDisplay component on loadReactComponentnull

Function

NameDescriptionType
setGlobalCurrentSet global current, do not pass current value by default use globalCurrent(current: string | string[]) => void
canPassGuardDetermine whether it can pass the guard based on the passed data({target, current}) => Promise

Installation

npm install react-guards

or

yarn add react-guards

Usage

import {ReactGuards} from 'react-guards'

// Show components without passing any data
<ReactGuards>
    213
    <div>324</div>
    <button onClick={() => alert(0)}>213213</button>
</ReactGuards>

Basic

import {ReactGuards} from 'react-guards'

// none
<ReactGuards target='213' current='22'>
    213
    <div>324</div>
    <button onClick={() => alert(0)}>213213</button>
</ReactGuards>

// show
<ReactGuards target='213' current='22,213'>
    213
    <div>324</div>
    <button onClick={() => alert(0)}>213213</button>
</ReactGuards>

// none
<ReactGuards target='213' current={['22']}>
    213
    <div>324</div>
    <button onClick={() => alert(0)}>213213</button>
</ReactGuards>

// show
<ReactGuards target='213' current={['22', '213']}>
    213
    <div>324</div>
    <button onClick={() => alert(0)}>213213</button>
</ReactGuards>

// none
<ReactGuards target={Promise.resolve(false)} current='22,213'>
    213
    <div>324</div>
    <button onClick={() => alert(0)}>213213</button>
</ReactGuards>

// show
<ReactGuards target={Promise.resolve(true)} current='22,213'>
    213
    <div>324</div>
    <button onClick={() => alert(0)}>213213</button>
</ReactGuards>

// show
<ReactGuards target={(current) => Promise.resolve(true)} current='22,213'>
    213
    <div>324</div>
    <button onClick={() => alert(0)}>213213</button>
</ReactGuards>

Global current

import { ReactGuards, setGlobalCurrent as setGlobalCurrentForReactGuard } from 'react-guards'

setGlobalCurrentForReactGuard('22,213')

// show
<ReactGuards target='22'>
    213
    <div>324</div>
    <button onClick={() => alert(0)}>213213</button>
</ReactGuards>

error and loading

import { ReactGuards, setGlobalCurrent as setGlobalCurrentForReactGuard } from 'react-guards'

const waitThenError = (): Promise<boolean> => {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(false)
    }, 2000);
  })
}

const Load = () => <div>Loading</div>

const Error = () => <div>Error</div>

<ReactGuards target={waitThenError} errComponent={Error} loadingComponent={Load}>
    213
    <div>324</div>
    <button onClick={() => alert(0)}>213213</button>
</ReactGuards>
0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago