Skip to content

Is there a way to prevent deleting the last remaining rule (e.g. disable remove button) #675

Answered by jakeboone02
roniemartinez asked this question in Q&A
Discussion options

You must be logged in to vote

There are a few ways you could go with this. The code below demonstrates a "remove rule" button that won't render if the rule is at the top level and is the only rule at that level.

It doesn't really take subgroups into account, i.e. it lets them be removed even if they're the only thing left in the query. As long as the initial query has at least one rule that shouldn't be an issue but it's an edge case you should keep in mind. You might want to do something similar for removeGroupAction just in case.

Working sandbox

import { useState } from 'react';
import type {
  ActionWithRulesAndAddersProps,
  Field,
  RuleGroupType,
} from 'react-querybuilder';
import {
  ActionElement,
  QueryBuilder

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@roniemartinez
Comment options

@jakeboone02
Comment options

Answer selected by roniemartinez
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants