calendar.stories.jsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. }
  378. class EventRenderDemo extends React.Component {
  379. constructor() {
  380. super();
  381. this.state = {
  382. mode: 'week',
  383. clickDate: '?'
  384. };
  385. }
  386. onSelect(e) {
  387. this.setState({
  388. mode: e.target.value,
  389. });
  390. }
  391. render() {
  392. const { mode } = this.state;
  393. const isMonthView = mode === 'month';
  394. const dailyEventStyle = {
  395. borderRadius: '3px',
  396. boxSizing: 'border-box',
  397. border: 'var(--semi-color-primary) 1px solid',
  398. padding: '10px',
  399. backgroundColor: 'var(--semi-color-primary-light-default)',
  400. height: '100%',
  401. overflow: 'hidden',
  402. };
  403. const allDayStyle = {
  404. borderRadius: '3px',
  405. boxSizing: 'border-box',
  406. border: 'var(--semi-color-bg-1) 1px solid',
  407. padding: '2px 4px',
  408. backgroundColor: 'var(--semi-color-primary-light-active)',
  409. height: '100%',
  410. overflow: 'hidden',
  411. };
  412. const dailyStyle = isMonthView ? allDayStyle : dailyEventStyle;
  413. const events = [
  414. {
  415. key: '0',
  416. start: new Date(2019, 5, 25, 14, 45, 0),
  417. end: new Date(2019, 6, 26, 6, 18, 0),
  418. children: <div style={dailyStyle}>6月25日 14:45 ~ 7月26日 6:18</div>,
  419. },
  420. {
  421. key: '1',
  422. start: new Date(2019, 6, 18, 10, 0, 0),
  423. end: new Date(2019, 6, 30, 8, 0, 0),
  424. children: <div style={allDayStyle}>7月18日 10:00 ~ 7月30日 8:00</div>,
  425. },
  426. {
  427. key: '2',
  428. start: new Date(2019, 6, 19, 20, 0, 0),
  429. end: new Date(2019, 6, 23, 14, 0, 0),
  430. children: <div style={allDayStyle}>7月19日 20:00 ~ 7月23日 14:00</div>,
  431. },
  432. {
  433. key: '3',
  434. start: new Date(2019, 6, 21, 6, 0, 0),
  435. end: new Date(2019, 6, 25, 6, 0, 0),
  436. children: <div style={allDayStyle}>7月21日 6:00 ~ 7月25日 6:00</div>,
  437. },
  438. {
  439. key: '4',
  440. allDay: true,
  441. start: new Date(2019, 6, 22, 8, 0, 0),
  442. children: <div style={allDayStyle}>7月22日 全天</div>,
  443. },
  444. {
  445. key: '5',
  446. start: new Date(2019, 6, 22, 9, 0, 0),
  447. end: new Date(2019, 6, 23, 23, 0, 0),
  448. children: <div style={allDayStyle}>7月22日 9:00 ~ 7月23日 23:00</div>,
  449. },
  450. {
  451. key: '6',
  452. start: new Date(2019, 6, 23, 8, 32, 0),
  453. children: <div style={dailyStyle}>7月23日 8:32</div>,
  454. },
  455. {
  456. key: '7',
  457. start: new Date(2019, 6, 23, 14, 30, 0),
  458. end: new Date(2019, 6, 23, 20, 0, 0),
  459. children: <div style={dailyStyle}>7月23日 14:30-20:00</div>,
  460. },
  461. {
  462. key: '8',
  463. start: new Date(2019, 6, 25, 8, 0, 0),
  464. end: new Date(2019, 6, 27, 6, 0, 0),
  465. children: <div style={allDayStyle}>7月25日 8:00 ~ 7月27日 6:00</div>,
  466. },
  467. {
  468. key: '9',
  469. start: new Date(2019, 6, 26, 10, 0, 0),
  470. end: new Date(2019, 6, 27, 16, 0, 0),
  471. children: <div style={allDayStyle}>7月26日 10:00 ~ 7月27日 16:00</div>,
  472. },
  473. ];
  474. const displayValue = new Date(2019, 6, 23, 8, 32, 0);
  475. return (
  476. <>
  477. <RadioGroup onChange={e => this.onSelect(e)} value={mode}>
  478. <Radio value={'day'}>日视图</Radio>
  479. <Radio value={'week'}>周视图</Radio>
  480. <Radio value={'month'}>月视图</Radio>
  481. <Radio value={'range'}>多日视图</Radio>
  482. </RadioGroup>
  483. <br />
  484. <br />
  485. <Calendar
  486. height={400}
  487. mode={mode}
  488. displayValue={displayValue}
  489. events={events}
  490. onClick={(e, date) => { this.setState({clickDate: date.getDate()}); console.log(date.getDate())}}
  491. range={mode === 'range' ? [new Date(2019, 6, 23), new Date(2019, 6, 26)] : []}
  492. ></Calendar>
  493. <div>当前点击的日期是{this.state.clickDate}号</div>
  494. </>
  495. );
  496. }
  497. }
  498. export const EventRender = () => <EventRenderDemo />;
  499. export const WeekStartsOnDemo = () => {
  500. const [v, setV] = useState(6);
  501. const allDayStyle = {
  502. borderRadius: '3px',
  503. boxSizing: 'border-box',
  504. border: 'var(--semi-color-bg-1) 1px solid',
  505. padding: '2px 4px',
  506. backgroundColor: 'var(--semi-color-primary-light-active)',
  507. height: '100%',
  508. overflow: 'hidden',
  509. };
  510. const events = [
  511. {
  512. key: '0',
  513. start: new Date(2022, 8, 5, 14, 45, 0),
  514. end: new Date(2022, 8, 6, 6, 18, 0),
  515. children: <div style={allDayStyle}>9月5日 14:45 ~ 9月6日 6:18</div>,
  516. }
  517. ]
  518. return (
  519. <div>
  520. <RadioGroup defaultValue={v} aria-label="周起始日" name="demo-radio-group-vertical" onChange={e => setV(e.target.value)}>
  521. <Radio value={1}>周一</Radio>
  522. <Radio value={2}>周二</Radio>
  523. <Radio value={3}>周三</Radio>
  524. <Radio value={4}>周四</Radio>
  525. <Radio value={5}>周五</Radio>
  526. <Radio value={6}>周六</Radio>
  527. <Radio value={0}>周日</Radio>
  528. </RadioGroup>
  529. <Calendar
  530. mode="month"
  531. weekStartsOn={1}
  532. events={events}
  533. dateGridRender={(dateString, date) => {
  534. if (dateString === new Date(2019, 6, 16).toString()) {
  535. return (
  536. <div style={{ backgroundColor: 'red', height: '100%', width: '100%' }}>123test</div>
  537. );
  538. }
  539. return null;
  540. }}
  541. ></Calendar>
  542. </div>
  543. )
  544. }