refactor: affix
parent
3b89f59094
commit
e9854dafd8
|
@ -1,7 +1,6 @@
|
||||||
import {
|
import {
|
||||||
CSSProperties,
|
CSSProperties,
|
||||||
defineComponent,
|
defineComponent,
|
||||||
inject,
|
|
||||||
ref,
|
ref,
|
||||||
reactive,
|
reactive,
|
||||||
watch,
|
watch,
|
||||||
|
@ -10,13 +9,13 @@ import {
|
||||||
computed,
|
computed,
|
||||||
onUnmounted,
|
onUnmounted,
|
||||||
onUpdated,
|
onUpdated,
|
||||||
|
ExtractPropTypes,
|
||||||
} from 'vue';
|
} from 'vue';
|
||||||
import PropTypes from '../_util/vue-types';
|
import PropTypes from '../_util/vue-types';
|
||||||
import classNames from '../_util/classNames';
|
import classNames from '../_util/classNames';
|
||||||
import omit from 'omit.js';
|
import omit from 'omit.js';
|
||||||
import ResizeObserver from '../vc-resize-observer';
|
import ResizeObserver from '../vc-resize-observer';
|
||||||
import throttleByAnimationFrame from '../_util/throttleByAnimationFrame';
|
import throttleByAnimationFrame from '../_util/throttleByAnimationFrame';
|
||||||
import { defaultConfigProvider } from '../config-provider';
|
|
||||||
import { withInstall } from '../_util/type';
|
import { withInstall } from '../_util/type';
|
||||||
import {
|
import {
|
||||||
addObserveTarget,
|
addObserveTarget,
|
||||||
|
@ -25,6 +24,7 @@ import {
|
||||||
getFixedTop,
|
getFixedTop,
|
||||||
getFixedBottom,
|
getFixedBottom,
|
||||||
} from './utils';
|
} from './utils';
|
||||||
|
import useConfigInject from '../_util/hooks/useConfigInject';
|
||||||
|
|
||||||
function getDefaultTarget() {
|
function getDefaultTarget() {
|
||||||
return typeof window !== 'undefined' ? window : null;
|
return typeof window !== 'undefined' ? window : null;
|
||||||
|
@ -42,7 +42,7 @@ export interface AffixState {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Affix
|
// Affix
|
||||||
const AffixProps = {
|
const affixProps = {
|
||||||
/**
|
/**
|
||||||
* 距离窗口顶部达到指定偏移量后触发
|
* 距离窗口顶部达到指定偏移量后触发
|
||||||
*/
|
*/
|
||||||
|
@ -58,12 +58,14 @@ const AffixProps = {
|
||||||
onChange: PropTypes.func,
|
onChange: PropTypes.func,
|
||||||
onTestUpdatePosition: PropTypes.func,
|
onTestUpdatePosition: PropTypes.func,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type AffixProps = Partial<ExtractPropTypes<typeof affixProps>>;
|
||||||
|
|
||||||
const Affix = defineComponent({
|
const Affix = defineComponent({
|
||||||
name: 'AAffix',
|
name: 'AAffix',
|
||||||
props: AffixProps,
|
props: affixProps,
|
||||||
emits: ['change', 'testUpdatePosition'],
|
emits: ['change', 'testUpdatePosition'],
|
||||||
setup(props, { slots, emit, expose }) {
|
setup(props, { slots, emit, expose }) {
|
||||||
const configProvider = inject('configProvider', defaultConfigProvider);
|
|
||||||
const placeholderNode = ref();
|
const placeholderNode = ref();
|
||||||
const fixedNode = ref();
|
const fixedNode = ref();
|
||||||
const state = reactive({
|
const state = reactive({
|
||||||
|
@ -218,12 +220,12 @@ const Affix = defineComponent({
|
||||||
(lazyUpdatePosition as any).cancel();
|
(lazyUpdatePosition as any).cancel();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const { prefixCls } = useConfigInject('affix', props);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
const { prefixCls } = props;
|
|
||||||
const { affixStyle, placeholderStyle } = state;
|
const { affixStyle, placeholderStyle } = state;
|
||||||
const { getPrefixCls } = configProvider;
|
|
||||||
const className = classNames({
|
const className = classNames({
|
||||||
[getPrefixCls('affix', prefixCls)]: affixStyle,
|
[prefixCls.value]: affixStyle,
|
||||||
});
|
});
|
||||||
const restProps = omit(props, ['prefixCls', 'offsetTop', 'offsetBottom', 'target']);
|
const restProps = omit(props, ['prefixCls', 'offsetTop', 'offsetBottom', 'target']);
|
||||||
return (
|
return (
|
||||||
|
|
Loading…
Reference in New Issue