switch.stories.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import React, { useState } from 'react';
  2. import { Button } from '@douyinfe/semi-ui/';
  3. import {
  4. storiesOf
  5. } from '@storybook/react';
  6. // import { withKnobs, text, boolean } from '@storybook/addon-knobs';
  7. import { Switch } from '../../index';
  8. const stories = storiesOf('Switch', module);
  9. // stories.addDecorator(withKnobs);;
  10. stories.add('switch', () => (
  11. <div>
  12. <Switch onChange={(v, e) => console.log(v)} aria-label='power-switch'>
  13. </Switch>
  14. <Switch defaultChecked={true} onChange={(v, e) => console.log(v)} aria-label='power-switch'>
  15. </Switch>
  16. </div>
  17. ));
  18. stories.add('switch size', () => (
  19. <div>
  20. <Switch onChange={(v, e) => console.log(v)}></Switch>
  21. <Switch onChange={(v, e) => console.log(v)} size='small' aria-label='power-switch'></Switch>
  22. <Switch onChange={(v, e) => console.log(v)} size='large' aria-label='power-switch'></Switch>
  23. </div>
  24. ));
  25. stories.add('switch checkedText & uncheckedText', () => (
  26. <div>
  27. <Switch defaultChecked checkedText='开' uncheckedText='关' aria-label='power-switch'/>
  28. <Switch checkedText={'|'} uncheckedText='〇' />
  29. <br/><br/>
  30. <Switch checkedText='开' uncheckedText='关' />
  31. <Switch defaultChecked checkedText='|' uncheckedText='〇' aria-label='power-switch'/>
  32. <br/><br/>
  33. <Switch checkedText='开' uncheckedText='关' size='large' aria-label='power-switch'/>
  34. <Switch checkedText='|' uncheckedText='〇' size='large' aria-label='power-switch'/>
  35. <br/><br/>
  36. <Switch defaultChecked checkedText='开' uncheckedText='关' size='large' aria-label='power-switch'/>
  37. <Switch defaultChecked checkedText='|' uncheckedText='〇' size='large' aria-label='power-switch'/>
  38. </div>
  39. ));
  40. stories.add('switch disabled', () => (
  41. <>
  42. <Switch disabled>
  43. disabled
  44. </Switch>
  45. <Switch disabled checked={true} onChange={(v, e) => console.log(v)} aria-label='power-switch'>
  46. </Switch>
  47. </>
  48. ));
  49. const ControlledSwitch = () => {
  50. const [checked, onChange] = useState(true);
  51. return (
  52. <Switch checked={checked} onChange={(v, e) => onChange(v)} />
  53. );
  54. };
  55. stories.add('switch checked + onChange', () => <ControlledSwitch/>);
  56. const UnControlledSwitch = () => {
  57. const onChange = checked => {
  58. console.log(checked);
  59. };
  60. return (
  61. <>
  62. {/* <Switch onChange={onChange} defaultChecked={false}/> */}
  63. <Switch onChange={onChange} defaultChecked={true}/>
  64. </>
  65. );
  66. };
  67. stories.add('switch defaultChecked + onChange', () => <UnControlledSwitch/>);
  68. class LoadingDemo extends React.Component {
  69. constructor(props) {
  70. super(props);
  71. this.state = {
  72. checked: true,
  73. loading:false
  74. }
  75. this.onChange = this.onChange.bind(this);
  76. }
  77. onChange(checked) {
  78. this.setState({ checked });
  79. }
  80. render() {
  81. return (
  82. <>
  83. <Button onClick={() => { this.setState({ checked: true }); }}>
  84. checked
  85. </Button>
  86. <br /><br />
  87. <Button onClick={() => { this.setState({ checked: false }); }}>
  88. unchecked
  89. </Button>
  90. <br /><br />
  91. <Button onClick={() => { this.setState({ loading: !this.state.loading }); }}>
  92. loading
  93. </Button>
  94. <br /><br />
  95. <Switch
  96. checked={this.state.checked}
  97. onChange={this.onChange}
  98. loading={this.state.loading}>
  99. </Switch>
  100. <br /><br />
  101. <hr />
  102. <Switch loading disabled/>
  103. <br /><br />
  104. <Switch loading disabled defaultChecked/>
  105. <br /><br />
  106. </>
  107. )
  108. }
  109. }
  110. stories.add('loading', () => <LoadingDemo/>);