calendar.stories.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. import React, { useState } from 'react';
  2. import Calendar from '../index';
  3. import { Button, RadioGroup, Radio, LocaleProvider, Select } from '@douyinfe/semi-ui';
  4. import zh_CN from '@douyinfe/semi-ui/locale/source/zh_CN';
  5. import en_GB from '@douyinfe/semi-ui/locale/source/en_GB';
  6. import en_US from '@douyinfe/semi-ui/locale/source/en_US';
  7. import ko_KR from '@douyinfe/semi-ui/locale/source/ko_KR';
  8. import ja_JP from '@douyinfe/semi-ui/locale/source/ja_JP';
  9. import ar from '@douyinfe/semi-ui/locale/source/ar';
  10. import vi_VN from '@douyinfe/semi-ui/locale/source/vi_VN';
  11. import ru_RU from '@douyinfe/semi-ui/locale/source/ru_RU';
  12. import id_ID from '@douyinfe/semi-ui/locale/source/id_ID';
  13. import ms_MY from '@douyinfe/semi-ui/locale/source/ms_MY';
  14. import th_TH from '@douyinfe/semi-ui/locale/source/th_TH';
  15. import tr_TR from '@douyinfe/semi-ui/locale/source/tr_TR';
  16. import pt_BR from '@douyinfe/semi-ui/locale/source/pt_BR';
  17. const { Option } = Select;
  18. export default {
  19. title: 'Calendar',
  20. }
  21. const time = new Date();
  22. let id = 0;
  23. const language = {
  24. zh_CN: zh_CN,
  25. en_GB: en_GB,
  26. ko_KR: ko_KR,
  27. ja_JP: ja_JP,
  28. ar: ar,
  29. vi_VN: vi_VN,
  30. ru_RU: ru_RU,
  31. id_ID: id_ID,
  32. ms_MY: ms_MY,
  33. th_TH: th_TH,
  34. tr_TR: tr_TR,
  35. pt_BR: pt_BR,
  36. };
  37. const events = [
  38. {
  39. allDay: true,
  40. children: <div style={{ backgroundColor: 'blue', height: '100%' }}>today-allDay</div>,
  41. },
  42. {
  43. start: time,
  44. children: <div style={{ backgroundColor: 'indigo', height: '100px' }}>today-now</div>,
  45. },
  46. {
  47. allDay: true,
  48. children: <div style={{ backgroundColor: 'green', height: '100%' }}>today-allDay2</div>,
  49. },
  50. {
  51. allDay: true,
  52. children: <div style={{ backgroundColor: 'blue', height: '100%' }}>today-allDay3</div>,
  53. },
  54. {
  55. allDay: true,
  56. children: <div style={{ backgroundColor: 'green', height: '100%' }}>today-allDay4</div>,
  57. },
  58. ];
  59. const weeklyEvents = [
  60. {
  61. allDay: true,
  62. start: new Date(2019, 6, 15, 8, 0, 0),
  63. children: <div style={{ backgroundColor: 'blue', height: '100%' }}>7-15-AllDayA</div>,
  64. },
  65. {
  66. start: new Date(2019, 6, 16, 8, 32, 0),
  67. children: (
  68. <div style={{ backgroundColor: 'indigo', height: '100%' }}>
  69. 7-16 8:32 here is a very long content just to see if the content will collapse or not not
  70. sure if this is long enough aaaaaaaaaaa
  71. </div>
  72. ),
  73. },
  74. {
  75. start: new Date(2019, 6, 16, 14, 30, 0),
  76. end: new Date(2019, 6, 16, 20, 0, 0),
  77. children: <div style={{ backgroundColor: 'indigo', height: '100%' }}>7-16 14:30-20:00</div>,
  78. },
  79. {
  80. start: new Date(2019, 6, 18, 14, 45, 0),
  81. end: new Date(2019, 6, 19, 6, 18, 0),
  82. children: (
  83. <div style={{ backgroundColor: 'indigo', height: '100%' }}>7-18 14:45 ~ 7-19 6:18</div>
  84. ),
  85. },
  86. ];
  87. // const events = [];
  88. const date = new Date(2019, 6, 18, 8, 0, 0);
  89. const dailyEventStyle = {
  90. borderRadius: '3px',
  91. boxSizing: 'border-box',
  92. border: 'var(--semi-color-primary) 1px solid',
  93. padding: '10px',
  94. backgroundColor: 'var(--semi-color-primary-light-default)',
  95. height: '100%',
  96. overflow: 'hidden',
  97. };
  98. const allDayStyle = {
  99. borderRadius: '3px',
  100. boxSizing: 'border-box',
  101. border: 'var(--semi-color-bg-1) 1px solid',
  102. marginRight: '12px',
  103. padding: '2px 4px',
  104. backgroundColor: 'var(--semi-color-primary-light-active)',
  105. height: '100%',
  106. overflow: 'hidden',
  107. };
  108. const weeklyEvents2 = [
  109. {
  110. allDay: true,
  111. start: new Date(2019, 6, 22, 8, 0, 0),
  112. children: <div style={{ backgroundColor: 'blue', height: '100%' }}>7-22-allDay</div>,
  113. },
  114. {
  115. start: new Date(2019, 6, 23, 8, 32, 0),
  116. children: <div style={{ backgroundColor: 'indigo', height: '100%' }}>7-23 8:32</div>,
  117. },
  118. {
  119. start: new Date(2019, 6, 23, 14, 30, 0),
  120. end: new Date(2019, 6, 23, 20, 0, 0),
  121. children: <div style={{ backgroundColor: 'indigo', height: '100%' }}>7-23 14:30-20:00</div>,
  122. },
  123. {
  124. start: new Date(2019, 6, 25, 14, 45, 0),
  125. end: new Date(2019, 6, 26, 6, 18, 0),
  126. children: (
  127. <div style={{ backgroundColor: 'indigo', height: '100%' }}>7-25 14:45 ~ 7-26 6:18</div>
  128. ),
  129. },
  130. {
  131. start: new Date(2019, 6, 25, 8, 0, 0),
  132. end: new Date(2019, 6, 27, 6, 0, 0),
  133. children: (
  134. <div style={{ backgroundColor: 'indigo', height: '100%' }}>7-25 8:00 ~ 7-27 6:00</div>
  135. ),
  136. },
  137. {
  138. start: new Date(2019, 6, 22, 9, 0, 0),
  139. end: new Date(2019, 6, 23, 23, 0, 0),
  140. children: (
  141. <div style={{ backgroundColor: 'LightSkyBlue', height: '100%' }}>7-22 9:00 ~ 7-23 23:00</div>
  142. ),
  143. },
  144. {
  145. start: new Date(2019, 6, 21, 6, 0, 0),
  146. end: new Date(2019, 6, 25, 6, 0, 0),
  147. children: (
  148. <div style={{ backgroundColor: 'YellowGreen', height: '100%' }}>7-21 6:00 ~ 7-25 6:00</div>
  149. ),
  150. },
  151. {
  152. start: new Date(2019, 6, 19, 20, 0, 0),
  153. end: new Date(2019, 6, 23, 14, 0, 0),
  154. children: (
  155. <div style={{ backgroundColor: 'pink', height: '100%' }}>7-19 20:00 ~ 7-23 14:00</div>
  156. ),
  157. },
  158. {
  159. start: new Date(2019, 6, 26, 10, 0, 0),
  160. end: new Date(2019, 6, 27, 16, 0, 0),
  161. children: <div style={{ backgroundColor: 'red', height: '100%' }}>7-26 10:00 ~ 7-27 16:00</div>,
  162. },
  163. {
  164. start: new Date(2019, 6, 18, 10, 0, 0),
  165. end: new Date(2019, 6, 30, 8, 0, 0),
  166. children: (
  167. <div style={{ backgroundColor: 'green', height: '100%' }}>7-18 10:00 ~ 7-30 8:00</div>
  168. ),
  169. },
  170. ];
  171. export const DayCalendar = () => {
  172. return (
  173. <div>
  174. <Calendar
  175. mode="day"
  176. events={events}
  177. // displayValue={new Date(2019, 6, 16, 8, 0, 0)}
  178. ></Calendar>
  179. </div>
  180. );
  181. };
  182. DayCalendar.parameters = {
  183. chromatic: { disableSnapshot: true },
  184. }
  185. export const WeekCalendar = () => {
  186. return (
  187. <div>
  188. <Calendar displayValue={date} events={weeklyEvents}></Calendar>
  189. </div>
  190. );
  191. };
  192. export const ThisWeekCalendar = () => {
  193. return (
  194. <div>
  195. <Calendar events={weeklyEvents2} displayValue={new Date(2019, 6, 22, 8, 0, 0)}></Calendar>
  196. </div>
  197. );
  198. };
  199. export const MonthCalendar = () => {
  200. return <Calendar events={[...weeklyEvents2, ...weeklyEvents]} mode="month"></Calendar>;
  201. };
  202. MonthCalendar.parameters = {
  203. chromatic: { disableSnapshot: true },
  204. }
  205. const AddEventToCalendar = () => {
  206. const [event, setEvent] = useState([]);
  207. const [mode, setMode] = useState('day');
  208. const addEvent = () => {
  209. let newEvent = {
  210. allDay: true,
  211. start: new Date(),
  212. key: `${id}`,
  213. children: <div style={allDayStyle}>today-{id}</div>,
  214. };
  215. id++;
  216. setEvent([...event, newEvent]);
  217. };
  218. const removeEvent = () => {
  219. let newEvents = [...event];
  220. newEvents.pop();
  221. setEvent([...newEvents]);
  222. };
  223. return (
  224. <>
  225. <Button onClick={addEvent}>add</Button>
  226. <Button onClick={removeEvent}>remove</Button>
  227. <RadioGroup onChange={e => setMode(e.target.value)} value={mode}>
  228. <Radio value={'day'}>日视图</Radio>
  229. <Radio value={'week'}>周视图</Radio>
  230. <Radio value={'month'}>月视图</Radio>
  231. </RadioGroup>
  232. <Calendar events={event} mode={mode} />
  233. </>
  234. );
  235. };
  236. export const UpdateEvent = () => {
  237. const [event, setEvent] = useState([]);
  238. const [mode, setMode] = useState('day');
  239. const [updateId, setId] = useState(id);
  240. const addEvent = () => {
  241. let key = `${id}`;
  242. let newEvent = {
  243. allDay: true,
  244. start: new Date(),
  245. key,
  246. children: (
  247. <div
  248. style={allDayStyle}
  249. onClick={() => {
  250. console.log(key);
  251. setId(key);
  252. }}
  253. >
  254. today-{key}
  255. </div>
  256. ),
  257. };
  258. id++;
  259. setEvent([...event, newEvent]);
  260. };
  261. const updateEvent = () => {
  262. let ind = event.findIndex(item => {
  263. return item.key === updateId;
  264. });
  265. let newArr = [...event];
  266. newArr[ind].key = `${Math.random()}`;
  267. newArr[ind].children = <div style={allDayStyle}>today-{Math.random(0, 1)}</div>;
  268. setEvent(newArr);
  269. };
  270. return (
  271. <>
  272. <Button onClick={addEvent}>add</Button>
  273. <Button onClick={updateEvent}>update</Button>
  274. <RadioGroup onChange={e => setMode(e.target.value)} value={mode}>
  275. <Radio value={'day'}>日视图</Radio>
  276. <Radio value={'week'}>周视图</Radio>
  277. <Radio value={'month'}>月视图</Radio>
  278. </RadioGroup>
  279. <Calendar events={event} mode={mode} />
  280. </>
  281. );
  282. };
  283. UpdateEvent.parameters = {
  284. chromatic: { disableSnapshot: true },
  285. }
  286. export const DateGridRenderWeek = () => {
  287. return (
  288. <div>
  289. <Calendar
  290. displayValue={date}
  291. events={weeklyEvents}
  292. dateGridRender={(dateString, date) => {
  293. if (dateString === new Date(2019, 6, 16).toString()) {
  294. return (
  295. <div style={{ backgroundColor: 'red', height: '100%', width: '100%' }}>123test</div>
  296. );
  297. }
  298. return null;
  299. }}
  300. ></Calendar>
  301. </div>
  302. );
  303. };
  304. export const DateGridRenderMonth = () => {
  305. return (
  306. <div>
  307. <Calendar
  308. mode="month"
  309. displayValue={date}
  310. events={weeklyEvents}
  311. dateGridRender={(dateString, date) => {
  312. console.log(dateString);
  313. if (dateString === new Date(2019, 6, 16).toString()) {
  314. return (
  315. <div style={{ backgroundColor: 'red', height: '100%', width: '100%' }}>123test</div>
  316. );
  317. }
  318. return null;
  319. }}
  320. ></Calendar>
  321. </div>
  322. );
  323. };
  324. export const RangeCalenderMonth = () => {
  325. return (
  326. <div>
  327. <Calendar
  328. mode={'range'}
  329. range={[new Date(2019, 6, 22), new Date(2019, 6, 25)]}
  330. events={weeklyEvents2}
  331. displayValue={new Date(2019, 6, 22)}
  332. />
  333. </div>
  334. );
  335. };
  336. export const WithLocaleProvider = () => {
  337. const [locale, setLocale] = useState(zh_CN);
  338. const onLanguageChange = code => {
  339. setLocale(language[code]);
  340. };
  341. return (
  342. <LocaleProvider>
  343. <div style={{ borderBottom: '1px solid var(--semi-color-border)', paddingBottom: 20 }}>
  344. <Select
  345. onChange={onLanguageChange}
  346. insetLabel="切换语言"
  347. style={{ width: 250 }}
  348. defaultValue="zh_CN"
  349. >
  350. <Option value="zh_CN">中文</Option>
  351. <Option value="en_GB">英语(英)</Option>
  352. <Option value="ja_JP">日语</Option>
  353. <Option value="ko_KR">韩语</Option>
  354. <Option value="ar">阿拉伯语</Option>
  355. <Option value="vi_VN">越南语</Option>
  356. <Option value="ru_RU">俄罗斯语</Option>
  357. <Option value="id_ID">印尼语</Option>
  358. <Option value="ms_MY">马来语</Option>
  359. <Option value="th_TH">泰语</Option>
  360. <Option value="tr_TR">土耳其语</Option>
  361. <Option value="pt_BR">葡萄牙语(巴西)</Option>
  362. </Select>
  363. </div>
  364. <LocaleProvider locale={locale}>
  365. <Calendar mode="day" />
  366. <br />
  367. <Calendar mode="week" />
  368. <br />
  369. <Calendar mode="month" />
  370. <br />
  371. </LocaleProvider>
  372. </LocaleProvider>
  373. );
  374. };
  375. WithLocaleProvider.parameters = {
  376. chromatic: { disableSnapshot: true }
  377. }