gridEditable.stories.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import React from 'react';
  2. import {storiesOf} from '@storybook/react';
  3. import {withInfo} from '@storybook/addon-info';
  4. import styled from '@emotion/styled';
  5. import Button from 'app/components/button';
  6. import GlobalModal from 'app/components/globalModal';
  7. import GridEditable from 'app/components/gridEditable';
  8. const Section = styled('div')`
  9. margin-bottom: 32px;
  10. `;
  11. const COLUMN_ORDER = [
  12. {
  13. key: 'farm',
  14. name: 'farm',
  15. },
  16. {
  17. key: 'count(apple)',
  18. name: 'apples sold',
  19. },
  20. {
  21. key: 'count(banana)',
  22. name: 'banana sold',
  23. },
  24. ];
  25. const COLUMN_SORT_BY = [
  26. {
  27. key: 'count(apple)',
  28. order: -1,
  29. },
  30. ];
  31. const DATA = [
  32. {
  33. farm: 'Old McDonalds',
  34. 'count(apple)': 100,
  35. 'count(banana)': 500,
  36. 'count(cherry)': 200,
  37. 'count(date)': 400,
  38. 'count(eggplant)': 600,
  39. },
  40. {
  41. farm: 'Animal Farm',
  42. 'count(apple)': 900,
  43. 'count(banana)': 600,
  44. 'count(cherry)': 200,
  45. 'count(date)': 500,
  46. 'count(eggplant)': 700,
  47. },
  48. {
  49. farm: 'Avocado Toast Farm',
  50. 'count(apple)': 700,
  51. 'count(banana)': 600,
  52. 'count(cherry)': 500,
  53. 'count(date)': 400,
  54. 'count(eggplant)': 500,
  55. },
  56. ];
  57. class GridParent extends React.Component {
  58. state = {
  59. columnOrder: [...COLUMN_ORDER],
  60. columnSortBy: [...COLUMN_SORT_BY],
  61. };
  62. createColumn = () => {
  63. const dataRow = DATA[0];
  64. const keys = Object.keys(dataRow);
  65. const randomKey = keys[Math.floor(Math.random() * keys.length)];
  66. this.setState({
  67. columnOrder: [
  68. ...this.state.columnOrder,
  69. {
  70. key: randomKey,
  71. name: randomKey,
  72. },
  73. ],
  74. });
  75. };
  76. updateColumn = (i, nextColumn) => {
  77. const {columnOrder} = this.state;
  78. this.setState({
  79. columnOrder: [...columnOrder.slice(0, i), nextColumn, ...columnOrder.slice(i + 1)],
  80. });
  81. };
  82. deleteColumn = i => {
  83. const {columnOrder} = this.state;
  84. this.setState({
  85. columnOrder: [...columnOrder.slice(0, i), ...columnOrder.slice(i + 1)],
  86. });
  87. };
  88. renderModalBodyWithForm = (i, column) => {
  89. return (
  90. <React.Fragment>
  91. {column ? (
  92. <Button onClick={() => this.updateColumn(i, {...column, name: 'Sentry'})}>
  93. Rename this column to "Sentry"
  94. </Button>
  95. ) : (
  96. <Button onClick={this.createColumn}>Add a random column</Button>
  97. )}
  98. <br />
  99. <br />
  100. <div>You should create a user-friendly form here to edit the columns</div>
  101. </React.Fragment>
  102. );
  103. };
  104. renderModalFooter = () => {
  105. return <div>This is the footer</div>;
  106. };
  107. render() {
  108. return (
  109. <GridEditable
  110. isEditable
  111. isLoading={false}
  112. error={null}
  113. data={DATA}
  114. columnOrder={this.state.columnOrder}
  115. columnSortBy={this.state.columnSortBy}
  116. grid={{}}
  117. modalEditColumn={{
  118. renderBodyWithForm: this.renderModalBodyWithForm,
  119. renderFooter: this.renderModalFooter,
  120. }}
  121. actions={{
  122. deleteColumn: this.deleteColumn,
  123. moveColumn: () => {},
  124. }}
  125. />
  126. );
  127. }
  128. }
  129. storiesOf('UI|GridEditable', module)
  130. .add(
  131. 'default',
  132. withInfo('There is a dependency on GlobalModal to display the Modal')(() => (
  133. <React.Fragment>
  134. <Section>
  135. <h2>{'isEditable={true}'}</h2>
  136. <GridParent />
  137. </Section>
  138. <Section>
  139. <h2>{'isEditable={false}'}</h2>
  140. <GridEditable
  141. isEditable={false}
  142. isLoading={false}
  143. error={null}
  144. data={DATA}
  145. columnOrder={COLUMN_ORDER}
  146. columnSortBy={COLUMN_SORT_BY}
  147. grid={{}}
  148. modalEditColumn={{
  149. renderBodyWithForm: () => {},
  150. renderFooter: () => {},
  151. }}
  152. actions={{
  153. deleteColumn: () => {},
  154. moveColumn: () => {},
  155. }}
  156. />
  157. </Section>
  158. <GlobalModal />
  159. </React.Fragment>
  160. ))
  161. )
  162. .add(
  163. 'isLoading',
  164. withInfo('')(() => (
  165. <Section>
  166. <h2>Loading</h2>
  167. <GridEditable
  168. isEditable={false}
  169. isLoading
  170. error={null}
  171. data={DATA}
  172. columnOrder={COLUMN_ORDER}
  173. columnSortBy={COLUMN_SORT_BY}
  174. grid={{}}
  175. modalEditColumn={{
  176. renderBodyWithForm: () => <div>ModalBody</div>,
  177. renderFooter: () => <div>ModalFooter</div>,
  178. }}
  179. actions={{
  180. deleteColumn: () => {},
  181. moveColumn: () => {},
  182. }}
  183. />
  184. </Section>
  185. ))
  186. )
  187. .add(
  188. 'isError',
  189. withInfo('')(() => (
  190. <Section>
  191. <h2>Error</h2>
  192. <GridEditable
  193. isEditable={false}
  194. isLoading
  195. error="These aren't the droids you're looking for."
  196. data={DATA}
  197. columnOrder={COLUMN_ORDER}
  198. columnSortBy={COLUMN_SORT_BY}
  199. grid={{}}
  200. modalEditColumn={{
  201. renderBodyWithForm: () => <div>ModalBody</div>,
  202. renderFooter: () => <div>ModalFooter</div>,
  203. }}
  204. actions={{
  205. deleteColumn: () => {},
  206. moveColumn: () => {},
  207. }}
  208. />
  209. </Section>
  210. ))
  211. );