-
Notifications
You must be signed in to change notification settings - Fork 2
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/disable and enabled props #640
base: master
Are you sure you want to change the base?
Changes from all commits
05c4e68
6f33331
9d8db3e
7a4ef85
cf67f6a
79d36ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,7 @@ import { | |
} from '../../actions'; | ||
|
||
import { | ||
checkVisibility, | ||
referAndGetBool, | ||
} from './utils'; | ||
import FieldInsertPanel from './editor/FieldInsertPanel'; | ||
import FieldEditor from './editor/FieldEditor'; | ||
|
@@ -63,10 +63,16 @@ function makeCreateChildFields( | |
|
||
const elements = !styles ? [] : styles.map((field, i) => { | ||
// Handle "show" prop | ||
if (!checkVisibility(state, rootModel, field.show)) { | ||
if (!referAndGetBool(state, rootModel, field.show)) { | ||
return null; | ||
} | ||
|
||
// Translate "disabled" and "enabled" prop to boolean value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it is more appropriate to name something like checkEnabledToBool? (as it seems its main purpose is for check disable/enable -> bool output) |
||
const disabled = ( | ||
referAndGetBool(state, rootModel, field.disabled, false) || | ||
!referAndGetBool(state, rootModel, field.enabled, true) | ||
); | ||
|
||
const childFieldPath = `${fieldPath}[${i}]`; | ||
const component = fieldComponents[field.class] || fieldComponents.textinput; | ||
let children = null; | ||
|
@@ -108,15 +114,15 @@ function makeCreateChildFields( | |
component, | ||
{ | ||
key: i, | ||
...field, | ||
model: field.field ? `${rootModel}.${field.field}` : null, | ||
label: field.label, | ||
fieldOptions: fieldOptions[field.field], | ||
disabled, | ||
warning: warnings[field.field], | ||
rootModel, | ||
fieldEditProps, | ||
getPreviousData: field.ditto && getPreviousData && | ||
(() => getPreviousData && getPreviousData(field.field)), | ||
...field, | ||
}, | ||
children | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,11 +19,21 @@ | |
jest.unmock('lodash.get'); | ||
jest.unmock('../utils'); | ||
|
||
import { checkVisibility } from '../utils'; | ||
import { referAndGetBool } from '../utils'; | ||
|
||
describe('checkVisibility', () => { | ||
it('returns false if `show` prop is false', () => { | ||
expect(checkVisibility({}, 'hoge', false)).toBe(false); | ||
describe('referAndGetBool', () => { | ||
it('returns false if orgProp is false', () => { | ||
expect(referAndGetBool({}, 'hoge', false)).toBe(false); | ||
}); | ||
|
||
it('returns true if orgProp is true', () => { | ||
expect(referAndGetBool({}, 'hoge', true)).toBe(true); | ||
}); | ||
|
||
it('returns defaultValue if orgProp is undefined', () => { | ||
expect(referAndGetBool({}, 'hoge', undefined)).toBe(true); | ||
expect(referAndGetBool({}, 'hoge', undefined, true)).toBe(true); | ||
expect(referAndGetBool({}, 'hoge', undefined, false)).toBe(false); | ||
}); | ||
|
||
it('returns referred value if show prop is given as string', () => { | ||
|
@@ -38,8 +48,8 @@ describe('checkVisibility', () => { | |
}, | ||
}; | ||
|
||
expect(checkVisibility(state, 'root', 'foo.bar')).toBe(true); | ||
expect(checkVisibility(state, 'root', 'hoge.fuga')).toBe(false); | ||
expect(referAndGetBool(state, 'root', 'foo.bar')).toBe(true); | ||
expect(referAndGetBool(state, 'root', 'hoge.fuga')).toBe(false); | ||
}); | ||
|
||
it('handles Array as referent', () => { | ||
|
@@ -49,8 +59,8 @@ describe('checkVisibility', () => { | |
}, | ||
}; | ||
|
||
expect(checkVisibility(state, 'root', 'foo:aaa')).toBe(true); | ||
expect(checkVisibility(state, 'root', 'foo:bbb')).toBe(false); | ||
expect(referAndGetBool(state, 'root', 'foo:aaa')).toBe(true); | ||
expect(referAndGetBool(state, 'root', 'foo:bbb')).toBe(false); | ||
}); | ||
|
||
it('handles an empty array as falthy', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. falthy -> falsy |
||
|
@@ -60,7 +70,7 @@ describe('checkVisibility', () => { | |
}, | ||
}; | ||
|
||
expect(checkVisibility(state, 'root', 'foo')).toBe(false); | ||
expect(referAndGetBool(state, 'root', 'foo')).toBe(false); | ||
}); | ||
|
||
it('handles empty rootPath', () => { | ||
|
@@ -71,8 +81,8 @@ describe('checkVisibility', () => { | |
}, | ||
}; | ||
|
||
expect(checkVisibility(state, null, 'foo')).toBe(true); | ||
expect(checkVisibility(state, 'hoge', 'foo')).toBe(false); | ||
expect(referAndGetBool(state, null, 'foo')).toBe(true); | ||
expect(referAndGetBool(state, 'hoge', 'foo')).toBe(false); | ||
}); | ||
|
||
it('handles OR condition', () => { | ||
|
@@ -83,7 +93,7 @@ describe('checkVisibility', () => { | |
}, | ||
}; | ||
|
||
expect(checkVisibility(state, 'root', 'foo|bar')).toBe(true); | ||
expect(referAndGetBool(state, 'root', 'foo|bar')).toBe(true); | ||
}); | ||
|
||
it('handles negative', () => { | ||
|
@@ -98,8 +108,8 @@ describe('checkVisibility', () => { | |
}, | ||
}; | ||
|
||
expect(checkVisibility(state, 'root', '!foo.bar')).toBe(false); | ||
expect(checkVisibility(state, 'root', '!hoge.fuga')).toBe(true); | ||
expect(referAndGetBool(state, 'root', '!foo.bar')).toBe(false); | ||
expect(referAndGetBool(state, 'root', '!hoge.fuga')).toBe(true); | ||
}); | ||
|
||
it('handles negative on Array referent', () => { | ||
|
@@ -109,7 +119,7 @@ describe('checkVisibility', () => { | |
}, | ||
}; | ||
|
||
expect(checkVisibility(state, 'root', '!foo:aaa')).toBe(false); | ||
expect(checkVisibility(state, 'root', '!foo:bbb')).toBe(true); | ||
expect(referAndGetBool(state, 'root', '!foo:aaa')).toBe(false); | ||
expect(referAndGetBool(state, 'root', '!foo:bbb')).toBe(true); | ||
}); | ||
}); |
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.
Does 'refer' in this method name refer to doctor referral? Slightly unclear what this method does...