@comicrelief/storybook 1.33.3 → 1.34.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@comicrelief/storybook",
|
|
3
3
|
"description": "React components to build the Comic Relief front-end experience",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.34.2",
|
|
5
5
|
"dependencies": {
|
|
6
|
-
"@comicrelief/pattern-lab": "
|
|
6
|
+
"@comicrelief/pattern-lab": "7.67.1",
|
|
7
7
|
"@snyk/protect": "^1.1060.0",
|
|
8
8
|
"@storybook/addon-info": "3",
|
|
9
9
|
"@storybook/addon-knobs": "3",
|
|
@@ -5,6 +5,7 @@ import browser from 'browser-detect';
|
|
|
5
5
|
import SelectField from '../SelectField/SelectField';
|
|
6
6
|
import InputField from '../InputField/InputField';
|
|
7
7
|
import countries from './countries.json';
|
|
8
|
+
import { defaultPostcodeValidation, fallbackPostcodeValidation } from './postcodeValidations';
|
|
8
9
|
|
|
9
10
|
class PostcodeLookup extends Component {
|
|
10
11
|
/**
|
|
@@ -13,7 +14,11 @@ class PostcodeLookup extends Component {
|
|
|
13
14
|
constructor(props) {
|
|
14
15
|
super(props);
|
|
15
16
|
this.timeoutDuration = 10000;
|
|
17
|
+
this.defaultCountry = 'GB';
|
|
16
18
|
this.state = {
|
|
19
|
+
// Initially set using the postcodeValidation prop (be it default or overridden)
|
|
20
|
+
currentPostcodeValidation: props.postcodeValidation[this.defaultCountry],
|
|
21
|
+
postcodeTest: false,
|
|
17
22
|
addressDropdownList: [],
|
|
18
23
|
countryDropdownList: [],
|
|
19
24
|
postcodeValidationMessage: false,
|
|
@@ -119,25 +124,38 @@ class PostcodeLookup extends Component {
|
|
|
119
124
|
|
|
120
125
|
/**
|
|
121
126
|
* Update state with value and validity from child
|
|
122
|
-
* @param
|
|
123
|
-
* @param
|
|
127
|
+
* @param fieldName
|
|
128
|
+
* @param fieldValidityObj
|
|
124
129
|
*/
|
|
125
|
-
setValidity(
|
|
126
|
-
if ((this.state.validation[
|
|
127
|
-
(this.state.validation[
|
|
130
|
+
setValidity(fieldName, fieldValidityObj) {
|
|
131
|
+
if ((this.state.validation[fieldName].value === undefined || this.state.validation[fieldName].value !== fieldValidityObj.value) ||
|
|
132
|
+
(this.state.validation[fieldName].message !== fieldValidityObj.message)) {
|
|
133
|
+
// Only revalidate the postcode when the country has changed value
|
|
134
|
+
const revalidatePostcode = fieldName === 'country' && fieldValidityObj !== this.state.validation.country.value;
|
|
135
|
+
|
|
128
136
|
this.setState({
|
|
129
137
|
...this.state,
|
|
130
138
|
fieldRefs: this.fieldRefs,
|
|
139
|
+
// Only update the regex state when necessary
|
|
140
|
+
...(revalidatePostcode && {
|
|
141
|
+
// If there's no specific regex pattern for this country code, use the fallback pattern
|
|
142
|
+
currentPostcodeValidation: this.props.postcodeValidation[fieldValidityObj.value] || fallbackPostcodeValidation,
|
|
143
|
+
}),
|
|
131
144
|
validation: {
|
|
132
145
|
...this.state.validation,
|
|
133
|
-
[
|
|
134
|
-
valid:
|
|
135
|
-
value:
|
|
136
|
-
message:
|
|
137
|
-
showErrorMessage:
|
|
146
|
+
[fieldName]: {
|
|
147
|
+
valid: fieldValidityObj.valid,
|
|
148
|
+
value: fieldValidityObj.value,
|
|
149
|
+
message: fieldValidityObj.message,
|
|
150
|
+
showErrorMessage: fieldValidityObj.showErrorMessage,
|
|
138
151
|
},
|
|
139
152
|
},
|
|
140
153
|
});
|
|
154
|
+
|
|
155
|
+
// Force a revalidation of the postcode if the country (and therefore the regex) has changed
|
|
156
|
+
if (revalidatePostcode) {
|
|
157
|
+
this.revalidatePostcode();
|
|
158
|
+
}
|
|
141
159
|
}
|
|
142
160
|
}
|
|
143
161
|
|
|
@@ -153,6 +171,28 @@ class PostcodeLookup extends Component {
|
|
|
153
171
|
});
|
|
154
172
|
}
|
|
155
173
|
|
|
174
|
+
/**
|
|
175
|
+
* Crummy workaround to trigger a revalidation
|
|
176
|
+
*/
|
|
177
|
+
revalidatePostcode() {
|
|
178
|
+
// Store the current postcode to re-add
|
|
179
|
+
const postcodeField = document.getElementById('field-input--postcode');
|
|
180
|
+
const currentPostcodeValue = postcodeField.value;
|
|
181
|
+
const blurEvent = new Event('blur', { bubbles: true });
|
|
182
|
+
|
|
183
|
+
// Temporarily reset the postcode field and programmatically
|
|
184
|
+
// trigger a blur event to make the validation take notice
|
|
185
|
+
postcodeField.value = '';
|
|
186
|
+
postcodeField.dispatchEvent(blurEvent);
|
|
187
|
+
|
|
188
|
+
setTimeout(() => {
|
|
189
|
+
// Immediately re-add the value and trigger another blur event
|
|
190
|
+
postcodeField.value = currentPostcodeValue;
|
|
191
|
+
postcodeField.dispatchEvent(blurEvent);
|
|
192
|
+
}, 1);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
|
|
156
196
|
/**
|
|
157
197
|
* Get addresses from lookup API and update state
|
|
158
198
|
* @return {Promise}
|
|
@@ -225,7 +265,7 @@ class PostcodeLookup extends Component {
|
|
|
225
265
|
* Updates state with new country object.
|
|
226
266
|
*/
|
|
227
267
|
createCountryDropdownList() {
|
|
228
|
-
let value =
|
|
268
|
+
let value = this.defaultCountry;
|
|
229
269
|
let dropDownList = [];
|
|
230
270
|
if (this.props.valuesFromParent !== null) {
|
|
231
271
|
const isGBSelected = this.props.valuesFromParent.country.value === '' || this.props.valuesFromParent.country.value === 'GB';
|
|
@@ -372,8 +412,9 @@ class PostcodeLookup extends Component {
|
|
|
372
412
|
type: 'text',
|
|
373
413
|
placeholder: this.props.placeholder,
|
|
374
414
|
buttonText: this.props.buttonText,
|
|
375
|
-
pattern: this.
|
|
376
|
-
|
|
415
|
+
pattern: this.state.currentPostcodeValidation.pattern,
|
|
416
|
+
// As the error msg will depend on exactly what the regex is asking for, it comes via the associated validation object
|
|
417
|
+
invalidErrorText: this.state.currentPostcodeValidation.errorMsg,
|
|
377
418
|
emptyFieldErrorText: 'Please enter your postcode',
|
|
378
419
|
extraClass: 'search-box',
|
|
379
420
|
autoComplete: isBrowser.name === 'chrome' ? 'new-postcode' : 'off',
|
|
@@ -500,8 +541,7 @@ PostcodeLookup.defaultProps = {
|
|
|
500
541
|
plusURL: 'https://lookups.sls.comicrelief.com/postcode/lookup?query=',
|
|
501
542
|
buttonText: 'FIND UK ADDRESS',
|
|
502
543
|
placeholder: 'SE1 7TP',
|
|
503
|
-
|
|
504
|
-
invalidErrorText: 'Please enter a valid postcode',
|
|
544
|
+
postcodeValidation: defaultPostcodeValidation,
|
|
505
545
|
};
|
|
506
546
|
|
|
507
547
|
PostcodeLookup.propTypes = {
|
|
@@ -513,8 +553,9 @@ PostcodeLookup.propTypes = {
|
|
|
513
553
|
plusURL: propTypes.string,
|
|
514
554
|
buttonText: propTypes.string,
|
|
515
555
|
placeholder: propTypes.string,
|
|
516
|
-
|
|
517
|
-
|
|
556
|
+
// An optional prop to allow contexts to override them;
|
|
557
|
+
// see postcodeValidations.js for reference
|
|
558
|
+
postcodeValidation: propTypes.object,
|
|
518
559
|
};
|
|
519
560
|
|
|
520
561
|
export default PostcodeLookup;
|
|
@@ -4,6 +4,7 @@ import { storiesOf } from '@storybook/react';
|
|
|
4
4
|
import { withKnobs, text, boolean } from '@storybook/addon-knobs';
|
|
5
5
|
import { withInfo } from '@storybook/addon-info';
|
|
6
6
|
import PostcodeLookup from './PostcodeLookup';
|
|
7
|
+
import { postcodeValidationOverrideTest } from './postcodeValidations';
|
|
7
8
|
|
|
8
9
|
|
|
9
10
|
storiesOf('Postcode Lookup', module)
|
|
@@ -15,4 +16,12 @@ storiesOf('Postcode Lookup', module)
|
|
|
15
16
|
|
|
16
17
|
return (<PostcodeLookup label={label} forceManualInput={disableManualInput} isAddressValid={(validation) => console.log(validation) } />);
|
|
17
18
|
}),
|
|
18
|
-
)
|
|
19
|
+
)
|
|
20
|
+
.add('PostcodeLookup with postcode pattern override',
|
|
21
|
+
withInfo('Required')(() => {
|
|
22
|
+
const label = text('label', 'Postal address');
|
|
23
|
+
const disableManualInput = boolean('forceManualInput', false);
|
|
24
|
+
|
|
25
|
+
return (<PostcodeLookup postcodeValidation={postcodeValidationOverrideTest} label={label} forceManualInput={disableManualInput} isAddressValid={(validation) => console.log(validation) } />);
|
|
26
|
+
}),
|
|
27
|
+
);
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Passed to the PCLU component as default prop value
|
|
2
|
+
const defaultPostcodeValidation = {
|
|
3
|
+
GB: {
|
|
4
|
+
pattern: '(GIR 0AA)|((([A-Z][0-9][0-9]?)|(([A-Z][A-HJ-Y][0-9][0-9]?)|(([A-Z][0-9][A-Z])|([A-Z][A-HJ-Y][0-9]?[A-Z])))) [0-9][A-Z]{2})',
|
|
5
|
+
errorMsg: 'Please enter a valid UK postcode, using a space and capital letters',
|
|
6
|
+
},
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
// Generic pattern and error message used if user selects a country
|
|
10
|
+
// that isn't selected above, or in any override
|
|
11
|
+
const fallbackPostcodeValidation = {
|
|
12
|
+
pattern: '[^(?!\s*$).+]',
|
|
13
|
+
errorMsg: 'Please enter a valid postcode',
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// Goofy values just to show it working in a Story example
|
|
17
|
+
const postcodeValidationOverrideTest = {
|
|
18
|
+
GB: {
|
|
19
|
+
pattern: 'gb-regex-override',
|
|
20
|
+
errorMsg: 'GB specific error OVERRIDE',
|
|
21
|
+
},
|
|
22
|
+
AF: {
|
|
23
|
+
pattern: 'af-regex-override',
|
|
24
|
+
errorMsg: 'AF specific error OVERRIDE',
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export { defaultPostcodeValidation, postcodeValidationOverrideTest, fallbackPostcodeValidation };
|