Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

289
Views
react-sortable-tree - How to get the search API working

According to the API doc there needs to be a searchQuery prop which i've mentioned in my code but the search doesn't seem to be working

API doc doesn't explain how to implement it and the examples available online don't seem to be working on code sandbox.

The only article available which seems to explain search has incorrect code (duplicate props): https://frugalisminds.com/how-to-create-react-sortable-tree/

API Doc: https://www.npmjs.com/package/react-sortable-tree

Below is the code:

import React, { Component } from "react";
import SortableTree from "react-sortable-tree";
import "react-sortable-tree/style.css";

export default class Tree extends Component {
  constructor(props) {
    super(props);
 
    this.state = {
      treeData: [
        { title: "Chicken", children: [{ title: "Egg" }] },
        { title: "Fish", children: [{ title: "fingerline" }] },
      ],
      searchString: ""
    };
  }

  handleSearchOnChange = e => {
    this.setState({
      searchString: e.target.value,
    });
  };
 
  render() {
    return (
      <div style={{ height: 400 }}>
        <input 
          type="search" 
          onChange={this.handleSearchOnChange} 
          className="form-control" 
        />
        <SortableTree
          searchQuery={this.state.searchString}
          treeData={this.state.treeData}
          onChange={treeData => this.setState([...treeData])}
          isVirtualized={false}
        />
      </div>
    );
  }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

missing a searchFocusOffset to highlight the found item and a searchMethod which can be custom defined inside render method as follows:

import React, { Component } from "react";
import SortableTree from "react-sortable-tree";
import "react-sortable-tree/style.css"; // This only needs to be imported once in your app

export default class Tree extends Component {
  constructor(props) {
    super(props);
 
    this.state = {
      treeData: [
        { title: "Chicken", children: [{ title: "Egg" }] },
        { title: "Fish", children: [{ title: "fingerline" }] },
      ],
      searchString: ""
    };
  }
 
  render() {
    // Case insensitive search of `node.title`
    const customSearchMethod = ({ node, searchQuery }) =>
      searchQuery &&
      node.title.toLowerCase().indexOf(searchQuery.toLowerCase()) > -1;
      
    return (
      <div style={{ height: 400 }}>
        <input 
          type="search" 
          onChange={event => this.setState({ searchString: event.target.value })}
          className="form-control" 
        />
        <SortableTree
          searchMethod={customSearchMethod}
          searchQuery={this.state.searchString}
          searchFocusOffset={0}
          treeData={this.state.treeData}
          onChange={treeData => this.setState([...treeData])}
          isVirtualized={false}
        />
      </div>
    );
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!