-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web, mobile): allow passing string as a single color to colors prop
- Loading branch information
1 parent
ae12279
commit f5ba08c
Showing
10 changed files
with
203 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,43 @@ | ||
export const colorsValidator = ( | ||
propValue, | ||
key, | ||
componentName, | ||
location, | ||
propFullName | ||
) => { | ||
const color = propValue[0] | ||
const duration = propValue[1] | ||
const isHexColor = (color) => color.match(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/) | ||
const getErrorInProp = (propName, componentName) => | ||
`Invalid prop '${propName}' supplied to '${componentName}'` | ||
const getHexColorError = (propName, componentName, type = 'array') => | ||
new Error( | ||
`${getErrorInProp(propName, componentName)}. Expect ${ | ||
type === 'array' ? 'an array of tuples where the first element is a' : '' | ||
} HEX color code string. | ||
.` | ||
) | ||
|
||
if (!color.match(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/)) { | ||
return new Error( | ||
`Invalid prop '${propFullName}[0]' supplied to '${componentName}'.Expect a color with HEX color code.` | ||
) | ||
const validateColorsAsString = (colors, propName, componentName) => { | ||
if (!isHexColor(colors)) { | ||
return getHexColorError(propName, componentName, 'string') | ||
} | ||
} | ||
|
||
const validateColorsAsArray = (colors, propName, componentName) => { | ||
for (let index = 0; index < colors.length; index += 1) { | ||
const color = colors[index][0] | ||
const duration = colors[index][1] | ||
|
||
if (!(duration === undefined || (duration >= 0 && duration <= 1))) { | ||
return new Error( | ||
`Invalid prop '${propFullName}[1]' supplied to '${componentName}'. Expect a number of color transition duration with value between 0 and 1.` | ||
) | ||
if (!isHexColor(color)) { | ||
return getHexColorError(propName, componentName) | ||
} | ||
|
||
if (!(duration === undefined || (duration >= 0 && duration <= 1))) { | ||
return new Error( | ||
`${getErrorInProp(propName, componentName)}. | ||
Expect an array of tuples where the second element is a number between 0 and 1 representing color transition duration.` | ||
) | ||
} | ||
} | ||
} | ||
|
||
export const colorsValidator = (props, propName, componentName) => { | ||
const colors = props[propName] | ||
if (typeof colors === 'string') { | ||
return validateColorsAsString(colors, propName, componentName) | ||
} | ||
|
||
return validateColorsAsArray(colors, propName, componentName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters