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

feat(core): use-formts setup #11

Merged
merged 7 commits into from
Oct 2, 2020
Merged

feat(core): use-formts setup #11

merged 7 commits into from
Oct 2, 2020

Conversation

pidkopajo
Copy link
Collaborator

Created a basic setup for use-formts hook.
Currently, it only constructs a scheme-shaped tree of initial values and exports simple getter and setter.

Hook is not yet connected to react.

@pidkopajo pidkopajo force-pushed the feat/use-formts-draft branch from ef83d49 to 1a53507 Compare September 30, 2020 16:28
@pidkopajo pidkopajo changed the title feat(core): use-formts draft feat(core): use-formts setup Sep 30, 2020
Comment on lines 10 to 24
it("for one-element schema", () => {
const schema = createFormSchema(fields => ({
stringField: fields.string(),
}));

expect(createInitialState(schema)).toEqual({ stringField: "" });
});

it("for one-element schema", () => {
const schema = createFormSchema(fields => ({
stringField: fields.string(),
}));

expect(createInitialState(schema)).toEqual({ stringField: "" });
});
Copy link
Collaborator

Choose a reason for hiding this comment

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

duplicated test

},
});
});
});
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe another test case test case for sth like

const schema = createFormSchema(fields => ({
   arr: fields.array(fields.object({ foo: fields.string(), bar: fields.string() }))
}));
const init = { arr: [{ foo: "foo" }]}

const expectedResult = { arr: [{ foo: "foo", bar: "" }]}

assert<IsExact<typeof root, ("a" | "b" | "c")[]>>(true);

const first = get(Schema.theArrayChoice.nth(0));
assert<IsExact<typeof first, "a" | "b" | "c">>(true);
Copy link
Collaborator

Choose a reason for hiding this comment

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

shouldn't it be "a" | "b" | "c" | undefiend ?

Comment on lines +30 to +36
export const isArrayDesc = <Value>(
x: _DescriptorApprox_<Value>
): x is _ArrayDescriptorApprox_<Value> => "nth" in x && "root" in x;

export const isObjectDesc = <Value>(
x: _DescriptorApprox_<Value>
): x is _ObjectDescriptorApprox_<Value> => "root" in x && !("nth" in x);
Copy link
Collaborator

Choose a reason for hiding this comment

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

nice

});
});

describe("set", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

the set implementation is really nice - it preserves Structural sharing - meaning that we don't just deep copy the input, but only copy the stuff that was updated.

I think we could reflect that in tests, something like

const original = {
  left: { a: { id: 1 }, b: { id: 2 } },
  right: { a: { id: 3 }, b: { id: 4 } },
};
const updated = set(obj, "right.b.id", 10)

expect(original).not.toBe(updated)
expect(original.right).not.toBe(updated.right)
expect(original.right.b).not.toBe(updated.right.b)
expect(original.right.a).toBe(updated.right.a)
expect(original.left).toBe(updated.left)


// loose typing for helping with internal impl, as working with generic target types is impossible

export type _FormSchemaApprox_<Values extends object> = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

shouldn't that be used in create-form-schema.ts?

@pidkopajo pidkopajo merged commit 8236189 into master Oct 2, 2020
@pidkopajo pidkopajo linked an issue Oct 2, 2020 that may be closed by this pull request
@pidkopajo pidkopajo deleted the feat/use-formts-draft branch October 5, 2020 14:14
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.

[core] useFormts v1 - setup
2 participants