Skip to content

Commit

Permalink
Add support for stringSearch to Table Widget
Browse files Browse the repository at this point in the history
* Add support for text search to Table Widget

* PR Feedback
  • Loading branch information
jmgaya authored Sep 13, 2024
1 parent 091b6a3 commit d0dcb9e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Fix HistogramWidget with one non-zero bucket [#901](https://github.com/CartoDB/carto-react/pull/901)
- Spatial Index Sources use remote widgets calculation [#898](https://github.com/CartoDB/carto-react/pull/898)
- Support for `hiddenColumnFields` parameter and `onRowClick` handler for Table Widget [#900](https://github.com/CartoDB/carto-react/pull/900)
- Support for `searchText` and `searchColumn` for Table Widget [#900](https://github.com/CartoDB/carto-react/pull/902)

## 3.0.0

Expand Down
2 changes: 1 addition & 1 deletion packages/react-api/src/api/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function executeModel(props) {
source: data,
params: JSON.stringify(params),
queryParameters,
filters: JSON.stringify(filters),
filters: JSON.stringify({ ...filters, ...props.searchFilter }),
filtersLogicalOperator
};

Expand Down
3 changes: 2 additions & 1 deletion packages/react-widgets/src/models/TableModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ function formatResult(res) {

// From remote
function fromRemote(props) {
const { source, spatialFilter, abortController, ...params } = props;
const { source, spatialFilter, searchFilter, abortController, ...params } = props;
const { columns, sortBy, sortDirection, page, rowsPerPage } = params;

return _executeModel({
model: 'table',
source,
spatialFilter,
searchFilter,
params: {
column: columns,
sortBy,
Expand Down
10 changes: 10 additions & 0 deletions packages/react-widgets/src/widgets/TableWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import { _FeatureFlags, _hasFeatureFlag } from '@carto/react-core';
* @param {boolean=} [props.stableHeight] - If specified, error and no-data state will maintain same height as normal widget in loading/loaded state.
* @param {boolean} [props.dense] - Whether the table should use a compact layout with smaller cell paddings.
* @param {number} [props.pageSize] - Number of rows per page. This is used to manage internal state externally.
* @param {string} [props.searchText] - Text to search in the table
* @param {string} [props.searchColumn] - Column used to perform the search, using the searchText property
*/
function TableWidget({
Expand All @@ -46,6 +48,8 @@ function TableWidget({
height,
stableHeight,
dense,
searchText,
searchColumn,
// Internal state
pageSize
}) {
Expand All @@ -55,6 +59,7 @@ function TableWidget({
const [sortBy, setSortBy] = useState(undefined);
const [sortByColumnType, setSortByColumnType] = useState(undefined);
const [sortDirection, setSortDirection] = useState('asc');
const containsStringSearchFilter = searchColumn && searchText;

const {
data = { rows: [], totalCount: 0, hasData: false },
Expand All @@ -65,6 +70,9 @@ function TableWidget({
dataSource,
params: {
columns: [...columns.map((c) => c.field), ...hiddenColumnFields],
searchFilter: containsStringSearchFilter && {
[searchColumn]: { stringSearch: { values: [searchText] } }
},
sortBy,
sortDirection,
sortByColumnType,
Expand Down Expand Up @@ -153,6 +161,8 @@ TableWidget.propTypes = {
height: PropTypes.string,
stableHeight: PropTypes.bool,
dense: PropTypes.bool,
searchText: PropTypes.string,
searchColumn: PropTypes.string,
// Internal state
pageSize: PropTypes.number
};
Expand Down

0 comments on commit d0dcb9e

Please sign in to comment.