var _excluded = ["children"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
import React, { useState } from 'react';
import styled from 'styled-components';
import { Button, Flexbox, FlexTable, Number } from '../../..';
import docs from '../FlexTable.mdx';
export default {
  title: 'Molecules / FlexTable / With expandable rows',
  parameters: {
    component: FlexTable,
    docs: {
      page: docs
    }
  }
};
var expandedItemsGenerator = function expandedItemsGenerator() {
  var renderComponent = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  return _toConsumableArray(Array(20)).reduce(function (acc, _, itemIndex) {
    if (itemIndex === 0) {
      var _label = 'This is a reaaaallllyyy loooong label demonstrating truncation';
      var _value = 'These values are super long to also demonstrate truncation';
      return [].concat(_toConsumableArray(acc), [{
        label: _label,
        value: _value
      }]);
    }
    var keyName = "".concat(itemIndex + 1);
    var labelText = "Label ".concat(keyName);
    var label = renderComponent ? function () {
      return React.createElement(FlexTable.ExpandItem.TextWrapper, {
        isLabel: true
      }, labelText);
    } : labelText;
    var valueText = Math.floor(Math.pow(10, 20 - Math.ceil(Math.random() * 20)) * Math.random());
    var value = renderComponent ? function () {
      return React.createElement(FlexTable.ExpandItem.TextWrapper, null, React.createElement(Number, {
        value: valueText
      }));
    } : valueText.toString();
    return [].concat(_toConsumableArray(acc), [{
      label: label,
      value: value
    }]);
  }, []);
};
var expandChildrenComponents = React.createElement(Flexbox, {
  container: true,
  justifyContent: "center",
  gap: 2
}, React.createElement(Flexbox, {
  item: true
}, React.createElement(Button, {
  size: "l",
  variant: "primary"
}, "Buy")), React.createElement(Flexbox, {
  item: true
}, React.createElement(Button, {
  size: "l",
  variant: "secondary"
}, "Sell")));
export var ExpandedTable = function ExpandedTable() {
  var expandItemsText = expandedItemsGenerator();
  var expandItemsComponents = expandedItemsGenerator(true);
  return React.createElement(FlexTable, {
    expandable: true
  }, React.createElement(FlexTable.HeaderRow, null, React.createElement(FlexTable.Header, {
    columnId: "column1"
  }, "Header 1"), React.createElement(FlexTable.Header, {
    columnId: "column2"
  }, "Header 2"), React.createElement(FlexTable.Header, {
    columnId: "column3",
    justifyContent: "flex-end"
  }, "Header 3"), React.createElement(FlexTable.Header, {
    columnId: "column4",
    hidden: true
  }, "Header 4")), React.createElement(FlexTable.Row, null, React.createElement(FlexTable.Cell, {
    columnId: "column1"
  }, "Disabled"), React.createElement(FlexTable.Cell, {
    columnId: "column2"
  }, "Disabled"), React.createElement(FlexTable.Cell, {
    columnId: "column3",
    justifyContent: "flex-end"
  }, "Disabled"), React.createElement(FlexTable.Cell, {
    columnId: "column4",
    hidden: true
  }, "Hidden")), React.createElement(FlexTable.Row, {
    expandItems: expandItemsText
  }, React.createElement(FlexTable.Cell, {
    columnId: "column1"
  }, "Expandable"), React.createElement(FlexTable.Cell, {
    columnId: "column2"
  }, "Expandable"), React.createElement(FlexTable.Cell, {
    columnId: "column3",
    justifyContent: "flex-end"
  }, "Expandable"), React.createElement(FlexTable.Cell, {
    columnId: "column4",
    hidden: true
  }, "Hidden")), React.createElement(FlexTable.Row, {
    expandItems: expandItemsText,
    initiallyExpanded: true
  }, React.createElement(FlexTable.Cell, {
    columnId: "column1"
  }, "Expandable with initial state"), React.createElement(FlexTable.Cell, {
    columnId: "column2"
  }, "Expandable with initial state"), React.createElement(FlexTable.Cell, {
    columnId: "column3",
    justifyContent: "flex-end"
  }, "Expandable with initial state"), React.createElement(FlexTable.Cell, {
    columnId: "column4",
    hidden: true
  }, "Hidden")), React.createElement(FlexTable.Row, {
    expandItems: expandItemsComponents
  }, React.createElement(FlexTable.Cell, {
    columnId: "column1"
  }, "Expandable component items"), React.createElement(FlexTable.Cell, {
    columnId: "column2"
  }, "Expandable component items"), React.createElement(FlexTable.Cell, {
    columnId: "column3",
    justifyContent: "flex-end"
  }, "Expandable component items"), React.createElement(FlexTable.Cell, {
    columnId: "column4",
    hidden: true
  }, "Hidden")), React.createElement(FlexTable.Row, {
    expandItems: expandItemsText,
    expandChildren: expandChildrenComponents
  }, React.createElement(FlexTable.Cell, {
    columnId: "column1"
  }, "Expandable with children"), React.createElement(FlexTable.Cell, {
    columnId: "column2"
  }, "Expandable with children"), React.createElement(FlexTable.Cell, {
    columnId: "column3",
    justifyContent: "flex-end"
  }, "Expandable with children"), React.createElement(FlexTable.Cell, {
    columnId: "column4",
    hidden: true
  }, "Hidden")));
};
export var OnlyExpandableOnMobileTable = function OnlyExpandableOnMobileTable() {
  var columnData = [{
    label: 'Header 4',
    value: 'Expandable 4'
  }, {
    label: 'Header 5',
    value: 'Expandable 5'
  }, {
    label: 'Header 6',
    value: 'Expandable 6'
  }, {
    label: 'Header 7',
    value: 'Expandable 7'
  }];
  return React.createElement(FlexTable, {
    expandable: true,
    md: {
      expandable: false
    }
  }, React.createElement(FlexTable.HeaderRow, null, React.createElement(FlexTable.Header, {
    columnId: "column1"
  }, "Header 1"), React.createElement(FlexTable.Header, {
    columnId: "column2"
  }, "Header 2"), React.createElement(FlexTable.Header, {
    columnId: "column3"
  }, "Header 3"), React.createElement(FlexTable.Header, {
    columnId: "column4",
    hidden: true,
    md: {
      hidden: false
    }
  }, "Header 4"), React.createElement(FlexTable.Header, {
    columnId: "column5",
    hidden: true,
    md: {
      hidden: false
    }
  }, "Header 5"), React.createElement(FlexTable.Header, {
    columnId: "column6",
    hidden: true,
    md: {
      hidden: false
    }
  }, "Header 6"), React.createElement(FlexTable.Header, {
    columnId: "column7",
    hidden: true,
    md: {
      hidden: false
    }
  }, "Header 7")), React.createElement(FlexTable.Row, {
    expandItems: columnData
  }, React.createElement(FlexTable.Cell, {
    columnId: "column1"
  }, "Expandable"), React.createElement(FlexTable.Cell, {
    columnId: "column2"
  }, "Expandable"), React.createElement(FlexTable.Cell, {
    columnId: "column3"
  }, "Expandable"), React.createElement(FlexTable.Cell, {
    columnId: "column4",
    hidden: true,
    md: {
      hidden: false
    }
  }, "Expandable 4"), React.createElement(FlexTable.Cell, {
    columnId: "column5",
    hidden: true,
    md: {
      hidden: false
    }
  }, "Expandable 5"), React.createElement(FlexTable.Cell, {
    columnId: "column6",
    hidden: true,
    md: {
      hidden: false
    }
  }, "Expandable 6"), React.createElement(FlexTable.Cell, {
    columnId: "column7",
    hidden: true,
    md: {
      hidden: false
    }
  }, "Expandable 7")));
};
var ControlledExpandedTableExample = function ControlledExpandedTableExample() {
  var expandItemsText = expandedItemsGenerator();
  var expandItemsComponents = expandedItemsGenerator(true);
  var _useState = useState(['row3']),
    _useState2 = _slicedToArray(_useState, 2),
    expandedRows = _useState2[0],
    setExpandedRows = _useState2[1];
  var isExpanded = function isExpanded(rowId) {
    return expandedRows.includes(rowId);
  };
  var onExpandClick = function onExpandClick(rowId) {
    return function (newExpanded) {
      return newExpanded ? setExpandedRows([].concat(_toConsumableArray(expandedRows), [rowId])) : setExpandedRows(expandedRows.filter(function (row) {
        return row !== rowId;
      }));
    };
  };
  return React.createElement(FlexTable, {
    expandable: true
  }, React.createElement(FlexTable.HeaderRow, null, React.createElement(FlexTable.Header, {
    columnId: "column1"
  }, "Header 1"), React.createElement(FlexTable.Header, {
    columnId: "column2"
  }, "Header 2"), React.createElement(FlexTable.Header, {
    columnId: "column3"
  }, "Header 3")), React.createElement(FlexTable.Row, null, React.createElement(FlexTable.Cell, {
    columnId: "column1"
  }, "Disabled"), React.createElement(FlexTable.Cell, {
    columnId: "column2"
  }, "Disabled"), React.createElement(FlexTable.Cell, {
    columnId: "column3"
  }, "Disabled")), React.createElement(FlexTable.Row, {
    expandItems: expandItemsText,
    expanded: isExpanded('row2'),
    onExpandToggle: onExpandClick('row2')
  }, React.createElement(FlexTable.Cell, {
    columnId: "column1"
  }, "Expandable"), React.createElement(FlexTable.Cell, {
    columnId: "column2"
  }, "Expandable"), React.createElement(FlexTable.Cell, {
    columnId: "column3"
  }, "Expandable")), React.createElement(FlexTable.Row, {
    expandItems: expandItemsText,
    expanded: isExpanded('row3'),
    onExpandToggle: onExpandClick('row3')
  }, React.createElement(FlexTable.Cell, {
    columnId: "column1"
  }, "Expandable with initial state"), React.createElement(FlexTable.Cell, {
    columnId: "column2"
  }, "Expandable with initial state"), React.createElement(FlexTable.Cell, {
    columnId: "column3"
  }, "Expandable with initial state")), React.createElement(FlexTable.Row, {
    expandItems: expandItemsComponents,
    expanded: isExpanded('row4'),
    onExpandToggle: onExpandClick('row4')
  }, React.createElement(FlexTable.Cell, {
    columnId: "column1"
  }, "Expandable component items"), React.createElement(FlexTable.Cell, {
    columnId: "column2"
  }, "Expandable component items"), React.createElement(FlexTable.Cell, {
    columnId: "column3"
  }, "Expandable component items")), React.createElement(FlexTable.Row, {
    expandItems: expandItemsText,
    expandChildren: expandChildrenComponents,
    expanded: isExpanded('row5'),
    onExpandToggle: onExpandClick('row5')
  }, React.createElement(FlexTable.Cell, {
    columnId: "column1"
  }, "Expandable with children"), React.createElement(FlexTable.Cell, {
    columnId: "column2"
  }, "Expandable with children"), React.createElement(FlexTable.Cell, {
    columnId: "column3"
  }, "Expandable with children")));
};
export var ControlledExpandedTable = {
  render: function render() {
    return React.createElement(ControlledExpandedTableExample, null);
  }
};
var RowWithPointer = styled(FlexTable.Row).withConfig({
  displayName: "WithExpandableRowsstories__RowWithPointer",
  componentId: "sc-1e1fiux-0"
})([":hover{cursor:pointer;}"]);
var ControlledExpandableTableWithClickableRowsExample = function ControlledExpandableTableWithClickableRowsExample() {
  var _useState3 = useState([]),
    _useState4 = _slicedToArray(_useState3, 2),
    expandedRows = _useState4[0],
    setExpandedRows = _useState4[1];
  var isExpanded = function isExpanded(rowId) {
    return expandedRows.includes(rowId);
  };
  var onExpandClick = function onExpandClick(rowId) {
    return function (newExpanded) {
      return newExpanded ? setExpandedRows([].concat(_toConsumableArray(expandedRows), [rowId])) : setExpandedRows(expandedRows.filter(function (row) {
        return row !== rowId;
      }));
    };
  };
  var expandItems = [{
    label: 'Expand item 1',
    value: 123
  }, {
    label: 'Expand item 2',
    value: 456
  }, {
    label: 'Expand item 3',
    value: 789
  }];
  return React.createElement(FlexTable, {
    expandable: true
  }, React.createElement(FlexTable.HeaderRow, null, React.createElement(FlexTable.Header, {
    columnId: "column1"
  }, "Header 1"), React.createElement(FlexTable.Header, {
    columnId: "column2"
  }, "Header 2"), React.createElement(FlexTable.Header, {
    columnId: "column3"
  }, "Header 3")), React.createElement(FlexTable.Row, null, React.createElement(FlexTable.Cell, {
    columnId: "column1"
  }, "Disabled"), React.createElement(FlexTable.Cell, {
    columnId: "column2"
  }, "Disabled"), React.createElement(FlexTable.Cell, {
    columnId: "column3"
  }, "Disabled")), React.createElement(RowWithPointer, {
    expandItems: expandItems,
    expanded: isExpanded('row2'),
    onClick: function onClick() {
      return onExpandClick('row2')(!isExpanded('row2'));
    }
  }, React.createElement(FlexTable.Cell, {
    columnId: "column1"
  }, "Entire..."), React.createElement(FlexTable.Cell, {
    columnId: "column2"
  }, "...row..."), React.createElement(FlexTable.Cell, {
    columnId: "column3"
  }, "...clickable")));
};
export var ControlledExpandableTableWithClickableRows = function ControlledExpandableTableWithClickableRows() {
  return React.createElement(ControlledExpandableTableWithClickableRowsExample, null);
};
export var ExpandedTableDifferentFontSizeOnMobile = function ExpandedTableDifferentFontSizeOnMobile() {
  var expandItemsText = expandedItemsGenerator();
  return React.createElement(FlexTable, {
    fontSize: "m",
    md: {
      fontSize: 's'
    },
    expandable: true
  }, React.createElement(FlexTable.HeaderRow, null, React.createElement(FlexTable.Header, {
    columnId: "column1"
  }, "Header 1"), React.createElement(FlexTable.Header, {
    columnId: "column2"
  }, "Header 2"), React.createElement(FlexTable.Header, {
    columnId: "column3"
  }, "Header 3")), React.createElement(FlexTable.Row, {
    expandItems: expandItemsText,
    initiallyExpanded: true
  }, React.createElement(FlexTable.Cell, {
    columnId: "column1"
  }, "Expandable"), React.createElement(FlexTable.Cell, {
    columnId: "column2"
  }, "Expandable"), React.createElement(FlexTable.Cell, {
    columnId: "column3"
  }, "Expandable")));
};
export var HiddenExpandItemOnDesktop = function HiddenExpandItemOnDesktop() {
  return React.createElement(FlexTable, {
    expandable: true
  }, React.createElement(FlexTable.HeaderRow, null, React.createElement(FlexTable.Header, {
    columnId: "column1"
  }, "Header 1"), React.createElement(FlexTable.Header, {
    columnId: "column2"
  }, "Header 2"), React.createElement(FlexTable.Header, {
    columnId: "column3"
  }, "Header 3")), React.createElement(FlexTable.Row, {
    initiallyExpanded: true,
    expandItems: [{
      label: 'Always visible',
      value: 123
    }, {
      label: 'Hidden on desktop',
      value: 1000,
      md: {
        hidden: true
      }
    }]
  }, React.createElement(FlexTable.Cell, {
    columnId: "column1"
  }, "Value 1"), React.createElement(FlexTable.Cell, {
    columnId: "column2"
  }, "Value 2"), React.createElement(FlexTable.Cell, {
    columnId: "column3"
  }, "Value 3")));
};
export var WholeRowExpandableTable = function WholeRowExpandableTable() {
  var expandItemsText = expandedItemsGenerator();
  return React.createElement(FlexTable, {
    expandable: true
  }, React.createElement(FlexTable.HeaderRow, null, React.createElement(FlexTable.Header, {
    columnId: "column1"
  }, "Header 1"), React.createElement(FlexTable.Header, {
    columnId: "column2"
  }, "Header 2"), React.createElement(FlexTable.Header, {
    columnId: "column3"
  }, "Header 3")), React.createElement(FlexTable.Row, {
    expandItems: expandItemsText,
    clickRowToExpand: true
  }, React.createElement(FlexTable.Cell, {
    columnId: "column1"
  }, "Click anywhere"), React.createElement(FlexTable.Cell, {
    columnId: "column2"
  }, "on this row"), React.createElement(FlexTable.Cell, {
    columnId: "column3"
  }, "to toggle expand")), React.createElement(FlexTable.Row, {
    expandItems: expandItemsText,
    expandChildren: expandChildrenComponents,
    clickRowToExpand: true
  }, React.createElement(FlexTable.Cell, {
    columnId: "column1"
  }, "Click anywhere"), React.createElement(FlexTable.Cell, {
    columnId: "column2"
  }, "on this row"), React.createElement(FlexTable.Cell, {
    columnId: "column3"
  }, "to toggle expand")), React.createElement(FlexTable.Row, {
    expandItems: expandItemsText,
    clickRowToExpand: true
  }, React.createElement(FlexTable.Cell, {
    columnId: "column1"
  }, "Click anywhere"), React.createElement(FlexTable.Cell, {
    columnId: "column2"
  }, "on this row"), React.createElement(FlexTable.Cell, {
    columnId: "column3"
  }, "to toggle expand")), React.createElement(FlexTable.Row, {
    expandItems: expandItemsText,
    expandChildren: expandChildrenComponents,
    clickRowToExpand: true
  }, React.createElement(FlexTable.Cell, {
    columnId: "column1"
  }, "Click anywhere"), React.createElement(FlexTable.Cell, {
    columnId: "column2"
  }, "on this row"), React.createElement(FlexTable.Cell, {
    columnId: "column3"
  }, "to toggle expand")), React.createElement(FlexTable.Row, {
    expandItems: expandItemsText
  }, React.createElement(FlexTable.Cell, {
    columnId: "column1"
  }, "This row is NOT clickable!"), React.createElement(FlexTable.Cell, {
    columnId: "column2"
  }, "Except for on"), React.createElement(FlexTable.Cell, {
    columnId: "column3"
  }, "the chevron...")), React.createElement(FlexTable.Row, {
    expandItems: expandItemsText,
    expandChildren: expandChildrenComponents
  }, React.createElement(FlexTable.Cell, {
    columnId: "column1"
  }, "This row is NOT clickable!"), React.createElement(FlexTable.Cell, {
    columnId: "column2"
  }, "Except for on"), React.createElement(FlexTable.Cell, {
    columnId: "column3"
  }, "the chevron...")));
};
var ControlledWholeRowExpandableTableControlledRow = function ControlledWholeRowExpandableTableControlledRow(_ref) {
  var children = _ref.children,
    rowProps = _objectWithoutProperties(_ref, _excluded);
  var _useState5 = useState(false),
    _useState6 = _slicedToArray(_useState5, 2),
    expanded = _useState6[0],
    setExpanded = _useState6[1];
  return React.createElement(FlexTable.Row, _extends({}, rowProps, {
    expanded: expanded,
    onExpandToggle: function onExpandToggle() {
      return setExpanded(!expanded);
    },
    clickRowToExpand: true
  }), children);
};
export var ControlledWholeRowExpandableTable = function ControlledWholeRowExpandableTable() {
  var expandItemsText = expandedItemsGenerator();
  return React.createElement(FlexTable, {
    expandable: true
  }, React.createElement(FlexTable.HeaderRow, null, React.createElement(FlexTable.Header, {
    columnId: "column1"
  }, "Header 1"), React.createElement(FlexTable.Header, {
    columnId: "column2"
  }, "Header 2"), React.createElement(FlexTable.Header, {
    columnId: "column3"
  }, "Header 3")), React.createElement(ControlledWholeRowExpandableTableControlledRow, {
    expandItems: expandItemsText,
    expandChildren: expandChildrenComponents
  }, React.createElement(FlexTable.Cell, {
    columnId: "column1"
  }, "This row is"), React.createElement(FlexTable.Cell, {
    columnId: "column2"
  }, "a controlled example"), React.createElement(FlexTable.Cell, {
    columnId: "column3"
  }, "that handles the expansion")), React.createElement(ControlledWholeRowExpandableTableControlledRow, {
    expandItems: expandItemsText
  }, React.createElement(FlexTable.Cell, {
    columnId: "column1"
  }, "This row is"), React.createElement(FlexTable.Cell, {
    columnId: "column2"
  }, "a controlled example"), React.createElement(FlexTable.Cell, {
    columnId: "column3"
  }, "that handles the expansion")));
};