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: add select slot #1493

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from
Open
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
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,35 @@ yarn lint
```sh
# Types, ES, ESM, CommonJS, IIFE
yarn test
```
```

## Use Select Slot

```vue
<script>
import ASelect from 'ant-design-vue/es/select';
import AOption from 'ant-design-vue/es/select/option';
</script>

<template>
<VCalendarDatePicker v-if="date && (typeof date === 'object')" v-model.range="date" :columns="2" v-bind="{ ...attrs, ...$attrs }" :attributes="attributes" mode="dateTime" is24hr>
<template #select="{ onChange, value, options }">
<a-select
:default-value="value"
size="mini"
:style="{ width: '68px' }"
@change="(val) => onChange(val)"
>
<a-option
v-for="option in options"
:key="option.value"
:value="option.value"
:disabled="option.disabled"
>
{{ option.label }}
</a-option>
</a-select>
</template>
</VCalendarDatePicker>
</template>
```
57 changes: 30 additions & 27 deletions src/components/BaseSelect/BaseSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,29 @@
'vc-has-icon': showIcon,
}"
>
<select
v-bind="$attrs"
:value="modelValue"
class="vc-focus"
:class="{
'vc-align-right': alignRight,
'vc-align-left': alignLeft,
}"
@change="
$emit('update:modelValue', ($event.target as HTMLSelectElement).value)
"
>
<option
v-for="option in options"
:key="option.value"
:value="option.value"
:disabled="option.disabled"
<slot name="select" :onChange="handleChange" :value="modelValue" :options="options" :alignRight="alignRight" :alignLeft="alignLeft">
<select
v-bind="$attrs"
:value="modelValue"
class="vc-focus"
:class="{
'vc-align-right': alignRight,
'vc-align-left': alignLeft,
}"
@change="
$emit('update:modelValue', ($event.target as HTMLSelectElement).value)
"
>
{{ option.label }}
</option>
</select>
<option
v-for="option in options"
:key="option.value"
:value="option.value"
:disabled="option.disabled"
>
{{ option.label }}
</option>
</select>
</slot>
<BaseIcon v-if="showIcon" name="ChevronDown" size="18" />
<div v-if="fitContent" class="vc-base-sizer" aria-hidden="true">
{{ selectedLabel }}
Expand All @@ -35,12 +37,6 @@
</template>

<script lang="ts">
interface BaseOption {
value: any;
label: string;
disabled?: boolean;
}

export default {
inheritAttrs: false,
};
Expand All @@ -49,6 +45,8 @@ export default {
<script setup lang="ts">
import { computed } from 'vue';
import BaseIcon from '../BaseIcon/BaseIcon.vue';
import type { BaseOption } from './types'


const props = defineProps<{
options: BaseOption[];
Expand All @@ -58,12 +56,16 @@ const props = defineProps<{
showIcon?: boolean;
fitContent?: boolean;
}>();
defineEmits(['update:modelValue']);
const emit = defineEmits(['update:modelValue']);

const selectedLabel = computed(() => {
const option = props.options.find(opt => opt.value === props.modelValue);
return option?.label;
});

function handleChange(val: any): void {
emit('update:modelValue', val);
}
</script>

<style lang="css">
Expand Down Expand Up @@ -134,3 +136,4 @@ const selectedLabel = computed(() => {
}
}
</style>
./types
5 changes: 5 additions & 0 deletions src/components/BaseSelect/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface BaseOption {
value: any;
label: string;
disabled?: boolean;
}
6 changes: 5 additions & 1 deletion src/components/DatePicker/DatePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
<slot v-bind="slotCtx" />
<DatePickerPopover v-bind="$attrs" />
</template>
<DatePickerBase v-else v-bind="$attrs" />
<DatePickerBase v-else v-bind="$attrs">
<template #select="{ onChange, value, options, alignRight, alignLeft }">
<slot name="select" :onChange="onChange" :value="value" :options="options" :alignRight="alignRight" :alignLeft="alignLeft" />
</template>
</DatePickerBase>
</template>

<script lang="ts">
Expand Down
12 changes: 10 additions & 2 deletions src/components/DatePicker/DatePickerBase.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
v-for="position in positions"
:key="position"
:position="position"
/>
>
<template #select="{ onChange, value, options, alignRight, alignLeft }">
<slot name="select" :onChange="onChange" :value="value" :options="options" :alignRight="alignRight" :alignLeft="alignLeft" />
</template>
</TimePicker>
</div>
<Calendar
v-else
Expand All @@ -23,7 +27,11 @@
v-for="position in positions"
:key="position"
:position="position"
/>
>
<template #select="{ onChange, value, options, alignRight, alignLeft }">
<slot name="select" :onChange="onChange" :value="value" :options="options" :alignRight="alignRight" :alignLeft="alignLeft" />
</template>
</TimePicker>
</template>
<CalendarSlot name="dp-footer" />
</template>
Expand Down
30 changes: 25 additions & 5 deletions src/components/DatePicker/TimePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,23 @@
:options="hourOptions"
class="vc-time-select-hours"
align-right
/>
>
<template #select="{ onChange, value, options, alignRight, alignLeft }">
<slot name="select" :onChange="onChange" :value="value" :options="options" :alignRight="alignRight" :alignLeft="alignLeft" />
</template>
</BaseSelect>
<template v-if="timeAccuracy > 1">
<span class="vc-time-colon">:</span>
<BaseSelect
v-model.number="minutes"
:options="options.minutes"
class="vc-time-select-minutes"
:align-left="timeAccuracy === 2"
/>
>
<template #select="{ onChange, value, options, alignRight, alignLeft }">
<slot name="select" :onChange="onChange" :value="value" :options="options" :alignRight="alignRight" :alignLeft="alignLeft" />
</template>
</BaseSelect>
</template>
<template v-if="timeAccuracy > 2">
<span class="vc-time-colon">:</span>
Expand All @@ -43,7 +51,11 @@
:options="options.seconds"
class="vc-time-select-seconds"
:align-left="timeAccuracy === 3"
/>
>
<template #select="{ onChange, value, options, alignRight, alignLeft }">
<slot name="select" :onChange="onChange" :value="value" :options="options" :alignRight="alignRight" :alignLeft="alignLeft" />
</template>
</BaseSelect>
</template>
<template v-if="timeAccuracy > 3">
<span class="vc-time-decimal">.</span>
Expand All @@ -52,9 +64,17 @@
:options="options.milliseconds"
class="vc-time-select-milliseconds"
align-left
/>
>
<template #select="{ onChange, value, options, alignRight, alignLeft }">
<slot name="select" :onChange="onChange" :value="value" :options="options" :alignRight="alignRight" :alignLeft="alignLeft" />
</template>
</BaseSelect>
</template>
<BaseSelect v-if="!is24hr" v-model="isAM" :options="isAMOptions" />
<BaseSelect v-if="!is24hr" v-model="isAM" :options="isAMOptions">
<template #select="{ onChange, value, options, alignRight, alignLeft }">
<slot name="select" :onChange="onChange" :value="value" :options="options" :alignRight="alignRight" :alignLeft="alignLeft" />
</template>
</BaseSelect>
</div>
</div>
</template>
Expand Down