bottomSheet.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import {
  2. type ReactNode,
  3. Fragment
  4. } from "react";
  5. import {
  6. type BottomSheetContextType,
  7. type BottomSheetDataType
  8. } from "../types/bottomSheet";
  9. import NCoreContext, {
  10. type ConfigType
  11. } from "ncore-context";
  12. import BottomSheet from "../components/bottomSheet";
  13. import {
  14. uuid
  15. } from "../utils";
  16. class NCoreUIKitBottomSheet extends NCoreContext<BottomSheetContextType, ConfigType<BottomSheetContextType>> {
  17. constructor({
  18. data = []
  19. }: {
  20. data?: Array<BottomSheetDataType>
  21. }) {
  22. super({
  23. close: () => {},
  24. open: () => "",
  25. data: data
  26. }, {
  27. key: "NCoreUIKit-BottomSheetContext"
  28. });
  29. };
  30. open = (dialogData: BottomSheetDataType) => {
  31. const currentData = this.state.data;
  32. const dialogID = dialogData.id ? dialogData.id : uuid();
  33. currentData.push({
  34. ...dialogData,
  35. id: dialogID
  36. });
  37. this.setState({
  38. data: currentData
  39. });
  40. return dialogID;
  41. };
  42. close = (props?: {
  43. index?: number;
  44. id?: string;
  45. }) => {
  46. const currentData = this.state.data;
  47. if (props && props.id) {
  48. const keyIndex = currentData.findIndex((bottomSheet) => bottomSheet.id === props.id);
  49. if (keyIndex !== -1) {
  50. currentData.splice(keyIndex, 1);
  51. this.setState({
  52. data: currentData
  53. });
  54. }
  55. return;
  56. }
  57. if (props && props.index !== undefined) {
  58. currentData.splice(props.index, 1);
  59. this.setState({
  60. data: currentData
  61. });
  62. return;
  63. }
  64. currentData.pop();
  65. this.setState({
  66. data: currentData
  67. });
  68. };
  69. Render = ({
  70. children
  71. }: {
  72. children: ReactNode;
  73. }) => {
  74. const {
  75. data
  76. } = this.useContext();
  77. return <Fragment>
  78. {children}
  79. {data.map((item: BottomSheetDataType) => {
  80. return <BottomSheet
  81. key={`NCoreUIKit-BottomSheet-${item.id}`}
  82. id={item.id as string}
  83. isActive={true}
  84. onClosed={() => {
  85. if(item.onClosed) {
  86. item.onClosed({
  87. id: item.id as string
  88. });
  89. }
  90. if(item.isAutoClosed === undefined || item.isAutoClosed === true) {
  91. this.close({
  92. id: item.id
  93. });
  94. }
  95. }}
  96. {...item}
  97. />;
  98. })}
  99. </Fragment>;
  100. };
  101. }
  102. export default NCoreUIKitBottomSheet;