generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add headers and query params input form
- Loading branch information
1 parent
86b4cf0
commit 12b14a3
Showing
11 changed files
with
234 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
frontend/console/src/features/verbs/KeyValuePairForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { CheckmarkSquare01Icon, Delete03Icon, SquareIcon } from 'hugeicons-react' | ||
import { useEffect, useRef } from 'react' | ||
|
||
interface KeyValuePair { | ||
id: string | ||
enabled: boolean | ||
key: string | ||
value: string | ||
} | ||
|
||
interface KeyValuePairFormProps { | ||
keyValuePairs: KeyValuePair[] | ||
onChange: (pairs: KeyValuePair[]) => void | ||
} | ||
|
||
export const KeyValuePairForm = ({ keyValuePairs, onChange }: KeyValuePairFormProps) => { | ||
const inputRefs = useRef<{ [key: string]: HTMLInputElement | null }>({}) | ||
|
||
useEffect(() => { | ||
if (keyValuePairs.length === 0) { | ||
onChange([{ id: crypto.randomUUID(), enabled: false, key: '', value: '' }]) | ||
} | ||
}, []) | ||
|
||
const updatePair = (id: string, updates: Partial<KeyValuePair>) => { | ||
const updatedPairs = keyValuePairs.map((p) => { | ||
if (p.id === id) { | ||
const updatedPair = { ...p, ...updates } | ||
if ('key' in updates) { | ||
updatedPair.enabled = (updates.key?.length ?? 0) > 0 | ||
} | ||
return updatedPair | ||
} | ||
return p | ||
}) | ||
|
||
const filteredPairs = updatedPairs.filter((p, index) => index === updatedPairs.length - 1 || !(p.key === '' && p.value === '')) | ||
|
||
const lastPair = filteredPairs[filteredPairs.length - 1] | ||
if (lastPair?.key || lastPair?.value) { | ||
const newId = crypto.randomUUID() | ||
filteredPairs.push({ id: newId, enabled: false, key: '', value: '' }) | ||
} | ||
|
||
onChange(filteredPairs) | ||
|
||
// Focus the key input of the last empty row after deletion | ||
if ('key' in updates && updates.key === '') { | ||
setTimeout(() => { | ||
const lastPairId = filteredPairs[filteredPairs.length - 1].id | ||
inputRefs.current[lastPairId]?.focus() | ||
}, 0) | ||
} | ||
} | ||
|
||
return ( | ||
<div className='p-4'> | ||
<div className='space-y-2'> | ||
{keyValuePairs.map((pair, index) => ( | ||
<div key={pair.id} className='flex items-center gap-2'> | ||
<button | ||
type='button' | ||
onClick={() => updatePair(pair.id, { enabled: !pair.enabled })} | ||
className='text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-300 flex-shrink-0' | ||
> | ||
{pair.enabled ? ( | ||
<CheckmarkSquare01Icon className='h-5 w-5 text-indigo-500 dark:text-indigo-400' /> | ||
) : ( | ||
<SquareIcon className='h-5 w-5 dark:text-gray-600' /> | ||
)} | ||
</button> | ||
<input | ||
ref={(el) => { | ||
inputRefs.current[pair.id] = el | ||
}} | ||
type='text' | ||
value={pair.key} | ||
onChange={(e) => updatePair(pair.id, { key: e.target.value })} | ||
placeholder='Key' | ||
className='flex-1 block rounded-md border-0 py-1.5 text-gray-900 dark:text-white shadow-sm ring-1 ring-inset ring-gray-300 dark:ring-gray-700 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 dark:bg-gray-800 sm:text-sm sm:leading-6' | ||
/> | ||
<input | ||
type='text' | ||
value={pair.value} | ||
onChange={(e) => updatePair(pair.id, { value: e.target.value })} | ||
placeholder='Value' | ||
className='flex-1 block rounded-md border-0 py-1.5 text-gray-900 dark:text-white shadow-sm ring-1 ring-inset ring-gray-300 dark:ring-gray-700 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 dark:bg-gray-800 sm:text-sm sm:leading-6' | ||
/> | ||
<div className='w-9 flex-shrink-0'> | ||
{(pair.key || pair.value || keyValuePairs.length > 1) && index !== keyValuePairs.length - 1 && ( | ||
<button type='button' onClick={() => updatePair(pair.id, { key: '', value: '' })} className='p-2 text-gray-400 hover:text-gray-500'> | ||
<Delete03Icon className='h-5 w-5' /> | ||
</button> | ||
)} | ||
</div> | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.