增加多个url功能
parent
f7f9425117
commit
6942f630c7
@ -0,0 +1,80 @@
|
||||
import { useSignal } from "@/lib/signal";
|
||||
import { arrayEquals } from "@/lib/utils";
|
||||
import { useCallback, useEffect, useMemo } from "react";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { X } from "lucide-react";
|
||||
|
||||
export interface UrlListInputProps {
|
||||
value: string[];
|
||||
onChange: (urls: string[]) => void;
|
||||
placeholder?: string;
|
||||
}
|
||||
|
||||
export const UrlListInput = (props: UrlListInputProps) => {
|
||||
const urlList = useSignal<string[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!arrayEquals(urlList.value, props.value)) {
|
||||
urlList.value = props.value;
|
||||
}
|
||||
}, [props.value]);
|
||||
|
||||
const handleInputChange = useCallback((index: number, value: string) => {
|
||||
const newList = [...urlList.value];
|
||||
|
||||
if (index === urlList.value.length) {
|
||||
// 新的空白输入框,如果有内容则添加到列表
|
||||
if (value.trim() !== '') {
|
||||
newList.push(value);
|
||||
}
|
||||
} else {
|
||||
// 现有输入框
|
||||
if (value.trim() === '') {
|
||||
// 内容为空,删除该项
|
||||
newList.splice(index, 1);
|
||||
} else {
|
||||
// 更新内容
|
||||
newList[index] = value;
|
||||
}
|
||||
}
|
||||
|
||||
urlList.value = newList;
|
||||
props.onChange(newList);
|
||||
}, [props.onChange]);
|
||||
|
||||
const handleClear = useCallback((index: number) => {
|
||||
const newList = [...urlList.value];
|
||||
newList.splice(index, 1);
|
||||
urlList.value = newList;
|
||||
props.onChange(newList);
|
||||
}, [props.onChange]);
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-2">
|
||||
{/* 渲染现有的输入框 */}
|
||||
{[...urlList.value, null].map((url, index) => (
|
||||
<div key={index} className="flex items-center gap-2">
|
||||
<Input
|
||||
value={url || ""}
|
||||
onChange={(e) => handleInputChange(index, e.target.value)}
|
||||
placeholder={props.placeholder || "请输入URL"}
|
||||
type="url"
|
||||
name="video-url"
|
||||
className="flex-1"
|
||||
/>
|
||||
<Button
|
||||
variant="outline"
|
||||
type="button"
|
||||
size="sm"
|
||||
onClick={() => handleClear(index)}
|
||||
className="px-2"
|
||||
disabled={index === urlList.value.length}
|
||||
>
|
||||
<X className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
Loading…
Reference in New Issue