Skip to content

Commit 646e4b6

Browse files
committed
custom prop validation for min/max y offsets
1 parent d354f9e commit 646e4b6

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/components/Parallax.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { Component, PropTypes } from 'react';
2+
import { offsetMin, offsetMax } from '../utils/propValidation';
23

34
export default class Parallax extends Component {
45

@@ -18,8 +19,8 @@ export default class Parallax extends Component {
1819
disabled: PropTypes.bool.isRequired,
1920
offsetXMax: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
2021
offsetXMin: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
21-
offsetYMax: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
22-
offsetYMin: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
22+
offsetYMax: offsetMax,
23+
offsetYMin: offsetMin,
2324
slowerScrollRate: PropTypes.bool.isRequired,
2425
tag: PropTypes.string.isRequired,
2526
};

src/utils/propValidation.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export function offsetMin(props, propName, componentName) {
2+
componentName = componentName || 'ANONYMOUS';
3+
4+
if (!typeof value === 'string' || !typeof value === 'number') {
5+
return new Error(`[${propName}] in ${componentName} must be a string with with "%"" or "px" units or number`);
6+
}
7+
8+
if (props[propName]) {
9+
let value = props[propName];
10+
if (typeof value === 'string') {
11+
value = parseInt(value, 10);
12+
}
13+
return value <= 0 ? null : new Error(`[${propName}] in ${componentName} is greater than zero. [${propName}] must be less than or equal to zero.`);
14+
}
15+
return null;
16+
}
17+
18+
export function offsetMax(props, propName, componentName) {
19+
componentName = componentName || 'ANONYMOUS';
20+
21+
if (!typeof value === 'string' || !typeof value === 'number') {
22+
return new Error(`[${propName}] in ${componentName} must be a string with with "%"" or "px" units or number`);
23+
}
24+
25+
if (props[propName]) {
26+
let value = props[propName];
27+
if (typeof value === 'string') {
28+
value = parseInt(value, 10);
29+
}
30+
return value >= 0 ? null : new Error(`[${propName}] in ${componentName} is less than zero. [${propName}] must be greater than or equal to zero.`);
31+
}
32+
return null;
33+
}

0 commit comments

Comments
 (0)