Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added ability to pass in disableRemove property in SimpleFormIterator… #2850

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions packages/ra-ui-materialui/src/form/SimpleFormIterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ export class SimpleFormIterator extends Component {
fields.remove(index);
};

// Returns a boolean to indicate whether to disable the remove button for certain fields.
// If disableRemove is a function, then call the function with the current record to
// determing if the button should be disabled. Otherwise, use a boolean property that
// enables or disables the button for all of the fields.
disableRemoveField = (record, disableRemove) => {
if (typeof disableRemove === "boolean") {
return disableRemove;
}
return disableRemove && disableRemove(record);
}

addField = () => {
const { fields } = this.props;
this.ids.push(this.nextId++);
Expand Down Expand Up @@ -151,7 +162,7 @@ export class SimpleFormIterator extends Component {
/>
))}
</section>
{!disableRemove && (
{!(this.disableRemoveField((records && records[index]) || {}, disableRemove)) && (
<span className={classes.action}>
<Button
className={classNames(
Expand Down Expand Up @@ -212,7 +223,7 @@ SimpleFormIterator.propTypes = {
resource: PropTypes.string,
translate: PropTypes.func,
disableAdd: PropTypes.bool,
disableRemove: PropTypes.bool,
disableRemove: PropTypes.oneOfType([PropTypes.func, PropTypes.bool]),
};

export default compose(
Expand Down