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

Add initial implementation #1

Merged
merged 9 commits into from
Jul 26, 2019
Merged

Conversation

bsonntag
Copy link
Contributor

This PR adds the initial implementation which includes:

  • the FormProvider component;
  • the useForm hook;
  • the useFormActions hook;
  • the useFormState hook;
  • the useField hook;
  • the useFieldActions hook.

.babelrc.js Outdated
plugins: [
['module-resolver', {
alias: {
'package.json': './package.json',
Copy link
Contributor

Choose a reason for hiding this comment

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

Check if you will need this.

*/

type FieldActionsContextType = {
blurField: (field: string) => void,
Copy link
Contributor

Choose a reason for hiding this comment

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

WDYT of string => void or if you really want to be descriptive, (fieldName: string) => void.

}, [field, focusField]);

return {
error: get(errors, field, null),
Copy link
Contributor

Choose a reason for hiding this comment

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

According to useFormState type, the errors, meta and values always exist, so what do you think of abstaining from using lodash and replacing those get's with optional chaining.

return {
...state,
[payload.field]: {
...get(state, [payload.field]),
Copy link
Contributor

Choose a reason for hiding this comment

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

...get(state, payload.field),, also, IMO we could try to abstain from using lodash.

result.current.onBlur();

expect(actions.blurField).toHaveBeenCalledTimes(1);
expect(actions.blurField).toHaveBeenCalledWith('foo');
Copy link
Contributor

Choose a reason for hiding this comment

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

We could also have tests for these same flows for the field meta state, checking the active and the touched values.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm testing that in the useForm tests.


function getErrorRule(error) {
switch (error.keyword) {
case 'additionalProperties':
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the need to rename this error type?
IMO it can still be an additionalProperties error type.
This way we don't change the ajv API errors.

}, [initialValues]);

const submit = useCallback((event: ?SyntheticInputEvent<any>) => {
if (event && event.preventDefault) {
Copy link
Contributor

Choose a reason for hiding this comment

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

According to SyntheticInputEvent type, if it exists it will always have a preventDefault method. Validating that preventDefault exists, doesn't validate that it's a function, invoking it will throw an error.

@bsonntag bsonntag force-pushed the feature/initial-implementation branch from 3366c70 to cf6ff75 Compare July 26, 2019 11:17
@bsonntag bsonntag requested a review from flaviocastro July 26, 2019 11:18
@@ -0,0 +1,26 @@
/**
Copy link
Contributor

Choose a reason for hiding this comment

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

Add empty line 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.

Done.

@@ -0,0 +1,26 @@
/**
Copy link
Contributor

Choose a reason for hiding this comment

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

Add empty line before.

REGISTER_FIELD: 'REGISTER_FIELD',
RESET: 'RESET',
SET_FIELD_VALUE: 'SET_FIELD_VALUE',
SUBMIT_END: 'SUBMIT_END',
Copy link
Contributor

Choose a reason for hiding this comment

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

Rename this SUBMIT_END to SUBMIT_SUCCESS.

}

return result;
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this have a cath?
Also, couldn't finally dispatch the actionTypes.SUBMIT_END?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added the rejection callback to the then to handle onSubmit errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also had to remove the cancellation logic, because the first dispatch in the effect was making the effect re-run and cancel the request.

@bsonntag bsonntag force-pushed the feature/initial-implementation branch from cf6ff75 to ef6d51c Compare July 26, 2019 11:44
@bsonntag bsonntag requested a review from flaviocastro July 26, 2019 11:50
@bsonntag bsonntag force-pushed the feature/initial-implementation branch from ef6d51c to 973dbb9 Compare July 26, 2019 15:05
@flaviocastro flaviocastro merged commit 8df8ed1 into master Jul 26, 2019
@flaviocastro flaviocastro deleted the feature/initial-implementation branch July 26, 2019 15:28
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