Skip to content

Commit

Permalink
feat: init action-sheet component
Browse files Browse the repository at this point in the history
  • Loading branch information
taoyage committed Jul 22, 2022
1 parent c5eae24 commit 989df2e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions packages/action-sheet/actionSheet.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import React from 'react';
import Popup from '@/popup';

export interface Action {
key: string | number;
name: React.ReactNode;
description?: React.ReactNode;
disabled?: boolean;
color?: string;
onClick?: () => void;
}

export interface ActionSheetProps {
visible?: boolean;
title?: React.ReactNode;
actions: Action[];
onClose?: () => void;
onAction?: (action: Action, index: number) => void;
/** 是否点击action后触发onClose回调 */
closeOnAction?: boolean;
}

const classPrefix = `ygm-action-sheet`;

const ActionSheet: React.FC<ActionSheetProps> = React.memo((props) => {
const onClose = React.useCallback(() => {
props.onClose?.();
}, [props.onClose]);

const renderAction = React.useCallback(
(action: Action, index: number) => {
const onClick = () => {
action.onClick?.();
props.onAction?.(action, index);

if (props.closeOnAction) {
props.onClose?.();
}
};

return (
<button key={action.key} className={`${classPrefix}-action-item`} onClick={onClick}>
<div className={`${classPrefix}-action-item-name`}>{action.name}</div>
{action.description && <div className={`${classPrefix}-action-item-desc`}>{action.description}</div>}
</button>
);
},
[props.onAction, props.onClose]
);

return (
<Popup position="bottom" visible={props.visible!} onMaskClick={onClose}>
<div className={classPrefix}>
{props.title && <div className={`${classPrefix}-title`}>{props.title}</div>}
<div className={`${classPrefix}-action-list`}>{props.actions.map(renderAction)}</div>
</div>
</Popup>
);
});

ActionSheet.defaultProps = {
visible: false,
};

export default ActionSheet;
Empty file added packages/action-sheet/index.tsx
Empty file.
Empty file.
Empty file.

0 comments on commit 989df2e

Please sign in to comment.