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: Select component #18

Open
wants to merge 1 commit into
base: main
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
180 changes: 180 additions & 0 deletions packages/core/src/components/Select.ts
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: {
Copy link
Collaborator

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?

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({
Copy link
Collaborator

Choose a reason for hiding this comment

The 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),
])
}
},
})
1 change: 1 addition & 0 deletions packages/core/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export { TuiTextTransform } from './TextTransform'
export { TuiNewline } from './Newline'
export { TuiApp } from './App'
export { TuiBox } from './Box'
export { TuiSelect, TuiOption } from './Select'

export { TuiLink } from './Link'
// export { default as TuiInput } from './Input.vue'
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export {
TuiNewline,
TuiLink,
TuiTextTransform,
TuiSelect,
TuiOption,
} from './components'

export { render } from './renderer'
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/renderer/textColor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import chalk from 'chalk'

import type { ForegroundColor } from 'chalk'
type ColorType = 'foreground' | 'background'
import type { LiteralUnion } from '../utils'

export type ForegroundColorProp = LiteralUnion<ForegroundColor, string>

const RGB_LIKE_REGEX = /^(rgb|hsl|hsv|hwb)\(\s?(\d+),\s?(\d+),\s?(\d+)\s?\)$/
const ANSI_REGEX = /^(ansi|ansi256)\(\s?(\d+)\s?\)$/
Expand Down
2 changes: 2 additions & 0 deletions packages/playground/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ declare module '@vue/runtime-core' {
Br: typeof import('vue-termui')['TuiNewline']
Div: typeof import('vue-termui')['TuiBox']
Link: typeof import('vue-termui')['TuiLink']
Option: typeof import('vue-termui')['TuiOption']
Select: typeof import('vue-termui')['TuiSelect']
Span: typeof import('vue-termui')['TuiText']
Text: typeof import('vue-termui')['TuiText']
}
Expand Down
38 changes: 38 additions & 0 deletions packages/playground/src/SelectDemo.vue
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, this should look like this:

Suggested change
<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>
<Option :value="item.value" v-for="(item, index) in options" :key="index">
{{ item.label }}
</Option>

and even allow not passing the value prop. We can achieve this by using inject/provide. The v-slot api is nice to allow the user further customization though

</Select>
</template>
3 changes: 2 additions & 1 deletion packages/playground/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// import devtools from '@vue/devtools'
// import devtools from '@vue/devtools/node'
import { createApp } from 'vue-termui'
import App from './Focusables.vue'
// import App from './Focusables.vue'
// import App from './Fragments.vue'
// import App from './CenteredDemo.vue'
// import App from './App.vue'
// import App from './Counter.vue'
// import App from './Borders.vue'
import App from './SelectDemo.vue'

createApp(App, {
// swapScreens: true,
Expand Down
3 changes: 3 additions & 0 deletions packages/vite-plugin-vue-termui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ export const VueTuiComponents = new Map<string, ModuleExports>([

['transform', 'TuiTextTransform'],
['text-transform', 'TuiTextTransform'],

['select', 'TuiSelect'],
['option', 'TuiOption'],
])

// copied from auto import plugin source code
Expand Down