vuejs-challenges icon indicating copy to clipboard operation
vuejs-challenges copied to clipboard

21 - 函数式组件 tsx解法

Open Tofu-Xx opened this issue 5 months ago • 0 comments

<script setup lang='tsx'>
import { ref } from "vue"

/**
 * Implement a functional component :
 * 1. Render the list elements (ul/li) with the list data
 * 2. Change the list item text color to red when clicked.
*/
const ListComponent = ({ list, activeIndex }, { emit }) => (
  <ul>{
    list.map(({ name }, i) => (
      <li style={{ color: (activeIndex == i) ? 'red' : null }} onClick={() => { emit('toggle', i) }}> {
        name
      } </li>
    ))
  } </ul>
)


const list = [{
  name: "John",
}, {
  name: "Doe",
}, {
  name: "Smith",
}]

const activeIndex = ref(0)

function toggle(index: number) {
  activeIndex.value = index
}
</script>

<template>
  <list-component :list :activeIndex @toggle="toggle" />
</template>

Tofu-Xx avatar Sep 15 '24 18:09 Tofu-Xx