import Taro, { useState, useMemo, pxTransform } from "@tarojs/taro";
import classNames from "classnames";
import { IProps } from "../../../@types/actionSheet";
import ClCard from "../card";
import { generateId } from "../../lib";
import {
bgColorType,
bgColorMoreType,
lightBgColorType
} from "../../lib/types";
import ClText from "../text";
import { View } from "@tarojs/components";
import ClLayout from "../layout";
import "./index.scss";
import { BG_COLOR_LIST } from "../../lib/model";
export default function ClActionSheet(props: IProps) {
const {
type,
tip,
isOpened,
closeWithShadow,
options,
cancelText,
showCancel,
cancelBgColor,
cancelFontColor,
onClick,
onCancel
} = props;
const [actionsArr, setActionsArr] = useState([] as any[]);
const [show, setShow] = useState(false);
useMemo(() => {
const list = options || [];
setActionsArr(
list.map((item: any) => {
item.cu_action_sheet_id = generateId();
return item;
})
);
}, [options]);
useMemo(() => {
setShow(!!isOpened);
}, [isOpened]);
const click = (index: number) => {
onClick && onClick(index);
};
const cancelComponent = (
{
e.stopPropagation();
onCancel && onCancel();
}}
style={{
marginTop: pxTransform(20)
}}
className={classNames([
{
cu_action_sheet_card: type === "card"
}
])}
>
);
const tipComponent = (
{tip}
);
const actionsComponents = actionsArr.map(
(
item: {
cu_action_sheet_id: string;
text: string;
bgColor: bgColorType | bgColorMoreType | lightBgColorType;
},
index: number
) => (
{item.text}
)
);
return (
{
closeWithShadow && setShow(false);
onCancel && onCancel();
e.stopPropagation();
}}
>
{
e.stopPropagation();
}}
>
{tip ? tipComponent : ""}
{actionsComponents}
{showCancel ? cancelComponent : ""}
);
}
ClActionSheet.options = {
addGlobalClass: true
};
ClActionSheet.defaultProps = {
tip: "",
isOpened: false,
closeWithShadow: true,
actions: [],
cancelText: "取消",
showCancel: false,
cancelBgColor: "white",
cancelFontColor: undefined,
onClick: () => {},
onCancel: () => {}
} as IProps;