Skip to content

Commit

Permalink
fix: ...该死的手风琴 我恨手风琴
Browse files Browse the repository at this point in the history
想来一段优美的中国话
  • Loading branch information
sj817 committed Feb 18, 2025
1 parent 8f00701 commit 2349b71
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
17 changes: 12 additions & 5 deletions packages/web/src/components/heroui/accordions-pro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,18 @@ export const AccordionPro = (

if (options.componentType === 'input-group') {
const opt = { ...options, key: strKey }
return InputGroup(opt, {}, (index, value) => {
data[i][options.key][index] = value
if (!result[key][i]) result[key][i] = {}
result[key][i][options.key][index] = value
})
return InputGroup(opt, {}, (index, value, type) => {
if (type === 'add') {
data[i][options.key][index] = value
if (!result[key][i]) result[key][i] = {}
result[key][i][options.key][index] = value
} else {
data[i][options.key].splice(index, 1)
if (!result[key][i]) result[key][i] = {}
result[key][i][options.key].splice(index, 1)
}

}, result[key][i][options.key])
}

return null
Expand Down
13 changes: 9 additions & 4 deletions packages/web/src/components/heroui/accordions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const Accordion = (
}

if (options.componentType === 'checkbox-group') {
(result[key][index] as Record<string, AccordionKV>)[options.key] = {}
result[key][index][options.key] = {}
return CheckboxGroup(options, {}, (subKey, value) => {
result[key][index][options.key][subKey] = value
})
Expand All @@ -107,9 +107,14 @@ export const Accordion = (
}

if (options.componentType === 'input-group') {
return InputGroup(options, {}, (i, value) => {
result[key][index][options.key][i] = value
})
return InputGroup(options, {}, (i, value, type) => {
if (type === 'add') {
result[key][index][options.key][i] = value
result[key][index][options.key] = result[key][index][options.key].map(String)
} else {
result[key][index][options.key].splice(i, 1)
}
}, result[key][index][options.key])
}
})}
</div>
Expand Down
22 changes: 13 additions & 9 deletions packages/web/src/components/heroui/inputs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export const Input = (
export const InputGroup = (
props: InputGroupProps,
result: Result<'input-group'>,
onValueChange?: (index: number, value: string) => void
onValueChange?: (index: number, value: string, type: 'add' | 'del') => void,
cache?: string[],
) => {
const { key, template, data } = props
const { componentType: __, key: ___, ...templateOptions } = template
Expand All @@ -73,7 +74,7 @@ export const InputGroup = (
})
}

const [dataVal, setDataVal] = useState(() => result[key])
const [dataVal, setDataVal] = useState(() => result[key] || cache || [])

// 同步到result
useEffect(() => {
Expand Down Expand Up @@ -120,18 +121,20 @@ export const InputGroup = (
return
}

if (!onValueChange) {
result[key] = [...result[key], '']
setDataVal(prev => [...prev, ''])
if (onValueChange) {
onValueChange(dataVal.length, '', 'add')
} else {
onValueChange(dataVal.length, '')
result[key] = [...result[key], '']
}
setDataVal(prev => [...prev, ''])
}

// 删除输入框
const handleDeleteInput = (index: number) => {
if (!onValueChange) {
result[key] = result[key].filter((_, i) => i !== index)
if (onValueChange) {
onValueChange(index, '', 'del')
} else {
result[key].splice(index, 1)
}

setDataVal(prev => prev.filter((_, i) => i !== index))
Expand All @@ -152,6 +155,7 @@ export const InputGroup = (
{dataVal.length}{maxInputs !== 0 ? `/${maxInputs}` : ''}
</span>
<Button
key={`${key}-add-input`}
variant="solid"
color="primary"
size="sm"
Expand Down Expand Up @@ -191,7 +195,7 @@ export const InputGroup = (
}}
onValueChange={(value) => {
if (onValueChange) {
onValueChange(index, value)
onValueChange(index, value, 'add')
} else {
result[key][index] = value
}
Expand Down

0 comments on commit 2349b71

Please sign in to comment.