Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(comp:select): add loading prop for select component #1439

Merged
merged 2 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/components/select/demo/Loading.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
title:
zh: 加载中
en: Loading
order: 51
---
## zh

数据还未加载完成时,展示一个加载中的状态。

## en

When the data has not been loaded yet, display a loading status。
34 changes: 34 additions & 0 deletions packages/components/select/demo/Loading.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<template>
<IxSpace vertical>
<IxSelect v-model:value="value" :dataSource="dataSource" :spin="loading"></IxSelect>
<IxButton @click="onLoad">Load Data</IxButton>
</IxSpace>
</template>
<script setup lang="ts">
import { ref } from 'vue'

import { SelectData } from '@idux/components/select'

const dataSource = ref<SelectData[]>([{ label: 'Tom', value: 'tom' }])
const value = ref('tom')
const loading = ref(false)

const onLoad = () => {
dataSource.value = [{ label: 'Tom', value: 'tom' }]
value.value = 'tom'

if (loading.value) {
return
}

loading.value = true
setTimeout(() => {
loading.value = false
dataSource.value = [
{ label: 'Tom', value: 'tom' },
{ label: 'Jack', value: 'jack' },
{ label: 'Lucy', value: 'lucy' },
]
}, 3000)
}
</script>
1 change: 1 addition & 0 deletions packages/components/select/docs/Api.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
| `searchable` | 是否可搜索 | `boolean \| 'overlay'` | `false` | - | 当为 `true` 时搜索功能集成在选择器上,当为 `overlay` 时,搜索功能集成在悬浮层上 |
| `searchFn` | 根据搜索的文本进行筛选 | `boolean \| SelectSearchFn` | `true` | - | 为 `true` 时使用默认的搜索规则, 如果使用远程搜索,应该设置为 `false` |
| `size` | 设置选择器大小 | `'sm' \| 'md' \| 'lg'` | `md` | ✅ | - |
| `spin` | 是否显示加载中状态 | `boolean \| SpinProps` | `undefined` | - | - |
| `status` | 手动指定校验状态 | `valid \| invalid \| validating` | - | - | - |
| `suffix` | 设置后缀图标 | `string \| #suffix` | `down` | ✅ | - |
| `virtual` | 是否开启虚拟滚动 | `boolean` | `false` | - | - |
Expand Down
11 changes: 10 additions & 1 deletion packages/components/select/src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import { type ComputedRef, Slots, computed, defineComponent, normalizeClass, provide, ref, watch } from 'vue'

import { isBoolean } from 'lodash-es'

import { useAccessorAndControl } from '@idux/cdk/forms'
import { type VirtualScrollToFn } from '@idux/cdk/scroll'
import { type VKey, callEmit, useState } from '@idux/cdk/utils'
Expand All @@ -17,6 +19,7 @@ import { ɵOverlay } from '@idux/components/_private/overlay'
import { ɵSelector, type ɵSelectorInstance } from '@idux/components/_private/selector'
import { type SelectConfig, useGlobalConfig } from '@idux/components/config'
import { useFormItemRegister, useFormSize, useFormStatus } from '@idux/components/form'
import { IxSpin } from '@idux/components/spin'

import { useActiveState } from './composables/useActiveState'
import { GetKeyFn, useGetOptionKey } from './composables/useGetOptionKey'
Expand Down Expand Up @@ -160,8 +163,14 @@ export default defineComponent({
/>
)

const renderLoading = (children: JSX.Element) => {
const { spin } = props
const spinProps = isBoolean(spin) ? { spinning: spin } : spin
return spinProps ? <IxSpin {...spinProps}>{children}</IxSpin> : children
}

const renderContent = () => {
const children = [<Panel ref={panelRef} v-slots={slots} {...panelProps.value} />]
const children = [renderLoading(<Panel ref={panelRef} v-slots={slots} {...panelProps.value} />)]
const { searchable, overlayRender } = props

if (searchable === 'overlay') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export function usePanelProps(
multiple: props.multiple,
multipleLimit: props.multipleLimit,
virtual: props.virtual,

'onUpdate:activeValue': setActiveValue,
onOptionClick,
onScroll: props.onScroll,
Expand Down
2 changes: 2 additions & 0 deletions packages/components/select/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { VirtualScrollToFn } from '@idux/cdk/scroll'
import type { ExtractInnerPropTypes, ExtractPublicPropTypes, MaybeArray, VKey } from '@idux/cdk/utils'
import type { EmptyProps } from '@idux/components/empty'
import type { FormSize } from '@idux/components/form'
import type { SpinProps } from '@idux/components/spin'
import type { OverlayContainerType } from '@idux/components/utils'
import type { DefineComponent, FunctionalComponent, HTMLAttributes, PropType, VNode, VNodeChild } from 'vue'

Expand Down Expand Up @@ -79,6 +80,7 @@ export const selectProps = {
size: { type: String as PropType<FormSize>, default: undefined },
status: String as PropType<ValidateStatus>,
suffix: { type: String, default: undefined },
spin: { type: [Boolean, Object] as PropType<boolean | SpinProps>, default: undefined },
virtual: { type: Boolean, default: false },

// events
Expand Down