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

Custom rule controls #6

Merged

Conversation

maniax89
Copy link
Contributor

Adds in the ability to customize the components used for the controls for the and elements in the element.

Can be utilized similar to how the getEditor command worked before:

//create a custom component that has access to the original props
class MyCustomComponent extends React.Component {
  render() {
    return(<div />);
  }
}

//make a controls object
const controls = {
  valueEditor: <MyCustomComponent />
}

//pass it into the QueryBuilder
<QueryBuilder fields={[]} controls={controls} />

Addresses #2

Copy link
Contributor

@pavanpodila pavanpodila left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can improve a bit more

</span>
);
static shouldRender(props) {
return props.field === 'isDev' && props.operator === '=';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think we should have this. How about using the render itself to decide whether to render or not? Simplifies the contract of a custom control

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -24,7 +24,7 @@ export default class QueryBuilder extends React.Component {
fields: React.PropTypes.array.isRequired,
operators: React.PropTypes.array,
combinators: React.PropTypes.array,
getEditor: React.PropTypes.func,
controls: React.PropTypes.object,
Copy link
Contributor

@pavanpodila pavanpodila Sep 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add more explicit check with React.PropTypes.Shape since we know it should have one or more of the following props: {field,operator}Selector or valueEditor

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


if (children) {
let type = children.type;
if (type.shouldRender && type.shouldRender(this.props)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could just use React.createElement and pass in the props. Let the custom component decide whether or not to render

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


if (children) {
return (
React.cloneElement(children,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same logic as above about not having a shouldRender

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not entirely sure what you mean here - I was hoping to check for a child element and render if it exists, otherwise fallback to the default. I can definitely remove the shouldRender from the ValueEditor but is there a better pattern to achieve the "fallback" effect?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can pass in the fallback element as a prop. The control can return that of it wants to use the fallback.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but what if there is no custom control? i.e.

controls = {
  valueEditor: null
}

<QueryBuilder controls={controls} />

There won't be a control to render the fallback, right? maybe a short snippet would help me understand better?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or are you saying setup the defaults within the QueryBuilder component and then allow users to override it with props? I like that better actually

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya, the fallbacks will be part of QueryBuilder. Its only when we invoke the ValueEditor where we pass in the fallback as a prop.

const props = {
      operator, field,
      fallbackControl: (<Input type="text" />)
};

const editor = React.createElement(ValueEditor, props)

The ValueEditor can decide what to do in its render

class MyEditor extends React.Component {
        render() {
            // render the custom editor or use this.props.fallbackControl as the return value
        }
}

HTH.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also if there is no customControl provided, we use the fallback baked into QueryBuilder

@maniax89 maniax89 force-pushed the custom_rule_controls branch from b585dc4 to c1a4b4b Compare September 29, 2016 05:00
@maniax89
Copy link
Contributor Author

made a lot of changes to see if it fits better now. Also, do you want me to run the distribution build as well to check in the build artifacts along with this? or does that come as a separate PR?

@maniax89
Copy link
Contributor Author

not sure if you saw the latest updates - it uses the createElement and has a fallback without having to pass any fallback element through

@pavanpodila
Copy link
Contributor

Little swamped right now. Will take a look later tonight. Might have missed it. Thanks for checking in

Thanks,
Pavan

On Sep 30, 2016, at 11:27 AM, maniax89 [email protected] wrote:

not sure if you saw the latest updates - it uses the createElement and has a fallback without having to pass any fallback element through


You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.

@maniax89
Copy link
Contributor Author

maniax89 commented Oct 4, 2016

hi just checking in to see if you had a look yet. no rush, just seeing if there is anything else I could do

@pavanpodila
Copy link
Contributor

Looks good to me overall. Will merge.

@pavanpodila pavanpodila merged commit 61471e4 into react-querybuilder:master Oct 5, 2016
@pavanpodila
Copy link
Contributor

I think we also need to improve the code coverage in our unit tests. I was thinking of using Jest. What do you think?

@pavanpodila
Copy link
Contributor

pavanpodila commented Oct 5, 2016

@maniax89 BTW, I think now would be a good time to start a changelog.md ? I definitely want to credit you for your PR. Do you mind creating one and listing the changes/features you introduced?

I'll make a new NPM release once you have it 🍸

@maniax89
Copy link
Contributor Author

maniax89 commented Oct 5, 2016

Yes I can start a CHANGELOG.md with a list of feature additions. I guess I will add the tag as well. Since you were OK with the changes I made - I was going to continue doing the same with the buttons in the same fashion so all the components are customizable.

In terms of Jest, I haven't used it before but sounds fine. I think it would be good to get a travis.yml file in here so that the tests run with each PR and then merging is dependent on passing those tests. I think it is as easy as logging into travis https://docs.travis-ci.com/user/getting-started/#Step-one%3A-Sign-in then adding this file to the repo:

.travis.yml

language: node_js
node_js:
  - "0.10"

@maniax89 maniax89 deleted the custom_rule_controls branch October 12, 2016 21:45
jakeboone02 added a commit that referenced this pull request Sep 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants