reactivesearch icon indicating copy to clipboard operation
reactivesearch copied to clipboard

MultiList can't use customQuery with empty defaultValue

Open gauthierm opened this issue 5 years ago • 5 comments

Affected Projects React

Library Version: x.y.z

  "@appbaseio/reactivecore": "^9.0.3",
  "@appbaseio/reactivesearch": "^3.2.4",

Describe the bug MultiList with a customQuery never calls custom query when default value is an empty array.

To Reproduce

<MultiList
  dataField="type"
  defaultValue={[]}
  customQuery={(value, props) => {
    if (value.length) {
      return { query: { terms: { [props.dataField]: value } } };
    }

    return { query: { bool: must_not: { term: { [props.dataField]: 'comment' } } };
  }}
/>

Steps to reproduce the behavior:

Expected behavior Default query excludes type = comment documents.

Additional context This is caused by the MultiList component constructor not calling setValue and thus not calling updateQuery when the default value is empty.

gauthierm avatar Apr 06 '20 20:04 gauthierm

See https://github.com/appbaseio/reactivesearch/blob/next/packages/web/src/components/list/MultiList.js#L95-L97

gauthierm avatar Apr 06 '20 20:04 gauthierm

Because the base MultiList is not exported it's difficult to work around this issue. I was able to do so by setting a hacky default value:

const defaultDocumentType = { forEach: () => {}, length: 1, isFake: true };

gauthierm avatar Apr 06 '20 21:04 gauthierm

Right. Initially the query should have been with called with comment as value.

Reproducible CSB: https://codesandbox.io/s/wizardly-pascal-zim38?file=/src/index.js

ShahAnuj2610 avatar Apr 17 '20 07:04 ShahAnuj2610

@gauthierm I think it's not a bug. customQuery only gets applied when a value changes in MultiList components.

So if you want your query with value comment applied, then you can pass it as a defaultQuery in MultiList component as I have done here: https://codesandbox.io/s/stupefied-babbage-pyvo6?file=/src/index.js. Lmk if you have any further doubts.

Ref: https://docs.appbase.io/docs/reactivesearch/v3/list/multilist/#props

cc: @bietkul

ShahAnuj2610 avatar Apr 20 '20 10:04 ShahAnuj2610

@ShahAnuj2610 The defaultQuery affects the query for values displayed in the multilist and the customQuery affects the query in the parent reactive base.

Your suggestion to use defaultQuery just makes my default inverse value disappear from the multilist and does not affect the main search query.

The customQuery and defaultQuery documentation in the appbase docs is not particularly clear and the props names themselves seem misleading but reading the implementation this seems the be the intent. See also https://github.com/appbaseio/reactivesearch/pull/954 and https://github.com/appbaseio/reactivesearch/issues/855

gauthierm avatar Apr 20 '20 23:04 gauthierm