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 operator with settings/options doesn’t work #353

Open
tkapitonova opened this issue Jan 11, 2021 · 0 comments
Open

Custom operator with settings/options doesn’t work #353

tkapitonova opened this issue Jan 11, 2021 · 0 comments

Comments

@tkapitonova
Copy link

Hi!

Is your feature request related to a problem? Please describe.
I've created my own custom operator - bitwise operator AND (implementation based on proximity operator)

Definition in config.operators

bitwise_and: {
    label: "&",
    cardinality: 1,
    labelForFormat: '&',
    formatOp: (field, op, values, valueSrc, valueType, opDef, operatorOptions, isForDisplay) => {
        const operator = operatorOptions.get("operator");
        const value = operatorOptions.get("value");
        return `${field}&${values}${operator}${value}`;
    },
    sqlFormatOp: (field, op, values, valueSrc, valueType, opDef, operatorOptions) => {
        const operator = operatorOptions.get("operator");
        const value = operatorOptions.get("value");
        return `${field}&${values}${operator}${value}`;
    },
    mongoFormatOp: undefined,
    jsonLogic: "&",
    options: {
        factory: (props) => <BitwiseOperator {...props} />,
        default: {
            operator: " equal",
            value: ""
        }
    }
}

Settings for custom operator

import React from "react";

export default function BitwiseOperator(props) {
    const {config, options, default: defaultProps} = props;

    const {widgets} = config;
    const Select = widgets.select.factory;
    const Number = widgets.number.factory;

    const defaultOperator = defaultProps ? defaultProps.operator : "equal";
    const defaultValue = defaultProps ? defaultProps.value : undefined;

    const selectedOperator = options ? options.get("operator", defaultOperator): defaultOperator;
    const selectedValue = options ? options.get("value", defaultValue) : defaultValue;


    const operatorValues = [
        {
            title: "=",
            value: "equal"
        },
        {
            title: "!=",
            value: "not_equal"
        }
    ]

    const handleChangeOperator = (value) => {
        props.setOption("operator", value);
    }

    const handleChangeValue = (value) => {
        props.setOption("value", parseInt(value));
    }

    return (
            <div className="p-formgroup-inline">
                <Select
                    config={config}
                    value={selectedOperator}
                    listValues={operatorValues}
                    setValue={handleChangeOperator}
                    placeholder={"Enter operator 2"}
                    className="p-mr-2 p-d-inline"
                />
                &nbsp;
                <Number
                    key="bitwise_value"
                    config={config}
                    field={""}
                    value={selectedValue}
                    placeholder={"Enter value"}
                    setValue={handleChangeValue}
                    className="p-d-inline"
                />
            </div>
        );
}

if in config.types i set defaultOperator different from my custom operator,
for example: defaultOperator: "equal" then when user changes operator from default to my custom operator
props.optios returns null (props.optios=null) instead of properties (operator and value).

Describe the solution you'd like
As far as i understand such behaviour occures from flag canReuseValue in method setOperator (modules/stores/tree.js).
During Flag evaluation in method getNewValueForFieldOp (modules/utils/validation.js) additional properties of new operator aren't being taken into account.

For proximity operator described issue is not applicable because there is hard coded check for this operator:
if (currentOperator != newOperator && [currentOperator, newOperator].includes("proximity")) canReuseValue = false;

Is it possible to somehoww improve logic behind canReuseValue flag (to account custom operators properties) or am i doing something wrong?

Describe alternatives you've considered
At least for now there is a workaround: you can set config.settings.clearValueOnChangeOp=true, but it would be great if another way existed, because there are some cases when i dont want to clear filed value

Please let me know your thoughts.

Best regards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants