chore(Tab): using Vue built-in stringifyStyle (#11914)

* chore(Tab): using Vue built-in stringifyStyle

* fix(Tab): failed to normalize style
This commit is contained in:
neverland 2023-05-28 10:31:34 +08:00 committed by GitHub
parent 53a108ec7e
commit b2d49a35d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 24 deletions

View File

@ -46,7 +46,8 @@
], ],
"dependencies": { "dependencies": {
"@vant/popperjs": "^1.3.0", "@vant/popperjs": "^1.3.0",
"@vant/use": "^1.5.1" "@vant/use": "^1.5.1",
"@vue/shared": "^3.0.0"
}, },
"peerDependencies": { "peerDependencies": {
"vue": "^3.0.0" "vue": "^3.0.0"

View File

@ -4,14 +4,17 @@ import {
provide, provide,
computed, computed,
nextTick, nextTick,
defineComponent, watchEffect,
getCurrentInstance,
normalizeClass, normalizeClass,
normalizeStyle, normalizeStyle,
defineComponent,
getCurrentInstance,
type PropType, type PropType,
type CSSProperties, type CSSProperties,
type ExtractPropTypes, type ExtractPropTypes,
} from 'vue'; } from 'vue';
// eslint-disable-next-line vue/prefer-import-from-vue
import { stringifyStyle } from '@vue/shared';
// Utils // Utils
import { import {
@ -22,7 +25,6 @@ import {
numericProp, numericProp,
createNamespace, createNamespace,
ComponentInstance, ComponentInstance,
isObject,
} from '../utils'; } from '../utils';
import { TABS_KEY } from '../tabs/Tabs'; import { TABS_KEY } from '../tabs/Tabs';
@ -93,23 +95,16 @@ export default defineComponent({
}); });
// see: https://github.com/vant-ui/vant/issues/11763 // see: https://github.com/vant-ui/vant/issues/11763
const parserClass = ref(''); const parsedClass = ref('');
const parserStyle = ref(''); const parsedStyle = ref<string | undefined>('');
watch( watchEffect(() => {
[() => props.titleClass, () => props.titleStyle], const { titleClass, titleStyle } = props;
([newClass, newStyle]) => { parsedClass.value = titleClass ? normalizeClass(titleClass) : '';
parserClass.value = normalizeClass(newClass); parsedStyle.value =
const style = normalizeStyle(newStyle); titleStyle && typeof titleStyle !== 'string'
if (isObject(style)) { ? stringifyStyle(normalizeStyle(titleStyle))
parserStyle.value = Object.entries(style) : titleStyle;
.map(([key, val]) => `${key}:${val}; `) });
.join('');
} else {
parserStyle.value = style ?? '';
}
},
{ immediate: true }
);
const renderTitle = ( const renderTitle = (
onClickTab: ( onClickTab: (
@ -123,8 +118,8 @@ export default defineComponent({
v-slots={{ title: slots.title }} v-slots={{ title: slots.title }}
id={`${parent.id}-${index.value}`} id={`${parent.id}-${index.value}`}
ref={parent.setTitleRefs(index.value)} ref={parent.setTitleRefs(index.value)}
style={parserStyle.value} style={parsedStyle.value}
class={parserClass.value} class={parsedClass.value}
isActive={active.value} isActive={active.value}
controls={id} controls={id}
scrollable={parent.scrollable.value} scrollable={parent.scrollable.value}

View File

@ -189,13 +189,18 @@ test('should change title style when using title-style prop', async () => {
<Tab title="title1" titleStyle="color: red;"> <Tab title="title1" titleStyle="color: red;">
Text Text
</Tab> </Tab>
<Tab title="title1" titleStyle={{ color: 'blue' }}>
Text
</Tab>
</Tabs> </Tabs>
); );
}, },
}); });
await later(); await later();
expect(wrapper.find('.van-tab').style.color).toEqual('red'); const tabs = wrapper.findAll('.van-tab');
expect(tabs.at(0)!.style.color).toEqual('red');
expect(tabs.at(1)!.style.color).toEqual('blue');
}); });
test('should allot to hide bottom border by border prop', async () => { test('should allot to hide bottom border by border prop', async () => {

3
pnpm-lock.yaml generated
View File

@ -62,6 +62,9 @@ importers:
'@vant/use': '@vant/use':
specifier: ^1.5.1 specifier: ^1.5.1
version: link:../vant-use version: link:../vant-use
'@vue/shared':
specifier: ^3.0.0
version: 3.3.4
devDependencies: devDependencies:
'@types/jest': '@types/jest':
specifier: ^29.5.1 specifier: ^29.5.1