import './button.scss'; import {computed, designComponent, getComponentCls, iHTMLButtonElement, mergeAttrs, onBeforeUnmount, PropType, reactive, StyleProperties, useClasses, useNumber, useRefs, useStyles, watch} from "@peryl/react-compose"; import {EditProps, useEdit} from "../../uses/useEdit"; import {StyleProps, ThemeMode, ThemeStatus, useStyle} from "../../uses/useStyle"; import {throttle} from "@peryl/utils/throttle"; import {unit} from "@peryl/utils/unit"; import {Icon} from "../Icon"; import {Loading} from "../Loading"; import {ButtonGroup} from "../ButtonGroup"; import {AutoLoadingObserver} from "../AutoLoadingObserver"; import {useCollapseStyles} from "../../uses/useCollapseStyles"; import {createEffects} from "@peryl/utils/createEffects"; import {useClickRipple} from "../../directives/ClickRipple/useClickRipple"; export const Button = designComponent({ name: '-button', inheritPropsType: iHTMLButtonElement, props: { mode: { type: String as PropType }, // 按钮类型:fill填充,flat扁平,outline线性以及text文字 label: { type: String }, // 按钮文本 width: { type: [String, Number] }, // 按钮宽度 icon: { type: String }, // 按钮图标 active: { type: Boolean }, // 按钮是否高亮 noPadding: { type: Boolean }, // 按钮是否无边距 block: { type: Boolean }, // 块级元素 throttleClick: { type: [Number, Boolean] }, // click节流 asyncHandler: { type: Function as PropType<(e: MouseEvent) => void> }, // 异步处理函数,会自动开启关闭loading状态 autoLoading: { type: Boolean, default: true, }, // 是否自动检测异步任务的执行并且开启loading ...EditProps, ...StyleProps, /*native*/ type: { type: String, default: 'button' }, nativeAttrs: { type: Object }, }, emits: { onClick: (e: MouseEvent) => true, }, slots: ['default'], setup({ props, event: { emit }, slots }) { const { refs, onRef } = useRefs({ el: HTMLButtonElement, }); const { numberState } = useNumber(props, ['width']); const { styleComputed } = useStyle(); const autoLoadingObserver = AutoLoadingObserver.useObserver(); const { editState, editComputed } = useEdit({ adjust: (ret) => { if (props.autoLoading && autoLoadingObserver.isLoading && !ret.loading) { ret.loading = true; } } }); const group = ButtonGroup.use.inject(null); const mode = computed(() => props.mode || group?.props.mode || styleComputed.value.default.buttonMode); const status = computed(() => styleComputed.value.status || (mode.value === ThemeMode.flat ? ThemeStatus.secondary : ThemeStatus.primary)); const state = reactive({ handleClick: null as ((e: MouseEvent) => void) | null, handleClickWrapper: (e: MouseEvent) => { props.autoLoading && autoLoadingObserver.tapStart(); state.handleClick?.(e); props.autoLoading && autoLoadingObserver.tapEnd(); }, handleClickInner: async (e: MouseEvent) => { // e.stopPropagation(); if (!editComputed.value.editable) { return; } if (!props.asyncHandler) { return emit.onClick(e); } editState.loading = true; try { await props.asyncHandler(e); } catch (e) { console.error(e); } finally { editState.loading = null; } }, }); const { effects: throttleClickEffects } = createEffects(); onBeforeUnmount(() => {throttleClickEffects.clear();}); watch( () => props.throttleClick, (val) => { throttleClickEffects.clear(); if (!val) { return state.handleClick = state.handleClickInner; } if (val === true) { val = 1000; } const throttleClickHandler = throttle(state.handleClickInner, val, { trailing: false }); throttleClickEffects.push(() => throttleClickHandler.dispose()); state.handleClick = throttleClickHandler; }, { immediate: true } ); const classes = useClasses(() => ([ getComponentCls('button'), 'plain-click-node', `button-mode-${mode.value}`, `button-status-${status.value}`, `button-shape-${styleComputed.value.shape}`, `button-size-${styleComputed.value.size}`, { 'button-icon': !!props.icon, 'button-active': !!props.active, 'button-no-padding': !!props.noPadding, 'button-loading': !!editComputed.value.loading, 'button-has-icon': !!props.icon, 'button-block': !!props.block, 'button-disabled': !!editComputed.value.disabled, 'button-readonly': !!editComputed.value.readonly, 'button-icon-only': buttonIconOnly.value, }, ])); const buttonIconOnly = computed(() => !!props.icon && !props.label); const styles = useStyles(style => { style.width = unit(numberState.width); }); useClickRipple({ elGetter: () => refs.el, optionsGetter: () => ({ size: () => styleComputed.value.size, disabled: !editComputed.value.editable || mode.value === 'text' }), }); const { loadingCollapse, iconCollapse } = (() => { const active = () => ({ height: '1em', width: '1em', transition: 'all 300ms ease', transformOrigin: 'center', paddingRight: !buttonIconOnly.value ? '4px' : '', transform: 'scale(1)' }); const inactive = (): StyleProperties => ({ height: '0', width: '0', transition: 'all 300ms ease', transformOrigin: 'center', paddingRight: !buttonIconOnly.value ? '0' : '', transform: 'scale(0)' }); /*没有icon的时候,单独处理loading,有icon的情况下,loading在icon中渲染*/ const loadingCollapse = useCollapseStyles({ isShow: () => !!editComputed.value.loading && !props.icon, active, inactive, }); /*有icon的时候一直显示icon*/ const iconCollapse = useCollapseStyles({ isShow: () => !!props.icon, active, inactive, }); return { loadingCollapse, iconCollapse }; })(); return { refer: { refs, }, render: () => { return ( ); } }; }, }); export default Button;