-
-
Notifications
You must be signed in to change notification settings - Fork 201
Closed
Description
`<script setup lang='ts'>
import { ref, h } from "vue"
import type { FunctionalComponent } from 'vue'
type ListObj = {
name: string
}
type ListComponentProps = {
list?: Array,
activeIndex?: number
}
type ListComponentEvent = {
toggle(index: number): void
}
/**
- Implement a functional component :
-
- Render the list elements (ul/li) with the list data
-
- Change the list item text color to red when clicked.
*/
const ListComponent: FunctionalComponent<ListComponentProps, ListComponentEvent> = (props, context) => {
return h('ul', props.list.map((item, i) => h(
'li',
{
key: i,
style: Object.assign({ cursor: 'pointer' }, props['active-index'] === i && { color: 'red' }),
onClick: () => context.emit('toggle', i)
},
item.name
)))
}
ListComponent.displayName = 'ListComponent'
- Change the list item text color to red when clicked.
const list: Array = [{
name: "John",
}, {
name: "Doe",
}, {
name: "Smith",
}]
const activeIndex = ref(0)
function toggle(index: number) {
activeIndex.value = index
}