-
-
Notifications
You must be signed in to change notification settings - Fork 34
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: Select
component
#18
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
import { | ||
h, | ||
provide, | ||
inject, | ||
defineComponent, | ||
onMounted, | ||
onUpdated, | ||
onUnmounted, | ||
Fragment, | ||
computed, | ||
Comment, | ||
} from '@vue/runtime-core' | ||
import type { | ||
PropType, | ||
InjectionKey, | ||
VNode, | ||
ComputedRef, | ||
} from '@vue/runtime-core' | ||
import { TuiBox } from './Box' | ||
import { TuiText } from './Text' | ||
import { scheduleUpdateSymbol } from '../injectionSymbols' | ||
import { onKeyData } from '../composables/keyboard' | ||
import type { ForegroundColorProp } from '../renderer/textColor' | ||
import { KeyDataEventKey } from '../input/types' | ||
|
||
export interface TuiSelectIndicator { | ||
/** | ||
* Figure of indicator. | ||
* | ||
* @default ❯ | ||
*/ | ||
figure: string | ||
/** | ||
* Indicator figure color. | ||
* | ||
* @default ❯ | ||
*/ | ||
color: ForegroundColorProp | ||
} | ||
|
||
export const tuiSelectSymbol = Symbol('vue-termui:select') as InjectionKey<{ | ||
activeName: ComputedRef<string | number> | ||
indicator: TuiSelectIndicator | ||
}> | ||
|
||
export const TuiSelect = defineComponent({ | ||
name: 'TuiSelect', | ||
props: { | ||
modelValue: { | ||
type: [String, Number], | ||
required: true, | ||
}, | ||
indicator: { | ||
type: Object as PropType<TuiSelectIndicator>, | ||
default: { | ||
figure: '❯', | ||
color: 'blue', | ||
}, | ||
}, | ||
submitKey: { | ||
type: [String, Array] as PropType<KeyDataEventKey | KeyDataEventKey[]>, | ||
required: false, | ||
// Space key | ||
default: [' '], | ||
}, | ||
}, | ||
emit: ['update:modelValue', 'submit'], | ||
setup(props, { slots, emit }) { | ||
const children = computed(() => { | ||
const defaultSlots = slots.default?.() | ||
const children = defaultSlots | ||
?.filter((child) => child.type !== Comment) | ||
?.reduce( | ||
(nodeList: VNode[], node: VNode) => | ||
node.type === Fragment | ||
? [...nodeList, ...(node.children as VNode[])] | ||
: [...nodeList, node], | ||
[] | ||
) | ||
|
||
return children ?? [] | ||
}) | ||
|
||
const activeName = computed(() => props.modelValue) | ||
const activeIndex = computed(() => | ||
children.value?.findIndex( | ||
(item) => item?.props?.value === activeName.value | ||
) | ||
) | ||
|
||
const scheduleUpdate = inject(scheduleUpdateSymbol)! | ||
|
||
onMounted(scheduleUpdate) | ||
|
||
onUpdated(scheduleUpdate) | ||
|
||
onUnmounted(scheduleUpdate) | ||
|
||
provide(tuiSelectSymbol, { | ||
activeName: activeName, | ||
indicator: props.indicator, | ||
}) | ||
|
||
const stopDownInput = onKeyData(['ArrowDown', 'ArrowRight'], () => { | ||
const index = | ||
activeIndex.value! + 1 > children.value.length! - 1 | ||
? 0 | ||
: activeIndex.value! + 1 | ||
emit('update:modelValue', children.value?.[index]?.props?.value) | ||
}) | ||
|
||
const stopUpInput = onKeyData(['ArrowUp', 'ArrowLeft'], () => { | ||
const index = | ||
activeIndex.value! - 1 < 0 | ||
? children.value.length! - 1 | ||
: activeIndex.value! - 1 | ||
emit('update:modelValue', children.value?.[index]?.props?.value) | ||
}) | ||
|
||
const stopSubmitInput = onKeyData(props.submitKey, () => { | ||
stopDownInput() | ||
stopUpInput() | ||
stopSubmitInput() | ||
emit('submit', activeName.value, activeIndex.value) | ||
}) | ||
|
||
return () => { | ||
return h( | ||
TuiBox, | ||
{ | ||
flexDirection: 'column', | ||
}, | ||
() => slots.default?.() | ||
) | ||
} | ||
}, | ||
}) | ||
|
||
export const TuiOption = defineComponent({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think they can each of them have their own file 😄 |
||
name: 'TuiOption', | ||
|
||
props: { | ||
label: { | ||
type: [String, Number], | ||
required: false, | ||
}, | ||
value: [String, Number], | ||
}, | ||
|
||
setup(props, { slots }) { | ||
const tuiSelectRoot = inject(tuiSelectSymbol)! | ||
return () => { | ||
const isActive = props.value === tuiSelectRoot.activeName.value | ||
const { color, figure } = tuiSelectRoot.indicator | ||
return h(TuiBox, {}, () => [ | ||
// Indicator | ||
h( | ||
TuiText, | ||
{ | ||
color, | ||
}, | ||
() => (isActive ? figure : ' ') + ' ' | ||
), | ||
// Option | ||
slots | ||
.default?.({ | ||
isActive, | ||
}) | ||
?.filter((child) => child.type !== Comment)?.length | ||
? h( | ||
Fragment, | ||
slots.default?.({ | ||
isActive, | ||
}) | ||
) | ||
: h(TuiText, () => props.label), | ||
]) | ||
} | ||
}, | ||
}) |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,38 @@ | ||||||||||||||||||||||
<script setup lang="ts"> | ||||||||||||||||||||||
import { ref } from 'vue-termui' | ||||||||||||||||||||||
const value = ref('vue') | ||||||||||||||||||||||
const options = [ | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
value: 'vue', | ||||||||||||||||||||||
label: 'Vue', | ||||||||||||||||||||||
}, | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
value: 'react', | ||||||||||||||||||||||
label: 'React', | ||||||||||||||||||||||
}, | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
value: 'svelte', | ||||||||||||||||||||||
label: 'Svelte', | ||||||||||||||||||||||
}, | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
value: 'solid', | ||||||||||||||||||||||
label: 'Solid', | ||||||||||||||||||||||
}, | ||||||||||||||||||||||
] | ||||||||||||||||||||||
|
||||||||||||||||||||||
function submit(name: string, index: number) { | ||||||||||||||||||||||
console.log(name, index) | ||||||||||||||||||||||
} | ||||||||||||||||||||||
</script> | ||||||||||||||||||||||
|
||||||||||||||||||||||
<template borderStyle="round"> | ||||||||||||||||||||||
<Select v-model="value" @submit="submit"> | ||||||||||||||||||||||
<Option :value="item.value" v-for="(item, index) in options" :key="index"> | ||||||||||||||||||||||
<template v-slot="{ isActive }"> | ||||||||||||||||||||||
<Box flexDirection="column"> | ||||||||||||||||||||||
<Text :dimmed="isActive" :underline="isActive">{{ item.label }}</Text> | ||||||||||||||||||||||
</Box> | ||||||||||||||||||||||
</template> | ||||||||||||||||||||||
</Option> | ||||||||||||||||||||||
Comment on lines
+30
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally, this should look like this:
Suggested change
and even allow not passing the |
||||||||||||||||||||||
</Select> | ||||||||||||||||||||||
</template> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think this is more interesting that having two different props?