@8base-react/permissions-provider 中文文档教程

发布于 3年前 浏览 22 项目主页 更新于 3年前

8base Table Schema Provider

Table Schema Provider 获取 8base 表模式的列表,并通过 React Context 提供它。

API

Table of Contents

PermissionsProvider

扩展 React.Component

Provider 以获得 8base 用户权限

Properties

  • children Function Children of the provider. Could be either react node or function with loading state.

Usage

Programatic usage

import React from 'react';
import { PermissionsProvider, PermissionContext, isAllowed } from '@8base/permission-provider';

class MyComponent extends React.Component {
  static contextType = PermissionContext;

  render() {
    const allowed = isAllowed({
      resource: 'schema',
      type: 'custom',
      permission: 'edit'
    }, this.context);

    return <Button disabled={!allowed}>Edit Schema!</Button>
  }
}

const Application = () => (
  <PermissionsProvider>
    {
      ({ loading }) => loading ? 'Loading...' : (
        <MyComponent />
      )
    }
  </PermissionsProvider>
);

Conditional rendering by schema permission

import React from 'react';
import { PermissionsProvider, IfAllowed } from '@8base/permission-provider';

const MyComponent = () => (
  <IfAllowed permissions={[['custom', 'schema', 'edit']]}>
    <Button>Edit Schema!</Button>
  </IfAllowed>
);

const Application = () => (
  <PermissionsProvider>
    {
      ({ loading }) => loading ? 'Loading...' : (
        <MyComponent />
      )
    }
  </PermissionsProvider>
);

Conditional rendering by data permission

import React from 'react';
import { IfAllowed } from '@8base/permission-provider';

const MyComponent = () => (
  <IfAllowed permissions={[['data', 'Clients', 'create']]}>
    <Button>Create Client!</Button>
  </IfAllowed>
);

const Application = () => (
  <PermissionsProvider>
    {
      ({ loading }) => loading ? 'Loading...' : (
        <MyComponent />
      )
    }
  </PermissionsProvider>
);

Usage with render props

import React from 'react';
import { IfAllowed } from '@8base/permission-provider';

const MyComponent = () => (
  <IfAllowed permissions={[['custom', 'schema', 'edit']]}>
    {
      (allowed) => <Button disabled={!allowed}>Edit Schema!</Button>
    }
  </IfAllowed>
);

const Application = () => (
  <PermissionsProvider>
    {
      ({ loading }) => loading ? 'Loading...' : (
        <MyComponent />
      )
    }
  </PermissionsProvider>
);

8base Table Schema Provider

The Table Schema Provider fetching list of 8base table schemas and provide it via React Context.

API

Table of Contents

PermissionsProvider

Extends React.Component

Provider for 8base user permissions

Properties

  • children Function Children of the provider. Could be either react node or function with loading state.

Usage

Programatic usage

import React from 'react';
import { PermissionsProvider, PermissionContext, isAllowed } from '@8base/permission-provider';

class MyComponent extends React.Component {
  static contextType = PermissionContext;

  render() {
    const allowed = isAllowed({
      resource: 'schema',
      type: 'custom',
      permission: 'edit'
    }, this.context);

    return <Button disabled={!allowed}>Edit Schema!</Button>
  }
}

const Application = () => (
  <PermissionsProvider>
    {
      ({ loading }) => loading ? 'Loading...' : (
        <MyComponent />
      )
    }
  </PermissionsProvider>
);

Conditional rendering by schema permission

import React from 'react';
import { PermissionsProvider, IfAllowed } from '@8base/permission-provider';

const MyComponent = () => (
  <IfAllowed permissions={[['custom', 'schema', 'edit']]}>
    <Button>Edit Schema!</Button>
  </IfAllowed>
);

const Application = () => (
  <PermissionsProvider>
    {
      ({ loading }) => loading ? 'Loading...' : (
        <MyComponent />
      )
    }
  </PermissionsProvider>
);

Conditional rendering by data permission

import React from 'react';
import { IfAllowed } from '@8base/permission-provider';

const MyComponent = () => (
  <IfAllowed permissions={[['data', 'Clients', 'create']]}>
    <Button>Create Client!</Button>
  </IfAllowed>
);

const Application = () => (
  <PermissionsProvider>
    {
      ({ loading }) => loading ? 'Loading...' : (
        <MyComponent />
      )
    }
  </PermissionsProvider>
);

Usage with render props

import React from 'react';
import { IfAllowed } from '@8base/permission-provider';

const MyComponent = () => (
  <IfAllowed permissions={[['custom', 'schema', 'edit']]}>
    {
      (allowed) => <Button disabled={!allowed}>Edit Schema!</Button>
    }
  </IfAllowed>
);

const Application = () => (
  <PermissionsProvider>
    {
      ({ loading }) => loading ? 'Loading...' : (
        <MyComponent />
      )
    }
  </PermissionsProvider>
);
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文