gridEditable.stories.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import {Component, Fragment} from 'react';
  2. import PropTypes from 'prop-types';
  3. import Button from 'sentry/components/button';
  4. import GridEditable from 'sentry/components/gridEditable';
  5. const COLUMN_ORDER = [
  6. {
  7. key: 'farm',
  8. name: 'farm',
  9. width: -1,
  10. },
  11. {
  12. key: 'count(apple)',
  13. name: 'apples sold',
  14. width: -1,
  15. },
  16. {
  17. key: 'count(banana)',
  18. name: 'banana sold',
  19. width: -1,
  20. },
  21. ];
  22. const COLUMN_SORT_BY = [
  23. {
  24. key: 'count(apple)',
  25. order: -1,
  26. },
  27. ];
  28. const DATA = [
  29. {
  30. farm: 'Old McDonalds',
  31. 'count(apple)': 100,
  32. 'count(banana)': 500,
  33. 'count(cherry)': 200,
  34. 'count(date)': 400,
  35. 'count(eggplant)': 600,
  36. },
  37. {
  38. farm: 'Animal Farm',
  39. 'count(apple)': 900,
  40. 'count(banana)': 600,
  41. 'count(cherry)': 200,
  42. 'count(date)': 500,
  43. 'count(eggplant)': 700,
  44. },
  45. {
  46. farm: 'Avocado Toast Farm',
  47. 'count(apple)': 700,
  48. 'count(banana)': 600,
  49. 'count(cherry)': 500,
  50. 'count(date)': 400,
  51. 'count(eggplant)': 500,
  52. },
  53. ];
  54. class GridParent extends Component {
  55. static propTypes = {
  56. withHeader: PropTypes.bool,
  57. title: PropTypes.string,
  58. };
  59. state = {
  60. columnOrder: [...COLUMN_ORDER],
  61. columnSortBy: [...COLUMN_SORT_BY],
  62. };
  63. handleResizeColumn = (index, newColumn) => {
  64. const columnOrder = [...this.state.columnOrder];
  65. columnOrder[index] = {...columnOrder[index], width: newColumn.width};
  66. this.setState({columnOrder});
  67. };
  68. render() {
  69. const {withHeader, title} = this.props;
  70. const headerButtons = withHeader
  71. ? () => <Button size="sm">Action Button</Button>
  72. : null;
  73. return (
  74. <GridEditable
  75. headerButtons={headerButtons}
  76. isLoading={false}
  77. error={null}
  78. data={DATA}
  79. columnOrder={this.state.columnOrder}
  80. columnSortBy={this.state.columnSortBy}
  81. title={title}
  82. grid={{
  83. onResizeColumn: this.handleResizeColumn,
  84. }}
  85. />
  86. );
  87. }
  88. }
  89. export default {
  90. title: 'Components/Tables/GridEditable',
  91. };
  92. export const Default = () => (
  93. <Fragment>
  94. <div className="section">
  95. <h2>Basic Table</h2>
  96. <GridParent />
  97. </div>
  98. </Fragment>
  99. );
  100. Default.storyName = 'default';
  101. Default.parameters = {
  102. docs: {
  103. description: {
  104. story: 'Render a simple resizable table',
  105. },
  106. },
  107. };
  108. export const WithAHeader = () => (
  109. <div className="section">
  110. <h2>Table with title & header buttons</h2>
  111. <GridParent withHeader title="Results" />
  112. </div>
  113. );
  114. WithAHeader.storyName = 'with a header';
  115. WithAHeader.parameters = {
  116. docs: {
  117. description: {
  118. story: 'Include a header and action buttons',
  119. },
  120. },
  121. };
  122. export const IsLoading = () => (
  123. <div className="section">
  124. <h2>Loading</h2>
  125. <GridEditable
  126. isEditable={false}
  127. isLoading
  128. error={null}
  129. data={DATA}
  130. columnOrder={COLUMN_ORDER}
  131. columnSortBy={COLUMN_SORT_BY}
  132. grid={{}}
  133. />
  134. </div>
  135. );
  136. IsLoading.storyName = 'isLoading';
  137. export const IsError = () => (
  138. <div className="section">
  139. <h2>Error</h2>
  140. <GridEditable
  141. isEditable={false}
  142. isLoading
  143. error="These aren't the droids you're looking for."
  144. data={DATA}
  145. columnOrder={COLUMN_ORDER}
  146. columnSortBy={COLUMN_SORT_BY}
  147. grid={{}}
  148. />
  149. </div>
  150. );
  151. IsError.storyName = 'isError';