Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we get selectedLocationIds to update on re-render #44

Open
insivika opened this issue Jun 9, 2020 · 4 comments
Open

Can we get selectedLocationIds to update on re-render #44

insivika opened this issue Jun 9, 2020 · 4 comments
Labels
enhancement New feature or request v3 New feature of v3

Comments

@insivika
Copy link

insivika commented Jun 9, 2020

Having the component rerender when selectedLocationIds get updated is crucial to my use case as the selected states are stored on the backend.

@insivika
Copy link
Author

insivika commented Jun 9, 2020

I pulled down the project and added the below to checkbox-svg-map.jsx. Note that I installed lodash to compare the id arrays. For some reason github is giving me a hard time pushing up my branch :/


  componentDidUpdate({ selectedLocationIds: prevSelectedLocationIds }) {
    if (!_.isEqual(prevSelectedLocationIds, this.props.selectedLocationIds)) {
      const svgNode = ReactDOM.findDOMNode(this);
      const selectedLocations = this.props.selectedLocationIds.map(
        (locationId) => svgNode.getElementById(locationId)
      );
      this.setState({ selectedLocations });
    }
  }

@VictorCazanave
Copy link
Owner

VictorCazanave commented Jun 11, 2020

This component was built to handle internally the state of the selected locations (kinda like an uncontrolled component). I think making selectedLocationIds update the internal state would break the "single source of truth" principle.
It was created to fit the needs of my project, so it's probably a mistake of the initial design 🙇‍♂️

A better solution might be to follow the same API as react-checkbox-group:

  • value to pass the selected ids to the checkbox component
  • onChange to update value in the parent component

Unfortunately I think this issue can only be fixed in the next major version (v3.0.0) because there will be breaking changes.
I don't know when I have time to implement it, so if you need a quick solution, I would recommend to create your own checkbox component around SVGMap.
If you want to contribute, feel free to open a PR on the v3.0.0 branch!

@VictorCazanave VictorCazanave added enhancement New feature or request v3 New feature of v3 labels Jun 11, 2020
@arslanakhtar61
Copy link

I am also looking into using such a feature using RadioSVGMap but I can't get it to re-render and update the locationId.
@insivika were you able to accomplish this task?

@VictorCazanave
Copy link
Owner

VictorCazanave commented Sep 6, 2020

@arslanakhtar61
Since RadioSVGMap handles internally the state of the selected location (like CheckboxSVGMap), updating selectedLocationId won't re-render the component (as written in the documentation).

The next major version (v3.0.0) may include this feature. Until then, I would recommend to implement your own radio component around SVGMap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request v3 New feature of v3
Projects
None yet
3 participants