Skip to content

Override Chakra UI component props #289

@kculmback-eig

Description

@kculmback-eig

Would there be any interest in a PR that would allow users to override props for the Chakra Control Elements? This would allow greater control of styling without having to write some custom CSS.

Would look something like this in usage:

import QueryBuilder from 'react-querybuilder';
import { chakraControlElements as baseChakraControlElements } from '@react-querybuilder/chakra';
import { defaultQuery, fields } from './constants';

const chakraControlElements = {
  ...baseChakraControlElements,
  removeGroupAction: (props) => <ChakraActionElement {...props} buttonProps={{ colorScheme: 'red' }} />,
  removeRuleAction: (props) => <ChakraActionElement {...props} buttonProps={{ colorScheme: 'red' }} />,
}

export function App() {
  return (
    <QueryBuilder
      fields={fields}
      defaultQuery={defaultQuery}
      controlElements={chakraControlElements}
    />
  );
}

And the component:

import { Button, ButtonProps } from '@chakra-ui/react';
import type { ActionProps } from 'react-querybuilder';

export const ChakraActionElement = ({
  className,
  handleOnClick,
  label,
  title,
  disabled,
  disabledTranslation,
  buttonProps = {}
}: ActionProps & { buttonProps?: ButtonProps }) => (
  <Button
    className={className}
    title={disabledTranslation && disabled ? disabledTranslation.title : title}
    colorScheme="gray"
    variant="solid"
    onClick={e => handleOnClick(e)}
    size="xs"
    isDisabled={disabled && !disabledTranslation}
    {...buttonProps}>
    {disabledTranslation && disabled ? disabledTranslation.label : label}
  </Button>
);

ChakraActionElement.displayName = 'ChakraActionElement';

Alternatively in the component you could do something like this:

export const QueryBuilderActionElement = ({
  className,
  handleOnClick,
  label,
  title,
  disabled,
  disabledTranslation,
  level,
  path,
  context,
  testID,
  validation,
  ...buttonProps
}: ActionProps & ButtonProps) => { /* ... */ }

The main benefit would be allowing the user to do <ChakraActionElement {...props} colorScheme="red" />, but the drawback is that you would need to destructure all the ActionProps to avoid automatically passing them to the Chakra component. Chakra will automatically pass any additional prop to the DOM element resulting in warnings like this Warning: React does not recognize the "testID" prop on a DOM element..

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions