Skip to content

Commit

Permalink
feat(prop-types): add prop-types validation to components
Browse files Browse the repository at this point in the history
Fixes #26
  • Loading branch information
ekoeryanto committed Mar 14, 2018
1 parent 8ffb26d commit 60ee9f1
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 9 deletions.
8 changes: 8 additions & 0 deletions src/cms/preview-templates/AboutPagePreview.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react'
import PropTypes from 'prop-types'
import { AboutPageTemplate } from '../../templates/about-page'

const AboutPagePreview = ({ entry, widgetFor }) => (
Expand All @@ -8,4 +9,11 @@ const AboutPagePreview = ({ entry, widgetFor }) => (
/>
)

AboutPagePreview.propTypes = {
entry: PropTypes.shape({
getIn: PropTypes.func,
}),
widgetFor: PropTypes.func,
}

export default AboutPagePreview
8 changes: 8 additions & 0 deletions src/cms/preview-templates/BlogPostPreview.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react'
import PropTypes from 'prop-types'
import { BlogPostTemplate } from '../../templates/blog-post'

const BlogPostPreview = ({ entry, widgetFor }) => (
Expand All @@ -9,4 +10,11 @@ const BlogPostPreview = ({ entry, widgetFor }) => (
/>
)

BlogPostPreview.propTypes = {
entry: PropTypes.shape({
getIn: PropTypes.func,
}),
widgetFor: PropTypes.func,
}

export default BlogPostPreview
8 changes: 8 additions & 0 deletions src/cms/preview-templates/ProductPagePreview.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react'
import PropTypes from 'prop-types'
import { ProductPageTemplate } from '../../templates/product-page'

const ProductPagePreview = ({ entry, getAsset }) => {
Expand Down Expand Up @@ -45,4 +46,11 @@ const ProductPagePreview = ({ entry, getAsset }) => {
)
}

ProductPagePreview.propTypes = {
entry: PropTypes.shape({
getIn: PropTypes.func,
}),
getAsset: PropTypes.func,
}

export default ProductPagePreview
17 changes: 14 additions & 3 deletions src/components/Content.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import React from 'react'
import PropTypes from 'prop-types'

export default ({ content, className }) => (
<div className={className}>{content}</div>
)
export const HTMLContent = ({ content, className }) => (
<div className={className} dangerouslySetInnerHTML={{ __html: content }} />
)

const Content = ({ content, className }) => (
<div className={className}>{content}</div>
)

Content.propTypes = {
content: PropTypes.string,
className: PropTypes.string,
}

HTMLContent.propTypes = Content.propTypes

export default Content
10 changes: 10 additions & 0 deletions src/components/Features.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react'
import PropTypes from 'prop-types'

const FeatureGrid = ({ gridItems }) => (
<div className="columns is-multiline">
Expand All @@ -15,4 +16,13 @@ const FeatureGrid = ({ gridItems }) => (
</div>
)

FeatureGrid.propTypes = {
gridItems: PropTypes.arrayOf(
PropTypes.shape({
image: PropTypes.string,
text: PropTypes.string,
})
),
}

export default FeatureGrid
14 changes: 13 additions & 1 deletion src/components/Pricing.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react'
import PropTypes from 'prop-types'

export default ({ data }) => (
const Pricing = ({ data }) => (
<div className="columns">
{data.map(price => (
<div key={price.plan} className="column">
Expand All @@ -24,3 +25,14 @@ export default ({ data }) => (
))}
</div>
)

Pricing.propTypes = {
data: PropTypes.shape({
plan: PropTypes.string,
price: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
description: PropTypes.string,
items: PropTypes.array,
}),
}

export default Pricing
14 changes: 13 additions & 1 deletion src/components/Testimonials.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react'
import PropTypes from 'prop-types'

export default ({ testimonials }) => (
const Testimonials = ({ testimonials }) => (
<div>
{testimonials.map(testimonial => (
<article className="message">
Expand All @@ -13,3 +14,14 @@ export default ({ testimonials }) => (
))}
</div>
)

Testimonials.propTypes = {
testimonials: PropTypes.arrayOf(
PropTypes.shape({
quote: PropTypes.string,
author: PropTypes.string,
})
),
}

export default Testimonials
9 changes: 9 additions & 0 deletions src/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react'
import PropTypes from 'prop-types'
import Link from 'gatsby-link'

export default class IndexPage extends React.Component {
Expand Down Expand Up @@ -43,6 +44,14 @@ export default class IndexPage extends React.Component {
}
}

IndexPage.propTypes = {
data: PropTypes.shape({
allMarkdownRemark: PropTypes.shape({
edges: PropTypes.array,
}),
}),
}

export const pageQuery = graphql`
query IndexQuery {
allMarkdownRemark(sort: { order: DESC, fields: [frontmatter___date] }) {
Expand Down
15 changes: 14 additions & 1 deletion src/templates/about-page.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react'
import PropTypes from 'prop-types'
import Content, { HTMLContent } from '../components/Content'

export const AboutPageTemplate = ({ title, content, contentComponent }) => {
Expand All @@ -22,7 +23,13 @@ export const AboutPageTemplate = ({ title, content, contentComponent }) => {
)
}

export default ({ data }) => {
AboutPageTemplate.propTypes = {
title: PropTypes.string.isRequired,
content: PropTypes.string,
contentComponent: PropTypes.instanceOf(React.Component),
}

const AboutPage = ({ data }) => {
const { markdownRemark: post } = data

return (
Expand All @@ -34,6 +41,12 @@ export default ({ data }) => {
)
}

AboutPage.propTypes = {
data: PropTypes.object.isRequired,
}

export default AboutPage

export const aboutPageQuery = graphql`
query AboutPage($id: String!) {
markdownRemark(id: { eq: $id }) {
Expand Down
21 changes: 19 additions & 2 deletions src/templates/blog-post.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react'
import PropTypes from 'prop-types'
import Helmet from 'react-helmet'
import Content, { HTMLContent } from '../components/Content'

Expand Down Expand Up @@ -29,8 +30,16 @@ export const BlogPostTemplate = ({
)
}

export default props => {
const { markdownRemark: post } = props.data
BlogPostTemplate.propTypes = {
content: PropTypes.string.isRequired,
contentComponent: PropTypes.instanceOf(React.Component),
description: PropTypes.string,
title: PropTypes.string,
helmet: PropTypes.instanceOf(Helmet),
}

const BlogPost = ({ data }) => {
const { markdownRemark: post } = data

return (
<BlogPostTemplate
Expand All @@ -43,6 +52,14 @@ export default props => {
)
}

BlogPost.propTypes = {
data: PropTypes.shape({
data: PropTypes.object,
}),
}

export default BlogPost

export const pageQuery = graphql`
query BlogPostByID($id: String!) {
markdownRemark(id: { eq: $id }) {
Expand Down
37 changes: 36 additions & 1 deletion src/templates/product-page.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react'
import PropTypes from 'prop-types'
import Features from '../components/Features'
import Testimonials from '../components/Testimonials'
import Pricing from '../components/Pricing'
Expand Down Expand Up @@ -104,7 +105,31 @@ export const ProductPageTemplate = ({
</section>
)

export default ({ data }) => {
ProductPageTemplate.propTypes = {
image: PropTypes.string,
title: PropTypes.string,
heading: PropTypes.string,
description: PropTypes.string,
intro: PropTypes.shape({
blurbs: PropTypes.array,
}),
main: PropTypes.shape({
heading: PropTypes.string,
description: PropTypes.string,
image1: PropTypes.object,
image2: PropTypes.object,
image3: PropTypes.object,
}),
testimonials: PropTypes.array,
fullImage: PropTypes.string,
pricing: PropTypes.shape({
heading: PropTypes.string,
description: PropTypes.string,
plans: PropTypes.array,
}),
}

const ProductPage = ({ data }) => {
const { frontmatter } = data.markdownRemark

return (
Expand All @@ -122,6 +147,16 @@ export default ({ data }) => {
)
}

ProductPage.propTypes = {
data: PropTypes.shape({
markdownRemark: PropTypes.shape({
frontmatter: PropTypes.object,
}),
}),
}

export default ProductPage

export const productPageQuery = graphql`
query ProductPage($id: String!) {
markdownRemark(id: { eq: $id }) {
Expand Down

0 comments on commit 60ee9f1

Please sign in to comment.