Commit 649c3d07 authored by Joel's avatar Joel

feat: key value struct

parent 35c56237
'use client'
import type { FC } from 'react'
import React, { useCallback } from 'react'
import { useBoolean } from 'ahooks'
import type { KeyValue } from '../types'
import { Trash03 } from '@/app/components/base/icons/src/vender/line/general'
type Props = {
payload: KeyValue
onChange: (newPayload: KeyValue) => void
onRemove: () => void
isLastItem: boolean
onAdd: () => void
}
const KeyValueItem: FC<Props> = ({
payload,
onChange,
onRemove,
isLastItem,
onAdd,
}) => {
const [isKeyEditing, {
setTrue: setIsKeyEditing,
setFalse: setIsKeyEditingFalse,
}] = useBoolean(false)
const handleKeyChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
onChange({
key: e.target.value,
value: payload.value,
})
}, [])
return (
<div>
<div>
{isKeyEditing
? (
<input
type='text'
value={payload.key}
onChange={handleKeyChange}
onBlur={setIsKeyEditingFalse}
/>
)
: <div onClick={setIsKeyEditing}>{payload.key}</div>}
</div>
<div
>
{payload.value}
<div
className='p-1 cursor-pointer rounded-md hover:bg-black/5'
onClick={onRemove}
>
<Trash03 className='w-4 h-4' />
</div>
</div>
</div>
)
}
export default React.memo(KeyValueItem)
'use client'
import type { FC } from 'react'
import React from 'react'
import type { KeyValue } from '../types'
import KeyValueItem from './key-value-item'
type Props = {
list: KeyValue[]
onChange: (newList: KeyValue[]) => void
onAdd: () => void
}
const KeyValueList: FC<Props> = ({
list,
onChange,
onAdd,
}) => {
return (
<div>
<div>
<div>key</div>
<div>value</div>
</div>
{
list.map((item, index) => (
<KeyValueItem
key={index}
payload={item}
onChange={(newItem) => {
const newList = [...list]
newList[index] = newItem
onChange(newList)
}}
onRemove={() => {
const newList = [...list]
newList.splice(index, 1)
onChange(newList)
}}
isLastItem={index === list.length - 1}
onAdd={onAdd}
/>
))
}
</div>
)
}
export default React.memo(KeyValueList)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment