Skip to content

Instantly share code, notes, and snippets.

@fernandocamargo
Created January 10, 2020 00:10
Show Gist options
  • Save fernandocamargo/699a43f9b60cb1d0406260451e5b7f2e to your computer and use it in GitHub Desktop.
Save fernandocamargo/699a43f9b60cb1d0406260451e5b7f2e to your computer and use it in GitHub Desktop.
Recursive prop-types (menu-like)
import { func, node, oneOf, oneOfType, shape, string } from 'prop-types';
import items from './items';
export const item = {
id: string.isRequired,
children: node.isRequired,
url: oneOfType([string, func]),
target: oneOf(['_blank']),
};
export default function() {
return shape({ ...item, items }).apply(this, arguments);
}
import { arrayOf } from 'prop-types';
import item from './item';
export default function() {
return arrayOf(item).apply(this, arguments);
}
@fernandocamargo
Copy link
Author

Updated:

item.js

import { bool, func, node, oneOfType, string } from 'prop-types';

import { propTypes as items } from '../items';

export const propTypes = {
  label: node.isRequired,
  url: oneOfType([func, string]),
  fragment: string,
  active: bool,
  exact: bool,
  disabled: bool,
  items: function() {
    return items.apply(this, arguments);
  },
};

items.js

import { arrayOf, shape } from 'prop-types';

import { propTypes as item } from '../item';

export const propTypes = function() {
  return arrayOf(shape(item)).apply(this, arguments);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment