react-querybuilder / react-querybuilder

The Query Builder component for React
https://react-querybuilder.js.org/
MIT License
1.23k stars 263 forks source link

radio as boolean (0, 1) and parseNumbers #622

Open oleg-andreyev opened 10 months ago

oleg-andreyev commented 10 months ago

Right now experiencing issue with radio element, which holds boolean (0, 1).

image image image image

As soon I save and render components once again I see image and non-values are selected image


UPD

Noticed that values name is string, that's why it cannot match.

image image

but as soon as change type to int, types are swapped in value image


initial load image as soon I click on radio image

oleg-andreyev commented 10 months ago

atm applied following workaround:

    if (props.type === 'radio') {
        return (
            <ValueEditor
                {...props}
                handleOnChange={(v) => {
                    let valuesType = typeof props.values[0].name;
                    let valueType = typeof v;

                    if (valueType !== valuesType && valuesType === 'number') {
                        props.handleOnChange(parseInt(v))
                    }
                }}
            />
        );
    }
jakeboone02 commented 10 months ago

Thanks for the report. I think your issue might be more with the behavior of native HTML <input> elements, which always report their value as a string, than RQB. What would you propose as a workaround/fix?

oleg-andreyev commented 10 months ago

@jakeboone02 I've added a code-snippet here https://github.com/react-querybuilder/react-querybuilder/issues/622#issuecomment-1872126396, I see that we'll see to compare values and value, if values has Int, then value should be casted to Int, and vice-versa.

jakeboone02 commented 10 months ago

Yeah, I saw that but didn't consider it might be the actual solution. Want to submit a PR with that change while I think it over (code is here)? An additional test (radio tests here) will probably be necessary to maintain 100% coverage.