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

Feature/new context api #198

Merged
merged 18 commits into from
Feb 15, 2020
Merged

Feature/new context api #198

merged 18 commits into from
Feb 15, 2020

Conversation

ThibautMarechal
Copy link
Contributor

@ThibautMarechal ThibautMarechal commented Jul 12, 2019

For now Formsy use the legacy context api. (https://reactjs.org/docs/legacy-context.html)
We should use the new context api. (https://reactjs.org/docs/context.html)
So we don't have warnings when running in strict-mode.

I may need some help to fix the tests issue :/
It seems that the context isn't fully supported by Enzyme ? (enzymejs/enzyme#1553)
Or I miss something.

src/index.tsx Outdated
this.props.children,
return (
<FormsyContext.Provider value={this.contextValue}>
{React.createElement(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to not convert this to jsx? Mixing jsx and React.createElement seems messy

src/index.tsx Outdated
this.setState({
canChange: true,
});
onValidationComplete();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, and i also fixed a bug, where if you remove all the inputs but the last one was invalid, the form was still invalid. The callback onValidationComplete was never called.

@ThibautMarechal ThibautMarechal changed the title [WIP] Feature/new context api Feature/new context api Oct 6, 2019
@rkuykendall
Copy link
Member

Is this something we should have prepped but hold back on due to incompatibility? Enzyme is pretty serious, and I'd hate to make 2.x so bleeding edge nobody can use it. We're going to have a hard enough time with the release. :/

@ThibautMarechal
Copy link
Contributor Author

I totally forgot about this PR, I will try to fix it this week.

@rkuykendall
Copy link
Member

Great! I'm going to release 2.0.0, but I expect a 2.0.1 fix-release will follow pretty quickly, and it would be nice to add this.

@@ -497,6 +502,7 @@ class Formsy extends React.Component<FormsyProps, FormsyState> {
this.setState({
canChange: true,
});
onValidationComplete();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this related to context, or is it an unrelated bug-fix, or an accident?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#198 (comment)
Yes, when the last input is removed the form was still invalid (if the removed input was invalid)

src/index.ts Outdated
attachToForm: this.attachToForm,
detachFromForm: this.detachFromForm,
isFormDisabled: props.disabled,
isValidValue: (component, value) => this.runValidation(component, value).isValid,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this method deleted and put into the context like this?

@@ -150,27 +152,23 @@ export default function<T, V>(
this.setValidations(validations, required);

// Pass a function instead?
formsy.attachToForm(this);
attachToForm(this);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it considered better to have your context not be wrapped in a single namespace like formsy as it was before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way we subscribe the the context is with the static property ContextType. Subscribe to multiple contexts with a static property are deprecated.

src/FormsyContext.ts Outdated Show resolved Hide resolved
const noFormsyErrorMessage = 'Could not find Formsy Context Provider. Did you use withFormsy outside Formsy ?';

const throwNoFormsyProvider = () => {
throw new Error(noFormsyErrorMessage);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this stop formsy components from being able to be used outside of Formsy without throwing errors?

This may be too harsh a response. That said, I do like that it's very clear to users who are making a mistake and I normally really appreciate helpful errors like this.

I guess you could always export your input components twice, once withFormsy and once without.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it will crash the application if not catched. The same behavior as react-redux if you call connect outside a Provider.
If you think it should only print a error in the console let me know.

src/index.ts Outdated Show resolved Hide resolved
"react": "^15.6.1 || ^16.0.0",
"react-dom": "^15.6.1 || ^16.0.0"
"react": "^16.0.0",
"react-dom": "^16.0.0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been 2 years, so this seems like an acceptable change.

src/Wrapper.ts Outdated
@@ -232,7 +228,7 @@ export default function<T, V>(
isPristine: false,
},
() => {
formsy.validate(this);
this.context.validate(this); //eslint-disable-line
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add the rule here? It would be nice to know what eslint is mad about.

src/index.ts Outdated
@@ -55,6 +57,7 @@ export interface FormsyState {
isPristine?: boolean;
isSubmitting: boolean;
isValid: boolean;
contextValue: FormsyContextInterface;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be alphabetically sorted.

@rkuykendall
Copy link
Member

@ThibautMarechal This is looking great! Left comments. They are mostly questions and very light suggestions, so feel free to respond to any comments if you prefer the way you did it. A lot of it was just questions I had about context and best practices since I'm not familiar with that part of React.

One additional change is that you can drop all the dist changes. Jest runs directly on the source code and all dist changes are computed and committed automatically when I run the deploy script. It makes PRs so much easier to read and reduces conflicts in people's PRs.

@rkuykendall
Copy link
Member

Very strange that the build files are showing as changed even though they look exactly the same. Maybe you're on windows and it changed the line endings? I'm okay to just merge it if we can't figure out why those are still showing as changed.

Co-Authored-By: Robert Kuykendall <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants