segmentedControl.stories.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import {Fragment, useState} from 'react';
  2. import {SegmentedControl} from 'sentry/components/segmentedControl';
  3. import JSXNode from 'sentry/components/stories/jsxNode';
  4. import Matrix from 'sentry/components/stories/matrix';
  5. import SideBySide from 'sentry/components/stories/sideBySide';
  6. import {IconStats} from 'sentry/icons';
  7. import storyBook from 'sentry/stories/storyBook';
  8. export default storyBook(SegmentedControl, story => {
  9. story('Default', () => (
  10. <Fragment>
  11. <p>
  12. By default <JSXNode name="SegmentedControl" /> will keep track of what is
  13. selected.
  14. </p>
  15. <SegmentedControl>
  16. <SegmentedControl.Item key="one">One</SegmentedControl.Item>
  17. <SegmentedControl.Item key="two">Two</SegmentedControl.Item>
  18. <SegmentedControl.Item key="three">Three</SegmentedControl.Item>
  19. </SegmentedControl>
  20. </Fragment>
  21. ));
  22. story('Controlled Value', () => {
  23. const [value, setValue] = useState('two');
  24. return (
  25. <Fragment>
  26. <p>
  27. If you want to control the state of the tabs from outside, you can call{' '}
  28. <var>{'useState()'}</var> and set{' '}
  29. <JSXNode name="SegmentedControl" props={{value: String, onChange: Function}} />{' '}
  30. manually.
  31. </p>
  32. <p>selected={value}</p>
  33. <SegmentedControl value={value} onChange={setValue}>
  34. <SegmentedControl.Item key="one">One</SegmentedControl.Item>
  35. <SegmentedControl.Item key="two">Two</SegmentedControl.Item>
  36. <SegmentedControl.Item key="three">Three</SegmentedControl.Item>
  37. </SegmentedControl>
  38. </Fragment>
  39. );
  40. });
  41. story('Props', () => (
  42. <Matrix
  43. render={props => (
  44. <SegmentedControl {...props}>
  45. <SegmentedControl.Item key="one">One</SegmentedControl.Item>
  46. <SegmentedControl.Item key="two">Two</SegmentedControl.Item>
  47. <SegmentedControl.Item key="three">Three</SegmentedControl.Item>
  48. </SegmentedControl>
  49. )}
  50. selectedProps={['priority', 'size']}
  51. propMatrix={{
  52. size: ['md' as const, 'sm' as const, 'xs' as const],
  53. priority: ['default' as const, 'primary' as const],
  54. }}
  55. />
  56. ));
  57. story('SegmentedControl.Item', () => (
  58. <SideBySide>
  59. <Matrix
  60. render={({showChild, ...props}) => (
  61. <SegmentedControl>
  62. <SegmentedControl.Item
  63. key="one"
  64. aria-label={showChild ? undefined : 'One'}
  65. {...props}
  66. >
  67. {showChild ? 'One' : undefined}
  68. </SegmentedControl.Item>
  69. <SegmentedControl.Item
  70. key="two"
  71. aria-label={showChild ? undefined : 'Two'}
  72. {...props}
  73. >
  74. {showChild ? 'Two' : undefined}
  75. </SegmentedControl.Item>
  76. <SegmentedControl.Item
  77. key="three"
  78. aria-label={showChild ? undefined : 'Three'}
  79. {...props}
  80. >
  81. {showChild ? 'Three' : undefined}
  82. </SegmentedControl.Item>
  83. </SegmentedControl>
  84. )}
  85. selectedProps={['showChild', 'icon']}
  86. propMatrix={{
  87. showChild: [true, false],
  88. icon: [undefined, <IconStats key="play" />],
  89. }}
  90. />
  91. <Matrix
  92. render={props => (
  93. <SegmentedControl>
  94. <SegmentedControl.Item key="one" tooltip="One">
  95. One
  96. </SegmentedControl.Item>
  97. <SegmentedControl.Item key="two" {...props}>
  98. Two
  99. </SegmentedControl.Item>
  100. <SegmentedControl.Item key="three" tooltip="Three">
  101. Three
  102. </SegmentedControl.Item>
  103. </SegmentedControl>
  104. )}
  105. selectedProps={['tooltip', 'disabled']}
  106. propMatrix={{
  107. tooltip: [undefined, 'Pick Me'],
  108. disabled: [false, true],
  109. }}
  110. />
  111. <Matrix
  112. render={props => (
  113. <SegmentedControl priority={props.priority}>
  114. <SegmentedControl.Item key="one" disabled={props.disabled}>
  115. One
  116. </SegmentedControl.Item>
  117. <SegmentedControl.Item key="two" disabled={props.disabled}>
  118. Two
  119. </SegmentedControl.Item>
  120. <SegmentedControl.Item key="three" disabled={props.disabled}>
  121. Three
  122. </SegmentedControl.Item>
  123. </SegmentedControl>
  124. )}
  125. selectedProps={['disabled', 'priority']}
  126. propMatrix={{
  127. disabled: [true, false],
  128. priority: ['default' as const, 'primary' as const],
  129. }}
  130. />
  131. </SideBySide>
  132. ));
  133. });