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

Change enum to union type #3

Closed
shmuli9 opened this issue Jul 15, 2021 · 2 comments
Closed

Change enum to union type #3

shmuli9 opened this issue Jul 15, 2021 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@shmuli9
Copy link

shmuli9 commented Jul 15, 2021

Describe the bug
I am attempting to programmatically reset the component path state. I found the dispatchPathAction function on the instance of the component, and I can see there is a convenient action to reset defined in the enum pathActions.

I am having a bug when importing pathActions into my app to use the dispatch function. Due to the way enums work the string "RESET" does not match the enum RESET = "RESET" so I am forced to import the original enum from the lib directory like so:
import {pathActions} from "react-lasso-select/lib/pathReducer";

This import fails when running the app. From a little research this is an issue with enums (see here), that they cannot be imported from *.d.ts files.

This could be solved by simply changing the enum into a union type similar to this:

export type pathActions {
    "ADD" | "DELETE" | "MODIFY" | "MOVE" | "RESET" | "CHANGE"
}

This would be exportable, and would also have the benefit of allowing dispatch to work with string literals e.g. dispatchPathAction({type: "RESET"}).

To Reproduce
Steps to reproduce the behavior:

  1. Import pathActions into a component (eg import {pathActions} from "react-lasso-select/lib/pathReducer";)
  2. Attempt to use pathActions.ACTION in the dispatchPatchAction function (eg lassoRef.current.dispatchPathAction({type: pathActions.RESET}))
  3. npm/yarn start
  4. See error

Expected behavior
Type/enum should be imported successfully, and code should run

Screenshots
image

Desktop (please complete the following information):

  • OS: Windows
  • Browser Chrome 91
  • Version react-lasso-select@1.1.2
@shmuli9 shmuli9 added the bug Something isn't working label Jul 15, 2021
@akcyp
Copy link
Owner

akcyp commented Jul 15, 2021

Hello,
I do not recommend accessing the internal methods of the component in this case. The best way to reset a component is to set the "value" param to an empty array.

const [points, setPoints] = useState([]);
return (
  <div className="App">
    <ReactLassoSelect
      value={points}
      src="./demo.jpg"
      onChange={(path) => {
        setPoints(path);
      }}
    />
    <button onClick={() => setPoints([]) }>Reset</button>
  </div>
 );

However, if you want to use the dispatcher you can use little typescript trick:

import { pathActions } from "react-lasso-select/lib/pathReducer";
// ...
lassoRef.current.dispatchPathAction({
  type: "RESET" as pathActions.RESET
});

@akcyp akcyp closed this as completed Jul 19, 2021
@shmuli9
Copy link
Author

shmuli9 commented Jul 21, 2021

Thanks for your response.

I ended up using your idea to use the values prop to control the Lasso component. Works perfectly.

As far as the TypeScript trick, I didn’t know about that, appreciate you sharing it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants