Skip to content

Commit d786ecf

Browse files
committed
Add prefilled state for array param examples
1 parent fa3fbbf commit d786ecf

File tree

1 file changed

+19
-1
lines changed
  • packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/ParamOptions/ParamFormItems

1 file changed

+19
-1
lines changed

packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/ParamOptions/ParamFormItems/ParamArrayFormItem.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ export interface ParamProps {
2222
function ArrayItem({
2323
param,
2424
onChange,
25-
}: ParamProps & { onChange(value?: string): any }) {
25+
initialValue,
26+
}: ParamProps & { onChange(value?: string): any; initialValue?: string }) {
27+
const [value, setValue] = useState(initialValue || "");
28+
2629
if (param.schema?.items?.type === "boolean") {
2730
return (
2831
<FormSelect
@@ -38,7 +41,9 @@ function ArrayItem({
3841
return (
3942
<FormTextInput
4043
placeholder={param.description || param.name}
44+
value={value}
4145
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
46+
setValue(e.target.value);
4247
onChange(e.target.value);
4348
}}
4449
/>
@@ -76,9 +81,21 @@ export default function ParamArrayFormItem({ param }: ParamProps) {
7681
value: values.length > 0 ? values : undefined,
7782
})
7883
);
84+
7985
// eslint-disable-next-line react-hooks/exhaustive-deps
8086
}, [items]);
8187

88+
useEffect(() => {
89+
if (param.schema?.example?.length > 0) {
90+
const examplesWithIds = param.schema.example.map((item: any) => ({
91+
id: nanoid(),
92+
value: item.toString(),
93+
}));
94+
95+
setItems(examplesWithIds);
96+
}
97+
}, [param.schema.example, param.schema.length]);
98+
8299
function handleDeleteItem(itemToDelete: { id: string }) {
83100
return () => {
84101
const newItems = items.filter((i) => i.id !== itemToDelete.id);
@@ -112,6 +129,7 @@ export default function ParamArrayFormItem({ param }: ParamProps) {
112129
<ArrayItem
113130
param={param}
114131
onChange={handleChangeItem(item, onChange)}
132+
initialValue={item.value}
115133
/>
116134
<button
117135
className="openapi-explorer__delete-btn"

0 commit comments

Comments
 (0)