OpenTime.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. import React from "react";
  2. import PropTypes from "prop-types";
  3. import {
  4. StyleSheet,
  5. TouchableOpacity,
  6. LayoutAnimation,
  7. Platform,
  8. UIManager,
  9. } from "react-native";
  10. import { range, convert2Digit } from "beeshell/dist/common/utils";
  11. import {
  12. CheckBox,
  13. Layout,
  14. Modal,
  15. Button,
  16. Card,
  17. Text,
  18. MenuItem,
  19. Icon,
  20. SelectItem,
  21. } from "@ui-kitten/components";
  22. import { Scrollpicker } from "beeshell";
  23. import { useModel } from "flooks";
  24. import moment from "moment";
  25. if (Platform.OS === "android") {
  26. if (UIManager.setLayoutAnimationEnabledExperimental) {
  27. UIManager.setLayoutAnimationEnabledExperimental(true);
  28. }
  29. }
  30. const ForwardIcon = (props) => <Icon {...props} name='arrow-ios-forward' />;
  31. const titleText = (props, title, value) => (
  32. <Text {...props} category='s1' numberOfLines={1} ellipsizeMode='tail'>
  33. {title}
  34. <Text category='c1' style={{ paddingLeft: 10 }}>
  35. {value}
  36. </Text>
  37. </Text>
  38. );
  39. export default function OpenTime({
  40. open,
  41. submit,
  42. cancelEvent,
  43. startTime,
  44. endTime,
  45. week,
  46. openModal,
  47. defaultValue,
  48. }) {
  49. const {
  50. MON,
  51. TUE,
  52. WED,
  53. THU,
  54. FRI,
  55. SAT,
  56. SUN,
  57. every,
  58. confirm,
  59. cancel,
  60. start,
  61. end,
  62. hour,
  63. min,
  64. weekName,
  65. } = useModel("wordsModel");
  66. const weeks = [
  67. {
  68. key: "MON",
  69. label: MON,
  70. },
  71. {
  72. key: "TUE",
  73. label: TUE,
  74. },
  75. {
  76. key: "WED",
  77. label: WED,
  78. },
  79. {
  80. key: "THU",
  81. label: THU,
  82. },
  83. {
  84. key: "FRI",
  85. label: FRI,
  86. },
  87. {
  88. key: "SAT",
  89. label: SAT,
  90. },
  91. {
  92. key: "SUN",
  93. label: SUN,
  94. },
  95. ];
  96. const [checkList, changeChecked] = React.useState([]);
  97. const listItems = weeks.map((item, index) => (
  98. <CheckBox
  99. style={styles.checkBoxItem}
  100. key={index}
  101. checked={checkList.includes(item.key)}
  102. onChange={(checked) => {
  103. console.log(checked);
  104. let _checkList = [...checkList];
  105. if (checked) {
  106. _checkList.push(item.key);
  107. } else {
  108. _checkList.splice(_checkList.indexOf(item.key), 1);
  109. }
  110. changeChecked(_checkList);
  111. }}
  112. >
  113. {item.label}
  114. </CheckBox>
  115. ));
  116. const list = React.useMemo(() => {
  117. let hourSum = 24;
  118. let minuteSum = 60;
  119. let secondSum = 60;
  120. let hours = range(hourSum / 1).map((item) => {
  121. item = convert2Digit(item * 1);
  122. return {
  123. label: `${item} ${hour}`,
  124. value: item,
  125. };
  126. });
  127. let minutes = range(minuteSum / 10).map((item) => {
  128. item = convert2Digit(item * 10);
  129. return {
  130. label: `${item} ${min}`,
  131. value: item,
  132. };
  133. });
  134. return [hours, minutes];
  135. }, [open]);
  136. const [time1, setTime1] = React.useState([8, 0]);
  137. const [time2, setTime2] = React.useState([20, 0]);
  138. const [showTime1, changeShowTime1] = React.useState(true);
  139. const [showTime2, changeShowTime2] = React.useState(false);
  140. const [showWeek, changeShowWeek] = React.useState(false);
  141. const allChecked = React.useMemo(() => {
  142. if (weeks.length == checkList.length) {
  143. return true;
  144. } else {
  145. return false;
  146. }
  147. }, [weeks, checkList]);
  148. const indeterminate = React.useMemo(() => {
  149. if (!allChecked && checkList.length > 0) {
  150. return true;
  151. } else {
  152. return false;
  153. }
  154. }, [allChecked, checkList]);
  155. const canSubmit = React.useMemo(() => {
  156. if (time1.length == 2 && time2.length == 2 && checkList.length > 0) {
  157. return true;
  158. } else {
  159. return false;
  160. }
  161. }, [time1, time2, checkList]);
  162. const time1Str = React.useMemo(() => {
  163. if (time1.length == 2) {
  164. let str = time1[0] + ":" + time1[1] * 10;
  165. return moment(str, "H:m").format("HH:mm");
  166. } else {
  167. return "";
  168. }
  169. }, [time1]);
  170. const time2Str = React.useMemo(() => {
  171. if (time2.length == 2) {
  172. let str = time2[0] + ":" + time2[1] * 10;
  173. return moment(str, "H:m").format("HH:mm");
  174. } else {
  175. return "";
  176. }
  177. }, [time2]);
  178. const weekStr = React.useMemo(() => {
  179. if (allChecked) {
  180. return every;
  181. } else {
  182. let _weeks = weeks.filter((item) => {
  183. return checkList.includes(item.key);
  184. });
  185. return _weeks
  186. .map((item) => {
  187. return item.label;
  188. })
  189. .join(",");
  190. }
  191. }, []);
  192. React.useEffect(() => {
  193. if (defaultValue && defaultValue.length > 0) {
  194. setShowName(defaultValue);
  195. }
  196. }, [defaultValue]);
  197. function hideElseShow(key) {
  198. changeShowTime1(key === 1);
  199. changeShowTime2(key === 2);
  200. changeShowWeek(key === 3);
  201. LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
  202. }
  203. const [showName, setShowName] = React.useState([" ", ""]);
  204. return (
  205. <>
  206. <SelectItem
  207. appearance='form'
  208. style={{ flex: 1, height: 32 }}
  209. accessoryRight={ForwardIcon}
  210. title={(props) => titleText(props, showName[0], showName[1])}
  211. onPress={openModal}
  212. />
  213. <Modal
  214. visible={open && list.length > 0}
  215. backdropStyle={styles.backdrop}
  216. onBackdropPress={cancelEvent}
  217. >
  218. <Card
  219. disabled={true}
  220. appearance='modalCrad'
  221. style={{ width: 242 }}
  222. >
  223. <Layout>
  224. <MenuItem
  225. appearance='cardContent'
  226. title={(props) => titleText(props, start, time1Str)}
  227. accessoryRight={ForwardIcon}
  228. onPress={() => {
  229. hideElseShow(1);
  230. }}
  231. />
  232. </Layout>
  233. <Card
  234. disabled={true}
  235. appearance='modalCrad'
  236. style={[
  237. { width: 222 },
  238. showTime1 ? {} : styles.hideHeight,
  239. ]}
  240. >
  241. <Scrollpicker
  242. key='time1'
  243. list={list}
  244. proportion={[1, 1]}
  245. offsetCount={1}
  246. value={time1}
  247. onChange={(index, value) => {
  248. let _time1 = [...time1];
  249. _time1[index] = value;
  250. setTime1(_time1);
  251. }}
  252. />
  253. </Card>
  254. <Layout>
  255. <MenuItem
  256. appearance='cardContent'
  257. title={(props) => titleText(props, end, time2Str)}
  258. accessoryRight={ForwardIcon}
  259. onPress={() => {
  260. hideElseShow(2);
  261. }}
  262. />
  263. </Layout>
  264. <Card
  265. disabled={true}
  266. appearance='modalCrad'
  267. style={[
  268. { width: 222 },
  269. showTime2 ? {} : styles.hideHeight,
  270. ]}
  271. >
  272. <Scrollpicker
  273. key='time2'
  274. list={list}
  275. proportion={[1, 1]}
  276. offsetCount={1}
  277. value={time2}
  278. onChange={(index, value) => {
  279. let _time2 = [...time2];
  280. _time2[index] = value;
  281. setTime2(_time2);
  282. }}
  283. />
  284. </Card>
  285. <Layout>
  286. <MenuItem
  287. appearance='cardContent'
  288. title={(props) =>
  289. titleText(props, weekName, weekStr)
  290. }
  291. accessoryRight={ForwardIcon}
  292. onPress={() => {
  293. hideElseShow(3);
  294. }}
  295. />
  296. </Layout>
  297. <Card
  298. appearance='modalCrad'
  299. style={[showWeek ? {} : styles.hideHeight]}
  300. >
  301. <CheckBox
  302. style={styles.checkBoxAll}
  303. checked={allChecked}
  304. indeterminate={indeterminate}
  305. onChange={(checked) => {
  306. changeChecked(
  307. checked
  308. ? weeks.map((item) => {
  309. return item.key;
  310. })
  311. : []
  312. );
  313. }}
  314. >
  315. {every}
  316. </CheckBox>
  317. {listItems}
  318. </Card>
  319. <Layout style={styles.btnList}>
  320. <Button
  321. style={styles.btn}
  322. appearance='outline'
  323. status='info'
  324. onPress={cancelEvent}
  325. >
  326. {cancel}
  327. </Button>
  328. <Button
  329. style={[styles.btn, { marginLeft: 10 }]}
  330. disabled={!canSubmit}
  331. onPress={() => {
  332. submit(time1Str, time2Str, checkList);
  333. setShowName([
  334. time1Str + "~" + time2Str,
  335. weekStr,
  336. ]);
  337. }}
  338. >
  339. {confirm}
  340. </Button>
  341. </Layout>
  342. </Card>
  343. </Modal>
  344. </>
  345. );
  346. }
  347. const styles = StyleSheet.create({
  348. checkBoxAll: {
  349. paddingVertical: 10,
  350. },
  351. checkBoxItem: {
  352. paddingVertical: 2,
  353. paddingHorizontal: 10,
  354. },
  355. time: {
  356. // width: 100,
  357. // height: 300,
  358. },
  359. backdrop: {
  360. backgroundColor: "rgba(0, 0, 0, 0.5)",
  361. },
  362. btnList: {
  363. flexDirection: "row",
  364. justifyContent: "space-between",
  365. paddingTop: 15,
  366. },
  367. hideHeight: {
  368. height: 0,
  369. borderWidth: 0,
  370. },
  371. btn: {
  372. flex: 1,
  373. minWidth: 50,
  374. },
  375. });