types(Sidebar): use tsx (#8121)

This commit is contained in:
neverland 2021-02-10 11:14:50 +08:00 committed by GitHub
parent 3d0e6d661e
commit 692631968d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 43 additions and 33 deletions

View File

@ -60,7 +60,7 @@
"@vant/icons": "^1.5.2", "@vant/icons": "^1.5.2",
"@vant/lazyload": "^1.0.2", "@vant/lazyload": "^1.0.2",
"@vant/popperjs": "^1.0.2", "@vant/popperjs": "^1.0.2",
"@vant/use": "^1.0.2" "@vant/use": "^1.0.3"
}, },
"peerDependencies": { "peerDependencies": {
"vue": "^3.0.0" "vue": "^3.0.0"

View File

@ -29,14 +29,14 @@ export default createComponent({
const isFirst = computed(() => { const isFirst = computed(() => {
if (parent) { if (parent) {
const prev = parent.children[index!.value - 1]; const prev = parent.children[index.value - 1];
return !(prev && 'isButton' in prev); return !(prev && 'isButton' in prev);
} }
}); });
const isLast = computed(() => { const isLast = computed(() => {
if (parent) { if (parent) {
const next = parent.children[index!.value + 1]; const next = parent.children[index.value + 1];
return !(next && 'isButton' in next); return !(next && 'isButton' in next);
} }
}); });

View File

@ -28,8 +28,8 @@ export default createComponent({
const { spaces } = parent; const { spaces } = parent;
if (spaces && spaces.value && spaces.value[index!.value]) { if (spaces && spaces.value && spaces.value[index.value]) {
const { left, right } = spaces.value[index!.value]; const { left, right } = spaces.value[index.value];
return { return {
paddingLeft: left ? `${left}px` : null, paddingLeft: left ? `${left}px` : null,
paddingRight: right ? `${right}px` : null, paddingRight: right ? `${right}px` : null,

View File

@ -1,7 +1,7 @@
import { createNamespace } from '../utils'; import { createNamespace } from '../utils';
import { useParent } from '@vant/use'; import { useParent } from '@vant/use';
import { useRoute, routeProps } from '../composables/use-route'; import { useRoute, routeProps } from '../composables/use-route';
import { SIDEBAR_KEY } from '../sidebar'; import { SIDEBAR_KEY, SidebarProvide } from '../sidebar';
import Badge from '../badge'; import Badge from '../badge';
const [createComponent, bem] = createNamespace('sidebar-item'); const [createComponent, bem] = createNamespace('sidebar-item');
@ -19,7 +19,16 @@ export default createComponent({
setup(props, { emit, slots }) { setup(props, { emit, slots }) {
const route = useRoute(); const route = useRoute();
const { parent, index } = useParent(SIDEBAR_KEY); const { parent, index } = useParent<SidebarProvide>(SIDEBAR_KEY);
if (!parent) {
if (process.env.NODE_ENV !== 'production') {
console.error(
'[Vant] SidebarItem must be a child component of Sidebar.'
);
}
return;
}
const onClick = () => { const onClick = () => {
if (props.disabled) { if (props.disabled) {
@ -27,14 +36,13 @@ export default createComponent({
} }
emit('click', index.value); emit('click', index.value);
parent.emit('update:modelValue', index.value);
parent.setActive(index.value); parent.setActive(index.value);
route(); route();
}; };
return () => { return () => {
const { dot, badge, title, disabled } = props; const { dot, badge, title, disabled } = props;
const selected = index.value === parent.active(); const selected = index.value === parent.getActive();
return ( return (
<a class={bem({ select: selected, disabled })} onClick={onClick}> <a class={bem({ select: selected, disabled })} onClick={onClick}>

View File

@ -90,9 +90,9 @@ export default {
### Sidebar Events ### Sidebar Events
| Event | Description | Arguments | | Event | Description | Arguments |
| ------ | -------------------------------- | ---------------------------- | | ------ | -------------------------------- | --------------- |
| change | Emitted when chosen item changed | index: index of current item | | change | Emitted when chosen item changed | _index: number_ |
### SidebarItem Props ### SidebarItem Props
@ -108,9 +108,9 @@ export default {
### SidebarItem Events ### SidebarItem Events
| Event | Description | Arguments | | Event | Description | Arguments |
| ----- | ------------------------------- | ---------------------------- | | ----- | ------------------------------- | --------------- |
| click | Emitted when an item is clicked | index: index of current item | | click | Emitted when an item is clicked | _index: number_ |
### SidebarItem Slots ### SidebarItem Slots

View File

@ -98,9 +98,9 @@ export default {
### Sidebar Events ### Sidebar Events
| 事件名 | 说明 | 回调参数 | | 事件名 | 说明 | 回调参数 |
| ------ | ---------------- | ----------------------- | | ------ | ---------------- | --------------- |
| change | 切换导航项时触发 | index: 当前导航项的索引 | | change | 切换导航项时触发 | _index: number_ |
### SidebarItem Props ### SidebarItem Props
@ -116,9 +116,9 @@ export default {
### SidebarItem Events ### SidebarItem Events
| 事件名 | 说明 | 回调参数 | | 事件名 | 说明 | 回调参数 |
| ------ | ---------- | ----------------------- | | ------ | ---------- | --------------- |
| click | 点击时触发 | index: 当前导航项的索引 | | click | 点击时触发 | _index: number_ |
### SidebarItem Slots ### SidebarItem Slots

View File

@ -1,4 +1,3 @@
import { watch } from 'vue';
import { createNamespace } from '../utils'; import { createNamespace } from '../utils';
import { useChildren } from '@vant/use'; import { useChildren } from '@vant/use';
@ -6,6 +5,11 @@ const [createComponent, bem] = createNamespace('sidebar');
export const SIDEBAR_KEY = 'vanSidebar'; export const SIDEBAR_KEY = 'vanSidebar';
export type SidebarProvide = {
getActive: () => number;
setActive: (value: number) => void;
};
export default createComponent({ export default createComponent({
props: { props: {
modelValue: { modelValue: {
@ -19,19 +23,17 @@ export default createComponent({
setup(props, { emit, slots }) { setup(props, { emit, slots }) {
const { linkChildren } = useChildren(SIDEBAR_KEY); const { linkChildren } = useChildren(SIDEBAR_KEY);
const active = () => +props.modelValue; const getActive = () => +props.modelValue;
const setActive = (value) => { const setActive = (value: number) => {
if (value !== active()) { if (value !== getActive()) {
emit('update:modelValue', value);
emit('change', value); emit('change', value);
} }
}; };
watch(active, setActive);
linkChildren({ linkChildren({
emit, getActive,
active,
setActive, setActive,
}); });

View File

@ -1981,10 +1981,10 @@
resolved "https://registry.yarnpkg.com/@vant/touch-emulator/-/touch-emulator-1.2.0.tgz#486300b23e57db9ce9231a04e0a0c621c68692d8" resolved "https://registry.yarnpkg.com/@vant/touch-emulator/-/touch-emulator-1.2.0.tgz#486300b23e57db9ce9231a04e0a0c621c68692d8"
integrity sha512-sJ97zU85zOq51qoi7+CpBEcOyH3CitjP1KC7/GQwqaurUJni+EP7/F9n0HMnAh8GXMjgtgDBNJ5z48x+coNKYQ== integrity sha512-sJ97zU85zOq51qoi7+CpBEcOyH3CitjP1KC7/GQwqaurUJni+EP7/F9n0HMnAh8GXMjgtgDBNJ5z48x+coNKYQ==
"@vant/use@^1.0.2": "@vant/use@^1.0.3":
version "1.0.2" version "1.0.3"
resolved "https://registry.npm.taobao.org/@vant/use/download/@vant/use-1.0.2.tgz?cache=0&sync_timestamp=1609472383302&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40vant%2Fuse%2Fdownload%2F%40vant%2Fuse-1.0.2.tgz#75486aedf3539382baa922c77eeb8a2e2394a948" resolved "https://registry.npmjs.org/@vant/use/-/use-1.0.3.tgz#ec3f82c30b883b1ceb36e49b60882e332ea04bf4"
integrity sha1-dUhq7fNTk4K6qSLHfuuKLiOUqUg= integrity sha512-tnR6mdsjnN2mmiBznn8FNueyZYSFBavlOPNZd3Nf9SRP4QLPeHeebGSxWqYpFf6jpjYqOy0HHgtXz2Gu8dLeaw==
dependencies: dependencies:
"@babel/runtime" "7.x" "@babel/runtime" "7.x"