25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

326 satır
11 KiB

  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _extends2 = require('babel-runtime/helpers/extends');
  6. var _extends3 = _interopRequireDefault(_extends2);
  7. var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
  8. var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
  9. var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
  10. var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
  11. var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
  12. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  13. var _createClass2 = require('babel-runtime/helpers/createClass');
  14. var _createClass3 = _interopRequireDefault(_createClass2);
  15. var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
  16. var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
  17. var _inherits2 = require('babel-runtime/helpers/inherits');
  18. var _inherits3 = _interopRequireDefault(_inherits2);
  19. var _simpleAssign = require('simple-assign');
  20. var _simpleAssign2 = _interopRequireDefault(_simpleAssign);
  21. var _react = require('react');
  22. var _react2 = _interopRequireDefault(_react);
  23. var _propTypes = require('prop-types');
  24. var _propTypes2 = _interopRequireDefault(_propTypes);
  25. var _warning = require('warning');
  26. var _warning2 = _interopRequireDefault(_warning);
  27. var _TabTemplate = require('./TabTemplate');
  28. var _TabTemplate2 = _interopRequireDefault(_TabTemplate);
  29. var _InkBar = require('./InkBar');
  30. var _InkBar2 = _interopRequireDefault(_InkBar);
  31. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  32. function getStyles(props, context) {
  33. var tabs = context.muiTheme.tabs;
  34. return {
  35. tabItemContainer: {
  36. width: '100%',
  37. backgroundColor: tabs.backgroundColor,
  38. whiteSpace: 'nowrap',
  39. display: 'flex'
  40. }
  41. };
  42. }
  43. var Tabs = function (_Component) {
  44. (0, _inherits3.default)(Tabs, _Component);
  45. function Tabs() {
  46. var _ref;
  47. var _temp, _this, _ret;
  48. (0, _classCallCheck3.default)(this, Tabs);
  49. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  50. args[_key] = arguments[_key];
  51. }
  52. return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = Tabs.__proto__ || (0, _getPrototypeOf2.default)(Tabs)).call.apply(_ref, [this].concat(args))), _this), _this.state = { selectedIndex: 0 }, _this.handleTabClick = function (value, event, tab) {
  53. var valueLink = _this.getValueLink(_this.props);
  54. var index = tab.props.index;
  55. if (valueLink.value && valueLink.value !== value || _this.state.selectedIndex !== index) {
  56. valueLink.requestChange(value, event, tab);
  57. }
  58. _this.setState({ selectedIndex: index });
  59. if (tab.props.onActive) {
  60. tab.props.onActive(tab);
  61. }
  62. }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  63. }
  64. (0, _createClass3.default)(Tabs, [{
  65. key: 'componentWillMount',
  66. value: function componentWillMount() {
  67. var valueLink = this.getValueLink(this.props);
  68. var initialIndex = this.props.initialSelectedIndex;
  69. this.setState({
  70. selectedIndex: valueLink.value !== undefined ? this.getSelectedIndex(this.props) : initialIndex < this.getTabCount() ? initialIndex : 0
  71. });
  72. }
  73. }, {
  74. key: 'componentWillReceiveProps',
  75. value: function componentWillReceiveProps(newProps, nextContext) {
  76. var valueLink = this.getValueLink(newProps);
  77. var newState = {
  78. muiTheme: nextContext.muiTheme || this.context.muiTheme
  79. };
  80. if (valueLink.value !== undefined) {
  81. newState.selectedIndex = this.getSelectedIndex(newProps);
  82. }
  83. this.setState(newState);
  84. }
  85. }, {
  86. key: 'getTabs',
  87. value: function getTabs() {
  88. var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.props;
  89. var tabs = [];
  90. _react.Children.forEach(props.children, function (tab) {
  91. if ((0, _react.isValidElement)(tab)) {
  92. tabs.push(tab);
  93. }
  94. });
  95. return tabs;
  96. }
  97. }, {
  98. key: 'getTabCount',
  99. value: function getTabCount() {
  100. return this.getTabs().length;
  101. }
  102. // Do not use outside of this component, it will be removed once valueLink is deprecated
  103. }, {
  104. key: 'getValueLink',
  105. value: function getValueLink(props) {
  106. return props.valueLink || {
  107. value: props.value,
  108. requestChange: props.onChange
  109. };
  110. }
  111. }, {
  112. key: 'getSelectedIndex',
  113. value: function getSelectedIndex(props) {
  114. var valueLink = this.getValueLink(props);
  115. var selectedIndex = -1;
  116. this.getTabs(props).forEach(function (tab, index) {
  117. if (valueLink.value === tab.props.value) {
  118. selectedIndex = index;
  119. }
  120. });
  121. return selectedIndex;
  122. }
  123. }, {
  124. key: 'getSelected',
  125. value: function getSelected(tab, index) {
  126. var valueLink = this.getValueLink(this.props);
  127. return valueLink.value ? valueLink.value === tab.props.value : this.state.selectedIndex === index;
  128. }
  129. }, {
  130. key: 'render',
  131. value: function render() {
  132. var _this2 = this;
  133. var _props = this.props,
  134. contentContainerClassName = _props.contentContainerClassName,
  135. contentContainerStyle = _props.contentContainerStyle,
  136. initialSelectedIndex = _props.initialSelectedIndex,
  137. inkBarStyle = _props.inkBarStyle,
  138. inkBarContainerStyle = _props.inkBarContainerStyle,
  139. onChange = _props.onChange,
  140. style = _props.style,
  141. tabItemContainerStyle = _props.tabItemContainerStyle,
  142. tabTemplate = _props.tabTemplate,
  143. tabTemplateStyle = _props.tabTemplateStyle,
  144. other = (0, _objectWithoutProperties3.default)(_props, ['contentContainerClassName', 'contentContainerStyle', 'initialSelectedIndex', 'inkBarStyle', 'inkBarContainerStyle', 'onChange', 'style', 'tabItemContainerStyle', 'tabTemplate', 'tabTemplateStyle']);
  145. var prepareStyles = this.context.muiTheme.prepareStyles;
  146. var styles = getStyles(this.props, this.context);
  147. var valueLink = this.getValueLink(this.props);
  148. var tabValue = valueLink.value;
  149. var tabContent = [];
  150. var width = 100 / this.getTabCount();
  151. var tabs = this.getTabs().map(function (tab, index) {
  152. process.env.NODE_ENV !== "production" ? (0, _warning2.default)(tab.type && tab.type.muiName === 'Tab', 'Material-UI: Tabs only accepts Tab Components as children.\n Found ' + (tab.type.muiName || tab.type) + ' as child number ' + (index + 1) + ' of Tabs') : void 0;
  153. process.env.NODE_ENV !== "production" ? (0, _warning2.default)(!tabValue || tab.props.value !== undefined, 'Material-UI: Tabs value prop has been passed, but Tab ' + index + '\n does not have a value prop. Needs value if Tabs is going\n to be a controlled component.') : void 0;
  154. tabContent.push(tab.props.children ? (0, _react.createElement)(tabTemplate || _TabTemplate2.default, {
  155. key: index,
  156. selected: _this2.getSelected(tab, index),
  157. style: tabTemplateStyle
  158. }, tab.props.children) : undefined);
  159. return (0, _react.cloneElement)(tab, {
  160. key: index,
  161. index: index,
  162. selected: _this2.getSelected(tab, index),
  163. width: width + '%',
  164. onClick: _this2.handleTabClick
  165. });
  166. });
  167. var realSelectedIndex = valueLink.value ? this.getSelectedIndex(this.props) : this.state.selectedIndex;
  168. var inkBar = realSelectedIndex !== -1 ? _react2.default.createElement(_InkBar2.default, {
  169. left: width * realSelectedIndex + '%',
  170. width: width + '%',
  171. style: inkBarStyle
  172. }) : null;
  173. var inkBarContainerWidth = tabItemContainerStyle ? tabItemContainerStyle.width : '100%';
  174. return _react2.default.createElement(
  175. 'div',
  176. (0, _extends3.default)({ style: prepareStyles((0, _simpleAssign2.default)({}, style)) }, other),
  177. _react2.default.createElement(
  178. 'div',
  179. { style: prepareStyles((0, _simpleAssign2.default)(styles.tabItemContainer, tabItemContainerStyle)) },
  180. tabs
  181. ),
  182. _react2.default.createElement(
  183. 'div',
  184. { style: prepareStyles((0, _simpleAssign2.default)({ width: inkBarContainerWidth }, inkBarContainerStyle)) },
  185. inkBar
  186. ),
  187. _react2.default.createElement(
  188. 'div',
  189. {
  190. style: prepareStyles((0, _simpleAssign2.default)({}, contentContainerStyle)),
  191. className: contentContainerClassName
  192. },
  193. tabContent
  194. )
  195. );
  196. }
  197. }]);
  198. return Tabs;
  199. }(_react.Component);
  200. Tabs.defaultProps = {
  201. initialSelectedIndex: 0,
  202. onChange: function onChange() {}
  203. };
  204. Tabs.contextTypes = {
  205. muiTheme: _propTypes2.default.object.isRequired
  206. };
  207. Tabs.propTypes = process.env.NODE_ENV !== "production" ? {
  208. /**
  209. * Should be used to pass `Tab` components.
  210. */
  211. children: _propTypes2.default.node,
  212. /**
  213. * The css class name of the root element.
  214. */
  215. className: _propTypes2.default.string,
  216. /**
  217. * The css class name of the content's container.
  218. */
  219. contentContainerClassName: _propTypes2.default.string,
  220. /**
  221. * Override the inline-styles of the content's container.
  222. */
  223. contentContainerStyle: _propTypes2.default.object,
  224. /**
  225. * Specify initial visible tab index.
  226. * If `initialSelectedIndex` is set but larger than the total amount of specified tabs,
  227. * `initialSelectedIndex` will revert back to default.
  228. * If `initialSelectedIndex` is set to any negative value, no tab will be selected intially.
  229. */
  230. initialSelectedIndex: _propTypes2.default.number,
  231. /**
  232. * Override the inline-styles of the InkBar.
  233. */
  234. inkBarStyle: _propTypes2.default.object,
  235. /**
  236. * Override the inline-styles of the InkBar container.
  237. */
  238. inkBarContainerStyle: _propTypes2.default.object,
  239. /**
  240. * Called when the selected value change.
  241. */
  242. onChange: _propTypes2.default.func,
  243. /**
  244. * Override the inline-styles of the root element.
  245. */
  246. style: _propTypes2.default.object,
  247. /**
  248. * Override the inline-styles of the tab-labels container.
  249. */
  250. tabItemContainerStyle: _propTypes2.default.object,
  251. /**
  252. * Override the default tab template used to wrap the content of each tab element.
  253. */
  254. tabTemplate: _propTypes2.default.func,
  255. /**
  256. * Override the inline-styles of the tab template.
  257. */
  258. tabTemplateStyle: _propTypes2.default.object,
  259. /**
  260. * Makes Tabs controllable and selects the tab whose value prop matches this prop.
  261. */
  262. value: _propTypes2.default.any
  263. } : {};
  264. exports.default = Tabs;