/** * @file 组件 drawer * @author baozhixin * @borrows https://ant.design/components/drawer-cn/ * * @todo API: destroyOnClose * @todo API: getContainer */ import './style/index.less'; import * as I from './interface'; import Base from 'santd/base'; import {classCreator} from '../core/util'; import KeyCode from '../core/util/keyCode'; import isNumber from 'lodash/isNumber'; import Icon from '../icon'; import filters from '../modal/Dialog'; const prefixCls = classCreator('drawer')(); export default class Drawer extends Base { static template = /* html */ `
{{title}}
`; static components = { 's-icon': Icon } static computed: I.Computed = { wrapStyle(this: Drawer) { const placement = this.data.get('placement'); const isHorizontal = placement === 'left' || placement === 'right'; const placementName = `translate${isHorizontal ? 'X' : 'Y'}`; const placementPos = placement === 'left' || placement === 'top' ? '-100%' : '100%'; const width = isHorizontal && this.data.get('width'); const height = !isHorizontal && this.data.get('height'); const transform = this.data.get('visible') ? '' : `${placementName}(${placementPos})`; return { transform, msTransform: transform, width: isNumber(width) ? `${width}px` : width, height: isNumber(height) ? `${height}px` : height }; } } filter = filters; initData(): I.State { return { closable: true, destroyOnClose: false, getContainer: 'body', mask: true, maskClosable: true, maskStyle: {}, width: 256, height: 256, placement: 'right', keyboard: true, showCloseBtn: false }; } inited(): void { this.watch('visible', val => { if (val) { this.nextTick(() => { // wrapper内的元素聚焦之后,wrapper才能生效keydown事件监听 const sentinel = this.ref('sentinel') as unknown as HTMLElement; !(this.slot('closeIcon')[0] && (this.slot('closeIcon')[0] as unknown as HTMLElement).children.length) && this.data.set('showCloseBtn', true); sentinel && sentinel.focus(); }); } // 切换抽屉时动画结束后的回调 setTimeout(() => { this.fire('afterVisibleChange', val); }, 300); }); } onMaskClick(e: MouseEvent): void { if (!this.data.get('maskClosable')) { return; } this.close(e); } close(e: KeyboardEvent | MouseEvent): void { if (this.data.get('visible') !== undefined) { this.fire('close', e); } this.data.set('visible', false); } onKeyDown(e: KeyboardEvent): void { if (this.data.get('keyboard') && e.keyCode === KeyCode.ESC) { e.stopPropagation(); this.close(e); } } };