-
Notifications
You must be signed in to change notification settings - Fork 1
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
Nested fields support #33
Conversation
74b7926
to
36e2f0a
Compare
…utoform into feature/array-fields
4900b44
to
323339e
Compare
d2391bd
to
b40b324
Compare
b40b324
to
6e8b661
Compare
a1b2dc9
to
9b6fcb4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
general thing is to simplify with more futil tree usage (even if that means improving futil tree methods)
src/util.js
Outdated
import F from 'futil' | ||
import _ from 'lodash/fp' | ||
|
||
export let tokenizePath = path => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You already have a futil tree configured, so you can just use the futil methods (e.g. Tree.lookup, etc)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It turns out I still need this fn, because remove
takes a string like remove('path.to.["dotted.field"]')
and passing an array all the time is cumbersome
src/index.js
Outdated
reset() { | ||
node.value = F.when(_.isUndefined, '')(x.value) | ||
if (_.isEmpty(path)) form.submit.state.error = null // Lil hack |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just a reminder about our call, reset only needs to set values
and not fields
src/index.js
Outdated
x.value = node.value | ||
node.errors = undefined | ||
F.setOn(['value', ...path], toJS(node.value), baseNode) | ||
F.setOn(['fields', ...path], toJS(node.fields), baseNode) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this also goes away
src/index.js
Outdated
clean() { | ||
x.value = node.value | ||
node.errors = undefined |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this goes away also
src/index.js
Outdated
..._.omit('value', x), | ||
remove(x) { | ||
let parentPath = tokenizePath(x) | ||
let path = parentPath.splice(parentPath.length - 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
splitAt(-1)
or something 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I just discovered chunk
from lodash so this can be done like
let tokens = tokenizePath(x)
let [parentPath, path] = tokens.length === 1 ? [[], tokens] : _.chunk(tokens.length - 1, tokens)
69ffe75
to
0df6dbb
Compare
2c9002e
to
e442d15
Compare
e442d15
to
bfdd465
Compare
…utoform into feature/array-fields
220d9b4
to
a015bd0
Compare
src/index.js
Outdated
let saved = {} | ||
|
||
let state = observable({ | ||
// config.field values are more of default values than anything else |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is confusing because you'd expect more specific value
s to take precedent over a top level value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
after talking again about defaultValue
on array items, there is good reason for this behavior but we should document it )(because parent values should take precedent for arrays because the children values would be defaults and not have a value for each child)
src/index.js
Outdated
}, | ||
reset(value) { | ||
// Reset errors | ||
if (_.isEmpty(node.path)) clearFormErrors() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these seem like they should be the same code path - clearing errors on a node should traverse its children (e.g. clearing b
in {a:{b:{c}}}
should clear c
also
src/index.js
Outdated
if (_.isEmpty(node.path)) clearFormErrors() | ||
else F.unsetOn([dotPath], state.errors) | ||
// Reset value | ||
if (!_.isUndefined(value)) F.setOn(valuePath, clone(value), saved) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont think we have a real use case for this - let's remove until we do
src/index.js
Outdated
F.walk(x => x.fields)(x => { | ||
let valuePath = ['value', ...x.path] | ||
if (x.itemField) x.fields = initFields(makeItemFields(x), x.path) | ||
if (_.isUndefined(x.value)) x.value = clone(x.defaultValue) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should not have both value
and defaultValue
, since resetting resets to the stored value
src/index.js
Outdated
if (x.itemField) x.fields = initFields(makeItemFields(x), x.path) | ||
if (_.isUndefined(x.value)) x.value = clone(x.defaultValue) | ||
if (_.isUndefined(_.get(valuePath, saved))) | ||
F.setOn(valuePath, clone(x.defaultValue), saved) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see above about defaultValue
src/index.js
Outdated
else if (_.isPlainObject(toJS(x.value))) node.value = {} | ||
else node.value = '' | ||
add(x) { | ||
throwIfLeaf(node) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lets just not add add
to leaf nodes - same with remove
src/index.js
Outdated
let field = node.getField(parentPath) | ||
field && field.remove(path) | ||
} else { | ||
throwIfLeaf(node) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lets just not add remove
to leaves
8a390fe
to
7ac6f67
Compare
7ac6f67
to
127c6e1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks really good to me. Nice work!
0ffc534
to
b6d3012
Compare
Dragons be here