Skip to content

Commit

Permalink
Merge pull request #2850 from travisMichael/disable_certain_fields_in…
Browse files Browse the repository at this point in the history
…_simple_form_iterator

added ability to pass in disableRemove property in SimpleFormIterator…
  • Loading branch information
fzaninotto authored Mar 7, 2019
2 parents 50944c5 + e4fe385 commit 1356103
Showing 1 changed file with 13 additions and 2 deletions.
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 @@ -152,7 +163,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 @@ -213,7 +224,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

0 comments on commit 1356103

Please sign in to comment.