projectstorm / react-diagrams

a super simple, no-nonsense diagramming library written in react that just works
https://projectstorm.cloud/react-diagrams
MIT License
8.45k stars 1.16k forks source link

Support for the ability to search a node #955

Open KumarilDave opened 1 year ago

KumarilDave commented 1 year ago

Hello. Our team has been using this product for our internal tools. It's been amazing so far. I was just wondering if there is any plans to add search functionality in the roadmap ? Or if there is some way to be able to search a node and it can zoom and pan to the target node/nodes

Flowwl commented 1 year ago

@KumarilDave Just add a search bar in your component with this logic


const [search, setSearch] = useState("")

const onSearchChange(e: ChangeEvent<HTMLInputElement>) {
   setSearch(e.target.value)
}

useEffect(() => {

  if (search === "") {
    engine.zoomToFitNodes()
    return;
  }
  const models = engine.getActiveModel().getModels()
  models.forEach((model) => model.setSelected(false))

  const matchedModels = models.filter((model) => model.getDescriptionOrWhateverYouWantToSearch().includes(search))
  matchedModels.forEach((model) => model.setSelected(true))
  engine.zoomToFitNodes(matchedModels)

}, [search])

Hope it will work for your use case