-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add KeyValue component to connector form (#1970)
- Loading branch information
Showing
12 changed files
with
315 additions
and
4 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
25 changes: 25 additions & 0 deletions
25
src/ui/units/connections/components/ConnectorForm/components/KeyValue/KeyValue.scss
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,25 @@ | ||
.conn-form-key-value { | ||
display: flex; | ||
flex-direction: column; | ||
row-gap: 10px; | ||
width: 100%; | ||
|
||
&__add-button { | ||
width: fit-content; | ||
} | ||
|
||
&__entry { | ||
display: flex; | ||
column-gap: 10px; | ||
} | ||
|
||
&__key-select { | ||
flex-shrink: 0; | ||
max-width: 40%; | ||
} | ||
|
||
&__value-input { | ||
flex-shrink: 1; | ||
flex-basis: auto; | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
src/ui/units/connections/components/ConnectorForm/components/KeyValue/KeyValue.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,110 @@ | ||
import React from 'react'; | ||
|
||
import {Plus, Xmark} from '@gravity-ui/icons'; | ||
import {Button, Icon, PasswordInput, Select, TextInput} from '@gravity-ui/uikit'; | ||
import block from 'bem-cn-lite'; | ||
import type {KeyValueItem} from 'shared/schema/types'; | ||
|
||
import {i18n10647} from '../../../../constants'; | ||
|
||
import {useKeyValueProps, useKeyValueState} from './hooks'; | ||
import type {KeyValueEntry, KeyValueProps} from './types'; | ||
|
||
import './KeyValue.scss'; | ||
|
||
const b = block('conn-form-key-value'); | ||
const ICON_SIZE = 18; | ||
|
||
type KeyValueEntryViewProps = Omit<KeyValueItem, 'id' | 'name'> & { | ||
index: number; | ||
entry: KeyValueEntry; | ||
onDelete: (index: number) => void; | ||
onUpdate: (index: number, updates: Partial<KeyValueEntry>) => void; | ||
}; | ||
|
||
const KeyValueEntryView = (props: KeyValueEntryViewProps) => { | ||
const {index, keys, entry, keySelectProps, valueInputProps, secret, onDelete, onUpdate} = props; | ||
let placeholder = valueInputProps?.placeholder; | ||
|
||
if (entry.initial && secret && !placeholder) { | ||
placeholder = i18n10647['label_secret-value']; | ||
} | ||
|
||
if (entry.value === null) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div className={b('entry')}> | ||
<Select | ||
{...keySelectProps} | ||
className={b('key-select')} | ||
options={keys} | ||
value={[entry.key]} | ||
onUpdate={(value) => { | ||
onUpdate(index, {key: value[0]}); | ||
}} | ||
validationState={entry.error ? 'invalid' : undefined} | ||
errorMessage={i18n10647['label_duplicated-keys']} | ||
/> | ||
{secret ? ( | ||
<PasswordInput | ||
{...valueInputProps} | ||
className={b('value-input')} | ||
value={entry.value} | ||
hideCopyButton={true} | ||
placeholder={placeholder} | ||
onUpdate={(value) => { | ||
onUpdate(index, {value}); | ||
}} | ||
/> | ||
) : ( | ||
<TextInput | ||
{...valueInputProps} | ||
className={b('value-input')} | ||
value={entry.value} | ||
onUpdate={(value) => { | ||
onUpdate(index, {value}); | ||
}} | ||
/> | ||
)} | ||
<Button view="flat" onClick={() => onDelete(index)}> | ||
<Icon data={Xmark} size={ICON_SIZE} /> | ||
</Button> | ||
</div> | ||
); | ||
}; | ||
|
||
export const KeyValue = (props: KeyValueProps) => { | ||
const {keys = [], keySelectProps, valueInputProps, secret} = props; | ||
const {value, updateForm} = useKeyValueProps(props); | ||
const {keyValues, handleAddKeyValue, handleUpdateKeyValue, handleDeleteKeyValue} = | ||
useKeyValueState({ | ||
value, | ||
updateForm, | ||
}); | ||
|
||
return ( | ||
<div className={b()}> | ||
{keyValues.map((item, index) => { | ||
return ( | ||
<KeyValueEntryView | ||
key={`${item.key}-${index}`} | ||
index={index} | ||
keys={keys} | ||
keySelectProps={keySelectProps} | ||
valueInputProps={valueInputProps} | ||
entry={item} | ||
secret={secret} | ||
onDelete={handleDeleteKeyValue} | ||
onUpdate={handleUpdateKeyValue} | ||
/> | ||
); | ||
})} | ||
<Button className={b('add-button')} onClick={handleAddKeyValue}> | ||
<Icon data={Plus} size={ICON_SIZE} /> | ||
{i18n10647['button_add']} | ||
</Button> | ||
</div> | ||
); | ||
}; |
139 changes: 139 additions & 0 deletions
139
src/ui/units/connections/components/ConnectorForm/components/KeyValue/hooks.ts
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,139 @@ | ||
import React from 'react'; | ||
|
||
import {batch, useDispatch, useSelector} from 'react-redux'; | ||
|
||
import {ValidationErrorType} from '../../../../constants'; | ||
import { | ||
changeForm, | ||
changeInnerForm, | ||
formSelector, | ||
innerFormSelector, | ||
setValidationErrors, | ||
validationErrorsSelector, | ||
} from '../../../../store'; | ||
import type {ValidationError} from '../../../../typings'; | ||
import {getValidationError} from '../../../../utils'; | ||
|
||
import type {KeyValueEntry, KeyValueProps} from './types'; | ||
|
||
type KeyValueResult = { | ||
entries: Record<string, KeyValueEntry['value']>; | ||
}; | ||
|
||
const initialEntriesToKeyValues = (entries: KeyValueResult['entries'] = {}): KeyValueEntry[] => { | ||
return Object.entries(entries).map(([key, value]) => { | ||
return {key, value, initial: true}; | ||
}); | ||
}; | ||
|
||
const keyValuesToEntries = (keyValues: KeyValueEntry[] = []): KeyValueResult['entries'] => { | ||
return keyValues.reduce<KeyValueResult['entries']>( | ||
(acc, {key, value, error, initial, touched}) => { | ||
if (key && !error && (!initial || (initial && touched))) { | ||
acc[key] = value; | ||
} | ||
return acc; | ||
}, | ||
{}, | ||
); | ||
}; | ||
|
||
const getValidatedKeyValues = (keyValues: KeyValueEntry[]) => { | ||
return keyValues.map((item, index) => { | ||
const resultItem = {...item}; | ||
const hasDuplicatedKey = keyValues.some( | ||
({key}, innerIndex) => innerIndex !== index && key && key === item.key, | ||
); | ||
if (hasDuplicatedKey) { | ||
resultItem.error = 'duplicated-key'; | ||
} else { | ||
resultItem.error = undefined; | ||
} | ||
return resultItem; | ||
}); | ||
}; | ||
|
||
export function useKeyValueProps(props: KeyValueProps) { | ||
const {name, inner, keys, keySelectProps, valueInputProps, secret} = props; | ||
const dispatch = useDispatch(); | ||
const form = useSelector(formSelector); | ||
const innerForm = useSelector(innerFormSelector); | ||
const validationErrors = useSelector(validationErrorsSelector); | ||
const value = (inner ? innerForm[name] : form[name]) as KeyValueResult | undefined; | ||
const error = getValidationError(name, validationErrors); | ||
|
||
const updateForm = (nextKeyValues: KeyValueEntry[]) => { | ||
const validatedNextKeyValues = getValidatedKeyValues(nextKeyValues); | ||
const formUpdates: KeyValueResult = { | ||
entries: keyValuesToEntries(validatedNextKeyValues), | ||
}; | ||
|
||
batch(() => { | ||
if (inner) { | ||
dispatch(changeInnerForm({[name]: formUpdates})); | ||
} else { | ||
dispatch(changeForm({[name]: formUpdates})); | ||
} | ||
|
||
const hasErrors = validatedNextKeyValues.some((keyValue) => Boolean(keyValue.error)); | ||
|
||
if (hasErrors) { | ||
const errors: ValidationError[] = [ | ||
...validationErrors, | ||
{type: ValidationErrorType.DuplicatedKey, name}, | ||
]; | ||
dispatch(setValidationErrors({errors})); | ||
} else if (error) { | ||
const errors = validationErrors.filter((err) => err.name !== error.name); | ||
dispatch(setValidationErrors({errors})); | ||
} | ||
}); | ||
}; | ||
|
||
return {value, keys, keySelectProps, valueInputProps, secret, updateForm}; | ||
} | ||
|
||
export function useKeyValueState(props: { | ||
value: KeyValueResult | undefined; | ||
updateForm: (nextKeyValues: KeyValueEntry[]) => void; | ||
}) { | ||
const {value, updateForm} = props; | ||
const [keyValues, setKeyValues] = React.useState<KeyValueEntry[]>( | ||
initialEntriesToKeyValues(value?.entries), | ||
); | ||
|
||
const updateKeyValues = (nextKeyValues: KeyValueEntry[]) => { | ||
const validatedNextKeyValues = getValidatedKeyValues(nextKeyValues); | ||
updateForm(validatedNextKeyValues); | ||
setKeyValues(validatedNextKeyValues); | ||
}; | ||
|
||
const handleAddKeyValue = () => { | ||
updateKeyValues([...keyValues, {key: '', value: ''}]); | ||
}; | ||
|
||
const handleUpdateKeyValue = (index: number, updates: Partial<KeyValueEntry>) => { | ||
const nextKeyValues = [...keyValues]; | ||
const updatedKeyValue = nextKeyValues[index]; | ||
nextKeyValues[index] = { | ||
...updatedKeyValue, | ||
...updates, | ||
touched: true, | ||
}; | ||
updateKeyValues(nextKeyValues); | ||
}; | ||
|
||
const handleDeleteKeyValue = (index: number) => { | ||
const deletedItem = keyValues[index]; | ||
const nextKeyValues: KeyValueEntry[] = deletedItem.initial | ||
? [ | ||
...keyValues.slice(0, index), | ||
{key: deletedItem.key, value: null}, | ||
...keyValues.slice(index + 1), | ||
] | ||
: [...keyValues.slice(0, index), ...keyValues.slice(index + 1)]; | ||
updateKeyValues(nextKeyValues); | ||
}; | ||
|
||
return {keyValues, handleAddKeyValue, handleUpdateKeyValue, handleDeleteKeyValue}; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/ui/units/connections/components/ConnectorForm/components/KeyValue/types.ts
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,11 @@ | ||
import type {KeyValueItem} from 'shared/schema/types'; | ||
|
||
export type KeyValueProps = Omit<KeyValueItem, 'id'>; | ||
|
||
export type KeyValueEntry = { | ||
key: string; | ||
value: string | null; | ||
error?: 'duplicated-key'; | ||
initial?: boolean; | ||
touched?: boolean; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export enum ValidationErrorType { | ||
Required = 'required', | ||
Length = 'length', | ||
DuplicatedKey = 'duplicated-key', | ||
} |
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