@instructure/ui-react-utils
Version:
A React utility library made by Instructure Inc.
97 lines (92 loc) • 3.82 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.ComponentIdentifier = void 0;
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _matchComponentTypes = require("./matchComponentTypes");
var _ensureSingleChild = require("./ensureSingleChild");
var _deprecated = _interopRequireDefault(require("./deprecated"));
const _excluded = ["children"];
var _dec, _class, _class2;
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 - present Instructure, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
// TODO delete this class, its not used anywhere, see livegrep
/**
---
category: utilities/react
---
Abstract component identifier. Helpful for picking out a specific child.
```js
class App extends Component {
render () {
const title = pick(Title, this.props.children)
const content = pick(Content, this.props.children)
return (
<div>
{title}
<ContextView>
{content}
</ContextView>
</div>
)
}
}
class Title extends ComponentIdentifier { static displayName = 'Title' }
class Content extends ComponentIdentifier { static displayName = 'Content' }
ReactDOM.render(
<App>
<Title><h2>Hello World!</h2></Title>
<Content><div>This text gets decorated within `App`.</div></Content>
</App>,
document.getElementById('container')
)
```
@module ComponentIdentifier
**/
let ComponentIdentifier = exports.ComponentIdentifier = (_dec = (0, _deprecated.default)('8.0.0', void 0, 'This component will be removed in InstUI 9.0 since its not used.'), _dec(_class = (_class2 = class ComponentIdentifier extends _react.Component {
render() {
const _this$props = this.props,
children = _this$props.children,
props = (0, _objectWithoutProperties2.default)(_this$props, _excluded);
return (0, _ensureSingleChild.ensureSingleChild)(children, props);
}
}, _class2.displayName = "ComponentIdentifier", _class2.propTypes = {
children: _propTypes.default.node
}, _class2.defaultProps = {
children: null
}, _class2.pick = (component, children) => {
let result;
_react.default.Children.forEach(children, child => {
if ((0, _matchComponentTypes.matchComponentTypes)(child, [component])) {
result = child;
}
});
return result;
}, _class2)) || _class);
var _default = exports.default = ComponentIdentifier;