Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

290
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda