@hmcts/one-per-page
Advanced tools
Comparing version 2.4.0 to 2.5.0
@@ -0,1 +1,11 @@ | ||
<a name="2.5.0"></a> | ||
# 2.5.0 (2018-02-21) | ||
* Attach .ref functions to basic field types ([d195670](https://github.com/hmcts/one-per-page/commit/d195670)) | ||
* Document the new ref functions ([9888f2f](https://github.com/hmcts/one-per-page/commit/9888f2f)) | ||
* Fix linting errors ([4fcd0e0](https://github.com/hmcts/one-per-page/commit/4fcd0e0)) | ||
* Switch example app to use new ref approach ([90996fb](https://github.com/hmcts/one-per-page/commit/90996fb)) | ||
<a name="2.4.0"></a> | ||
@@ -2,0 +12,0 @@ # 2.4.0 (2018-02-20) |
@@ -21,2 +21,14 @@ # Field types | ||
### `text.ref(step, fieldName)` | ||
Loads a text field from another step. | ||
> See [`ref(step, field, fieldName)`] for more information. | ||
```js | ||
form({ | ||
appType: text.ref(this.journey.steps.ApplicationType, 'applicationType') | ||
}); | ||
``` | ||
## `nonEmptyText` | ||
@@ -31,6 +43,22 @@ | ||
form({ | ||
isCitizen: ref(this.journey.steps.CitizenCheck, 'isCitizen') | ||
firstName: nonEmptyText, | ||
lastName: nonEmptyText | ||
}); | ||
``` | ||
### `nonEmptyText.ref(step, fieldName)` | ||
Loads a nonEmptyText field from another step. | ||
> See [`ref(step, field, fieldName)`] for more information. | ||
```js | ||
form({ | ||
appType: nonEmptyText.ref(this.journey.steps.ApplicationType, 'applicationType') | ||
}); | ||
``` | ||
This will load `applicationType` from the `ApplicationType` step and will be | ||
available to you on `this.fields.appType`. | ||
## `bool` | ||
@@ -67,4 +95,16 @@ | ||
## `ref(step, fieldName)` | ||
### `bool.ref(step, fieldName)` | ||
Loads a bool field from another step. | ||
> See [`ref(step, field, fieldName)`] for more information. | ||
```js | ||
form({ | ||
contact: bool.ref(this.journey.steps.ContactPreference, 'contactMe') | ||
}); | ||
``` | ||
## `ref(step, field, fieldName)` | ||
> `const { ref } = require('@hmcts/one-per-page/forms');` | ||
@@ -100,2 +140,14 @@ | ||
### `list.ref(step, fieldName, fieldType)` | ||
Loads a list field from another step. | ||
> See [`ref(step, field, fieldName)`] for more information. | ||
```js | ||
form({ | ||
names: list.ref(this.journey.steps.NameStep, 'names', text) | ||
}); | ||
``` | ||
## `object({ [name]: [field type] })` | ||
@@ -143,2 +195,17 @@ | ||
### `object.ref(step, fieldName, { [field name]: [field type] })` | ||
Loads a object field from another step. | ||
> See [`ref(step, field, fieldName)`] for more information. | ||
```js | ||
form({ | ||
petitioner: object.ref(this.journey.steps.PetitionerDetails, 'peitioner', { | ||
firstName: text, | ||
lastName: text | ||
}) | ||
}); | ||
``` | ||
## `convert(transformation, field)` | ||
@@ -210,1 +277,2 @@ | ||
[date-pattern]: https://govuk-elements.herokuapp.com/form-elements/example-date/ | ||
[`ref(step, field, fieldName)`]: #ref-step-field-fieldname |
const { Question, goTo } = require('@hmcts/one-per-page'); | ||
const { form, text, ref } = require('@hmcts/one-per-page/forms'); | ||
const { form, text } = require('@hmcts/one-per-page/forms'); | ||
const { answer } = require('@hmcts/one-per-page/checkYourAnswers'); | ||
@@ -17,3 +17,6 @@ const Joi = require('joi'); | ||
), | ||
husbandOrWife: ref(this.journey.steps.RespondentTitle, text), | ||
husbandOrWife: text.ref( | ||
this.journey.steps.RespondentTitle, | ||
'husbandOrWife' | ||
), | ||
respondentFirstName: text.joi( | ||
@@ -20,0 +23,0 @@ this.content.fields.respondentFirstName.required, |
@@ -5,3 +5,3 @@ { | ||
"homepage": "https://github.com/hmcts/one-per-page#readme", | ||
"version": "2.4.0", | ||
"version": "2.5.0", | ||
"main": "./src/main.js", | ||
@@ -8,0 +8,0 @@ "repository": { |
@@ -13,2 +13,22 @@ const { fieldDescriptor } = require('./fieldDescriptor'); | ||
const ref = (step, fieldType, fieldName) => { | ||
const returnNothing = () => { | ||
return {}; | ||
}; | ||
const fetchFromStepInSession = (name, _, req) => { | ||
const values = option | ||
.fromNullable(req.session) | ||
.flatMap(session => option.fromNullable(session[step.name])) | ||
.valueOrElse({}); | ||
return fieldType | ||
.deserialize(fieldName || name, values, req) | ||
.clone({ serializer: returnNothing }); | ||
}; | ||
return fieldDescriptor({ | ||
parser: fetchFromStepInSession, | ||
deserializer: fetchFromStepInSession, | ||
serializer: returnNothing | ||
}); | ||
}; | ||
const object = childFields => fieldDescriptor({ | ||
@@ -48,2 +68,3 @@ parser(name, body) { | ||
}); | ||
object.ref = (step, fieldName, fields) => ref(step, object(fields), fieldName); | ||
@@ -94,2 +115,3 @@ const list = field => fieldDescriptor({ | ||
}); | ||
list.ref = (step, fieldName, field) => ref(step, list(field), fieldName); | ||
@@ -110,2 +132,3 @@ const nonEmptyText = fieldDescriptor({ | ||
}); | ||
nonEmptyText.ref = (step, fieldName) => ref(step, nonEmptyText, fieldName); | ||
@@ -126,2 +149,3 @@ const text = fieldDescriptor({ | ||
}); | ||
text.ref = (step, fieldName) => ref(step, text, fieldName); | ||
@@ -154,23 +178,4 @@ const truthy = ['yes', 'y', 'true', 't', '1']; | ||
}); | ||
bool.ref = (step, fieldName) => ref(step, bool, fieldName); | ||
const ref = (step, field) => { | ||
const returnNothing = () => { | ||
return {}; | ||
}; | ||
const fetchFromStepInSession = (name, _, req) => { | ||
const values = option | ||
.fromNullable(req.session) | ||
.flatMap(session => option.fromNullable(session[step.name])) | ||
.valueOrElse({}); | ||
return field | ||
.deserialize(name, values, req) | ||
.clone({ serializer: returnNothing }); | ||
}; | ||
return fieldDescriptor({ | ||
parser: fetchFromStepInSession, | ||
deserializer: fetchFromStepInSession, | ||
serializer: returnNothing | ||
}); | ||
}; | ||
const convert = (transformation, field) => fieldDescriptor({ | ||
@@ -177,0 +182,0 @@ parser(name, body, req) { |
@@ -277,11 +277,75 @@ const { expect } = require('../util/chai'); | ||
const myStep = { name: 'MyStep' }; | ||
describe('ref([step], text)', fieldTest(ref(myStep, text), it => { | ||
const req = { session: { MyStep: { foo: 'From another step' } } }; | ||
{ | ||
const myStep = { name: 'MyStep' }; | ||
const session = { | ||
MyStep: { | ||
listKey: ['Foo', 'Bar', 'Baz'], | ||
objectKey: { | ||
a: 'A text field', | ||
b: true | ||
}, | ||
nonEmptyKey: 'From another step', | ||
textKey: 'From another step', | ||
boolKey: true | ||
} | ||
}; | ||
it.parses({ to: 'From another step', req }); | ||
it.deserializes({ value: 'From another step', req }); | ||
it.serializes({ to: {}, from: {}, req }); | ||
})); | ||
describe('ref([step], text)', fieldTest(ref(myStep, text), it => { | ||
const req = { session: { MyStep: { foo: 'From another step' } } }; | ||
it.parses({ to: 'From another step', req }); | ||
it.deserializes({ value: 'From another step', req }); | ||
it.serializes({ to: {}, from: {}, req }); | ||
})); | ||
const objectRef = object.ref(myStep, 'objectKey', { a: text, b: bool }); | ||
describe('object.ref([step], [key], [fields])', fieldTest(objectRef, it => { | ||
const req = { session }; | ||
it.parses({ to: { a: 'A text field', b: true }, req }); | ||
it.deserializes({ value: { a: 'A text field', b: true }, req }); | ||
it.serializes({ to: {}, from: {}, req }); | ||
})); | ||
const listRef = list.ref(myStep, 'listKey', text); | ||
describe('list.ref([step], [key], text)', fieldTest(listRef, it => { | ||
const req = { session }; | ||
it.parses({ to: ['Foo', 'Bar', 'Baz'], req }); | ||
it.deserializes({ value: ['Foo', 'Bar', 'Baz'], req }); | ||
it.serializes({ to: {}, from: {}, req }); | ||
})); | ||
const nonEmptyRef = nonEmptyText.ref(myStep, 'nonEmptyKey'); | ||
describe('nonEmptyText.ref([step], [key])', fieldTest(nonEmptyRef, it => { | ||
const req = { session }; | ||
it.parses({ to: 'From another step', req }); | ||
it.parses({ to: '', req: {} }); | ||
it.deserializes({ value: 'From another step', req }); | ||
it.deserializes({ value: '', req: {} }); | ||
it.serializes({ to: {}, from: {}, req }); | ||
})); | ||
const textRef = text.ref(myStep, 'textKey'); | ||
describe('text.ref([step], [key])', fieldTest(textRef, it => { | ||
const req = { session }; | ||
it.parses({ to: 'From another step', req }); | ||
it.parses({ to: undefined, req: {} }); | ||
it.deserializes({ value: 'From another step', req }); | ||
it.deserializes({ value: undefined, req: {} }); | ||
it.serializes({ to: {}, from: {}, req }); | ||
})); | ||
const boolRef = bool.ref(myStep, 'boolKey'); | ||
describe('bool.ref([step], [key])', fieldTest(boolRef, it => { | ||
const req = { session }; | ||
it.parses({ to: true, req }); | ||
it.deserializes({ value: true, req }); | ||
it.serializes({ to: {}, from: {}, req }); | ||
})); | ||
} | ||
const toUpper = convert(str => str.toUpperCase(), text); | ||
@@ -288,0 +352,0 @@ describe('convert(() => {}, text)', fieldTest(toUpper, it => { |
1297273
8759