/* ============================================================================ * Copyright (c) Palo Alto Networks * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * ========================================================================== */ import React, { useEffect, useState } from "react"; import { translate } from "@docusaurus/Translate"; import { ErrorMessage } from "@hookform/error-message"; import { nanoid } from "@reduxjs/toolkit"; import FormLabel from "@theme/ApiExplorer/FormLabel"; import FormSelect from "@theme/ApiExplorer/FormSelect"; import FormTextInput from "@theme/ApiExplorer/FormTextInput"; import { Param, setParam } from "@theme/ApiExplorer/ParamOptions/slice"; import { useTypedDispatch } from "@theme/ApiItem/hooks"; import { OPENAPI_FORM } from "@theme/translationIds"; import { Controller, useFormContext } from "react-hook-form"; export interface ParamProps { param: Param; label?: string; type?: string; required?: boolean; } function ArrayItem({ param, onChange, initialValue, }: ParamProps & { onChange(value?: string): any; initialValue?: string }) { const [value, setValue] = useState(initialValue || ""); if (param.schema?.items?.type === "boolean") { return ( ) => { const val = e.target.value; onChange(val === "---" ? undefined : val); }} /> ); } return ( ) => { setValue(e.target.value); onChange(e.target.value); }} /> ); } export default function ParamArrayFormItem({ param, label, type, required, }: ParamProps) { const [items, setItems] = useState<{ id: string; value?: string }[]>([]); const dispatch = useTypedDispatch(); const { control, formState: { errors }, } = useFormContext(); const showErrorMessage = errors?.paramArray?.message; function handleAddItem(e: any) { e.preventDefault(); // prevent form from submitting setItems((i) => [ ...i, { id: nanoid(), }, ]); } useEffect(() => { const values = items .map((item) => item.value) .filter((item): item is string => !!item); dispatch( setParam({ ...param, value: values.length > 0 ? values : undefined, }) ); // eslint-disable-next-line react-hooks/exhaustive-deps }, [items]); useEffect(() => { const example = param.schema?.example; if (Array.isArray(example) && example.length > 0) { const examplesWithIds = example.map((item: any) => ({ id: nanoid(), value: item.toString(), })); setItems(examplesWithIds); } }, [param.schema?.example]); function handleDeleteItem(itemToDelete: { id: string }) { return () => { const newItems = items.filter((i) => i.id !== itemToDelete.id); setItems(newItems); }; } function handleChangeItem(itemToUpdate: { id: string }, onChange: any) { return (value: string) => { const newItems = items.map((i) => { if (i.id === itemToUpdate.id) { return { ...i, value: value }; } return i; }); setItems(newItems); onChange(newItems); }; } return ( <> {label && } ( <> {items.map((item) => (
))} )} /> {showErrorMessage && ( (
{message}
)} /> )} ); }