2021-06-26 01:35:40 +00:00
|
|
|
import type { CSSProperties, HTMLAttributes, PropType } from 'vue';
|
|
|
|
import { computed, defineComponent, onUnmounted, ref } from 'vue';
|
2021-06-07 09:35:03 +00:00
|
|
|
import ResizeObserver from '../vc-resize-observer';
|
|
|
|
import classNames from '../_util/classNames';
|
2021-06-26 01:35:40 +00:00
|
|
|
import type { Key, VueNode } from '../_util/type';
|
2021-06-07 09:35:03 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
|
|
|
|
const UNDEFINED = undefined;
|
|
|
|
|
|
|
|
export default defineComponent({
|
2022-09-26 13:33:41 +00:00
|
|
|
compatConfig: { MODE: 3 },
|
2021-06-07 09:35:03 +00:00
|
|
|
name: 'Item',
|
|
|
|
props: {
|
|
|
|
prefixCls: String,
|
|
|
|
item: PropTypes.any,
|
|
|
|
renderItem: Function as PropType<(item: any) => VueNode>,
|
|
|
|
responsive: Boolean,
|
2021-09-06 14:47:56 +00:00
|
|
|
itemKey: { type: [String, Number] as PropType<string | number> },
|
2021-06-07 09:35:03 +00:00
|
|
|
registerSize: Function as PropType<(key: Key, width: number | null) => void>,
|
|
|
|
display: Boolean,
|
|
|
|
order: Number,
|
|
|
|
component: PropTypes.any,
|
|
|
|
invalidate: Boolean,
|
|
|
|
},
|
|
|
|
setup(props, { slots, expose }) {
|
|
|
|
const mergedHidden = computed(() => props.responsive && !props.display);
|
|
|
|
const itemNodeRef = ref();
|
|
|
|
|
|
|
|
expose({ itemNodeRef });
|
|
|
|
|
|
|
|
// ================================ Effect ================================
|
|
|
|
function internalRegisterSize(width: number | null) {
|
2021-09-06 14:47:56 +00:00
|
|
|
props.registerSize(props.itemKey, width);
|
2021-06-07 09:35:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onUnmounted(() => {
|
|
|
|
internalRegisterSize(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
const {
|
|
|
|
prefixCls,
|
|
|
|
invalidate,
|
|
|
|
item,
|
|
|
|
renderItem,
|
|
|
|
responsive,
|
|
|
|
registerSize,
|
|
|
|
itemKey,
|
|
|
|
display,
|
|
|
|
order,
|
|
|
|
component: Component = 'div',
|
|
|
|
...restProps
|
|
|
|
} = props;
|
|
|
|
const children = slots.default?.();
|
|
|
|
// ================================ Render ================================
|
|
|
|
const childNode = renderItem && item !== UNDEFINED ? renderItem(item) : children;
|
|
|
|
|
|
|
|
let overflowStyle: CSSProperties | undefined;
|
|
|
|
if (!invalidate) {
|
|
|
|
overflowStyle = {
|
|
|
|
opacity: mergedHidden.value ? 0 : 1,
|
|
|
|
height: mergedHidden.value ? 0 : UNDEFINED,
|
|
|
|
overflowY: mergedHidden.value ? 'hidden' : UNDEFINED,
|
|
|
|
order: responsive ? order : UNDEFINED,
|
|
|
|
pointerEvents: mergedHidden.value ? 'none' : UNDEFINED,
|
|
|
|
position: mergedHidden.value ? 'absolute' : UNDEFINED,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const overflowProps: HTMLAttributes = {};
|
|
|
|
if (mergedHidden.value) {
|
|
|
|
overflowProps['aria-hidden'] = true;
|
|
|
|
}
|
|
|
|
|
2021-06-22 02:47:33 +00:00
|
|
|
// ä˝żç¨ disabled éżĺ
çťćä¸ä¸č´ 察č´ĺçťäťś rerender
|
|
|
|
return (
|
|
|
|
<ResizeObserver
|
|
|
|
disabled={!responsive}
|
|
|
|
onResize={({ offsetWidth }) => {
|
|
|
|
internalRegisterSize(offsetWidth);
|
|
|
|
}}
|
2021-09-25 08:51:32 +00:00
|
|
|
v-slots={{
|
|
|
|
default: () => (
|
|
|
|
<Component
|
|
|
|
class={classNames(!invalidate && prefixCls)}
|
|
|
|
style={overflowStyle}
|
|
|
|
{...overflowProps}
|
|
|
|
{...restProps}
|
|
|
|
ref={itemNodeRef}
|
|
|
|
>
|
|
|
|
{childNode}
|
|
|
|
</Component>
|
|
|
|
),
|
|
|
|
}}
|
|
|
|
></ResizeObserver>
|
2021-06-22 02:47:33 +00:00
|
|
|
);
|
2021-06-07 09:35:03 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|