[Question] Get permission from AbilityContext to use it in state or as prop
See original GitHub issueI want to set a form to readonly by wrapping it in a <fieldset disable={someState}>
tag.
Now I wonder if I can use the AbilityContext which already holds the correct Ability instance to check if for a specific user the form should be en- or disabled. For sure I can create a new Ability object based on the permissions stored for the logged in user in the class component that contains the form but this would somehow mean to duplicate the Ability instance.
Here is a short code snippet which visualizes the described scenario in the first line of the render function:
import React from 'react';
import { Form, Input } from 'antd';
import PropTypes from 'prop-types';
class WrappedEditor extends React.Component {
constructor( props ) {
super(props);
this.state = {
editable: false,
};
}
render() {
const { getFieldDecorator } = this.props.form;
return (
// TODO: How can I use the AbilityContext to set fileset :: disabled to a dynamic permission based value?
<fieldset disabled={!this.state.editable}>
<Form id="editor">
{getFieldDecorator('someText', {
rules: [{ required: true, message: 'Please enter a text', initialValue: this.props.editableObject.someText }],
})}
<Form.Item label="Some Text">
<Input/>
</Form.Item>
</Form>
</fieldset>
);
}
}
WrappedEditor.propTypes = {
form: PropTypes.object.isRequired,
editableObject: PropTypes.object.required,
};
const Editor = Form.create()(WrappedEditor);
export {Editor};
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
casl/react
Getting Started. This package provides Can component which can be used to conditionally show UI elements based on user abilities.
Read more >CASL React
CASL (pronounced /ˈkæsəl/, like castle) is an isomorphic authorization JavaScript library which restricts what resources a given user is allowed to access.
Read more >stalniy-casl/casl - Gitter
Hi guys does anyone have any pointers using '@casl/react' with redux? ... store={store}> <LocalizeProvider store={this.state.store}> <AbilityContext.
Read more >Unexpected double redirect react Router v6
I am using react-router V6 for navigation and noticed strange behavior in navigation. When you go to a new page, the following happens:...
Read more >Chapter 5 - Advanced Degree or Exceptional Ability
The beneficiary must have not only had the advanced degree or its equivalent on the date that the permanent labor certification application was...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thank you for thinking along. The missing link in my brain was about the programatically access to the AbilityContext within a component. I now defined the static
ContextType
within my component and then can access the ability withthis.context.ability
.I guess this is the way to go with the new Context API even though
this.context
is marked as deprecated in my IDE. Might this have something to do withcreateContextualCan
returning aStatelessComponent
which is also marked as deprecated?Thanks for the hint. I will not update this until the next major release. Don’t want to break types for users who uses older version of react