Skip to content

Commit f97380a

Browse files
committed
createField: Uses "assertValue" as the default value for "shouldValidateOnMount"
1 parent 0ecc26c commit f97380a

File tree

4 files changed

+74
-25
lines changed

4 files changed

+74
-25
lines changed
Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,40 @@
11
import { messages } from '@examples/custom-fields/BirthDate'
22

3+
const assertError = (fieldName, errorText) => {
4+
return cy
5+
.getField(fieldName)
6+
.parent()
7+
.hasError(errorText)
8+
}
9+
310
describe('BirthDate', function() {
411
before(() => {
512
cy.loadStory(['Advanced', 'Custom fields', 'BirthDate'])
613
})
714

8-
it('Applies validation messages properly', () => {
9-
const assertError = (errorText) => {
10-
return cy
11-
.getField('birthDate')
12-
.parent()
13-
.hasError(errorText)
14-
}
15+
it('Inherits "assertValue" for mount validation', () => {
16+
cy.getField('birthDateTwo')
17+
.eq(0)
18+
.valid()
19+
20+
cy.getField('birthDateTwo')
21+
.eq(1)
22+
.valid()
1523

24+
cy.getField('birthDateTwo')
25+
.eq(2)
26+
.valid()
27+
28+
assertError('birthDateTwo', false)
29+
})
30+
31+
it('Applies validation messages properly', () => {
1632
/* Invaid day */
1733
cy.getField('birthDate')
1834
.eq(0)
1935
.typeIn('90')
2036
assertError(
37+
'birthDate',
2138
messages.name.birthDate.rule.year +
2239
messages.name.birthDate.rule.month +
2340
messages.name.birthDate.rule.day,
@@ -29,6 +46,7 @@ describe('BirthDate', function() {
2946
.clear()
3047
.typeIn('24')
3148
assertError(
49+
'birthDate',
3250
messages.name.birthDate.rule.year + messages.name.birthDate.rule.month,
3351
)
3452

@@ -37,6 +55,7 @@ describe('BirthDate', function() {
3755
.eq(1)
3856
.typeIn('13')
3957
assertError(
58+
'birthDate',
4059
messages.name.birthDate.rule.year + messages.name.birthDate.rule.month,
4160
)
4261

@@ -45,19 +64,19 @@ describe('BirthDate', function() {
4564
.eq(1)
4665
.clear()
4766
.typeIn('12')
48-
assertError(messages.name.birthDate.rule.year)
67+
assertError('birthDate', messages.name.birthDate.rule.year)
4968

5069
/* Invalid year */
5170
cy.getField('birthDate')
5271
.eq(2)
5372
.typeIn('900')
54-
assertError(messages.name.birthDate.rule.year)
73+
assertError('birthDate', messages.name.birthDate.rule.year)
5574

5675
/* Valid year */
5776
cy.getField('birthDate')
5877
.eq(2)
5978
.clear()
6079
.typeIn('2018')
61-
assertError(false)
80+
assertError('birthDate', false)
6281
})
6382
})

examples/custom-fields/BirthDate.jsx

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ import React from 'react'
22
import { Form } from 'react-advanced-form'
33
import { BirthDate } from '@examples/fields'
44

5+
const birthDateRules = {
6+
year: ({ date }) => date.year.length === 4,
7+
month: ({ date }) => date.month >= 1 && date.month <= 12,
8+
day: ({ date }) => date.day >= 1 && date.day <= 31,
9+
}
10+
511
const rules = {
612
name: {
7-
birthDate: {
8-
year: ({ date }) => date.year.length === 4,
9-
month: ({ date }) => date.month >= 1 && date.month <= 12,
10-
day: ({ date }) => date.day >= 1 && date.day <= 31,
11-
},
13+
birthDate: birthDateRules,
14+
birthDateTwo: birthDateRules,
1215
},
1316
}
1417

@@ -30,9 +33,18 @@ export const messages = {
3033
export default class BirthDateExample extends React.Component {
3134
render() {
3235
return (
33-
<Form rules={rules} messages={messages}>
34-
<BirthDate name="birthDate" label="Birth date" />
35-
</Form>
36+
<React.Fragment>
37+
<h1>BirthDate</h1>
38+
39+
<Form rules={rules} messages={messages}>
40+
<BirthDate name="birthDate" label="Birth date" />
41+
<BirthDate
42+
name="birthDateTwo"
43+
label="Birth date (initial value)"
44+
date="1999-12-10"
45+
/>
46+
</Form>
47+
</React.Fragment>
3648
)
3749
}
3850
}

examples/fields/BirthDate.jsx

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,25 @@ class BirthDate extends React.Component {
6868
}
6969

7070
export default createField({
71+
/**
72+
* Allow multiple instances of a field with the same name
73+
* because "birthDate" contains three children input fields.
74+
*/
7175
allowMultiple: true,
7276
valuePropName: 'date',
77+
/**
78+
* Modifies the field's state to include custom type.
79+
* This prevents children inputs to listen to "type: text"
80+
* validation rules.
81+
*/
7382
mapPropsToField: ({ fieldRecord }) => ({
7483
...fieldRecord,
75-
type: 'birth-date',
84+
type: 'birthDate',
7685
}),
77-
shouldValidateOnMount: ({ date }) => {
78-
return date.year || date.month || date.day
79-
},
86+
/**
87+
* Execute mapping function each time a "raw" value ("1999-12-10")
88+
* comes into the field. Has no effect over internal field value handling.
89+
*/
8090
mapValue(value) {
8191
const parsedDate = value.split('-')
8292
return {
@@ -85,9 +95,18 @@ export default createField({
8595
day: parsedDate[2] || '',
8696
}
8797
},
98+
/**
99+
* A predicate function that describes when the field has value.
100+
* Useful for the fields with complex internal value instance,
101+
* like this one, where the value is stored as an Object.
102+
*/
88103
assertValue(date) {
89104
return date.year || date.month || date.day
90105
},
106+
/**
107+
* Custom serialization function that executes upon the
108+
* field's serialization. Joins Object values into a string.
109+
*/
91110
serialize(date) {
92111
return [date.year, date.month, date.day].join('-')
93112
},

src/components/createField.jsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,17 @@ import {
1818

1919
/* Default options for `connectField()` HOC */
2020
const defaultOptions = {
21+
allowMultiple: false,
2122
valuePropName: 'value',
2223
initialValue: '',
23-
allowMultiple: false,
2424
mapPropsToField({ fieldRecord }) {
2525
return fieldRecord
2626
},
2727
beforeRegister({ fieldProps }) {
2828
return fieldProps
2929
},
3030
shouldValidateOnMount({ valuePropName, fieldRecord }) {
31-
const fieldValue = fieldRecord[valuePropName]
32-
return isset(fieldValue) && fieldValue !== ''
31+
return this.assertValue(fieldRecord[valuePropName])
3332
},
3433
shouldUpdateRecord({ prevValue, nextValue }) {
3534
return prevValue !== nextValue

0 commit comments

Comments
 (0)