-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b96e900
commit 0fac236
Showing
6 changed files
with
296 additions
and
18 deletions.
There are no files selected for viewing
154 changes: 154 additions & 0 deletions
154
app/client/src/components/propertyControls/ArrayComponent.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,154 @@ | ||
import { Button } from "@appsmith/ads"; | ||
import { debounce } from "lodash"; | ||
import React, { useCallback, useEffect, useState } from "react"; | ||
import styled from "styled-components"; | ||
import { ControlWrapper, InputGroup } from "./StyledControls"; | ||
|
||
function updateOptionLabel<T>( | ||
items: Array<T>, | ||
index: number, | ||
updatedLabel: string, | ||
) { | ||
return items.map((option: T, optionIndex) => { | ||
if (index !== optionIndex) { | ||
return option; | ||
} | ||
|
||
return updatedLabel; | ||
}); | ||
} | ||
|
||
const StyledBox = styled.div` | ||
width: 10px; | ||
`; | ||
|
||
type UpdatePairFunction = ( | ||
pair: string[], | ||
isUpdatedViaKeyboard?: boolean, | ||
// TODO: Fix this the next time the file is edited | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
) => any; | ||
|
||
interface ArrayComponentProps { | ||
pairs: string[]; | ||
updatePairs: UpdatePairFunction; | ||
addLabel?: string; | ||
} | ||
|
||
const StyledInputGroup = styled(InputGroup)` | ||
> .ads-v2-input__input-section > div { | ||
flex: 1; | ||
min-width: 0px; | ||
} | ||
`; | ||
|
||
export function ArrayComponent(props: ArrayComponentProps) { | ||
const [renderPairs, setRenderPairs] = useState<string[]>([]); | ||
const [typing, setTyping] = useState<boolean>(false); | ||
const { pairs } = props; | ||
|
||
useEffect(() => { | ||
let { pairs } = props; | ||
|
||
pairs = Array.isArray(pairs) ? pairs.slice() : []; | ||
|
||
pairs.length !== 0 && !typing && setRenderPairs(pairs); | ||
}, [props, pairs.length, renderPairs.length, typing]); | ||
|
||
const debouncedUpdatePairs = useCallback( | ||
debounce((updatedPairs: string[]) => { | ||
props.updatePairs(updatedPairs, true); | ||
}, 200), | ||
[props.updatePairs], | ||
); | ||
|
||
function updateKey(index: number, updatedKey: string) { | ||
let { pairs } = props; | ||
|
||
pairs = Array.isArray(pairs) ? pairs : []; | ||
const updatedPairs = updateOptionLabel(pairs, index, updatedKey); | ||
const updatedRenderPairs = updateOptionLabel( | ||
renderPairs, | ||
index, | ||
updatedKey, | ||
); | ||
|
||
setRenderPairs(updatedRenderPairs); | ||
debouncedUpdatePairs(updatedPairs); | ||
} | ||
|
||
function deletePair(index: number, isUpdatedViaKeyboard = false) { | ||
let { pairs } = props; | ||
|
||
pairs = Array.isArray(pairs) ? pairs : []; | ||
|
||
const newPairs = pairs.filter((o, i) => i !== index); | ||
const newRenderPairs = renderPairs.filter((o, i) => i !== index); | ||
|
||
setRenderPairs(newRenderPairs); | ||
props.updatePairs(newPairs, isUpdatedViaKeyboard); | ||
} | ||
|
||
function addPair(e: React.MouseEvent) { | ||
let { pairs } = props; | ||
|
||
pairs = Array.isArray(pairs) ? pairs.slice() : []; | ||
|
||
pairs.push(""); | ||
|
||
const updatedRenderPairs = renderPairs.slice(); | ||
|
||
updatedRenderPairs.push(""); | ||
|
||
setRenderPairs(updatedRenderPairs); | ||
props.updatePairs(pairs, e.detail === 0); | ||
} | ||
|
||
function onInputFocus() { | ||
setTyping(true); | ||
} | ||
|
||
function onInputBlur() { | ||
setTyping(false); | ||
} | ||
|
||
return ( | ||
<> | ||
{renderPairs.map((pair: string, index) => { | ||
return ( | ||
<ControlWrapper key={index} orientation={"HORIZONTAL"}> | ||
<StyledInputGroup | ||
dataType={"text"} | ||
onBlur={onInputBlur} | ||
onChange={(value: string) => updateKey(index, value)} | ||
onFocus={onInputFocus} | ||
value={pair} | ||
/> | ||
<StyledBox /> | ||
<Button | ||
isIconButton | ||
kind="tertiary" | ||
onClick={(e: React.MouseEvent) => | ||
deletePair(index, e.detail === 0) | ||
} | ||
size="sm" | ||
startIcon="delete-bin-line" | ||
/> | ||
</ControlWrapper> | ||
); | ||
})} | ||
|
||
<div className="flex flex-row-reverse mt-1"> | ||
<Button | ||
className="t--property-control-options-add" | ||
kind="tertiary" | ||
onClick={addPair} | ||
size="sm" | ||
startIcon="plus" | ||
> | ||
{props.addLabel || "Add suggestion"} | ||
</Button> | ||
</div> | ||
</> | ||
); | ||
} |
50 changes: 50 additions & 0 deletions
50
app/client/src/components/propertyControls/ArrayControl.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,50 @@ | ||
import { objectKeys } from "@appsmith/utils"; | ||
import type { DropdownOption } from "components/constants"; | ||
import React from "react"; | ||
import { isDynamicValue } from "utils/DynamicBindingUtils"; | ||
import { ArrayComponent } from "./ArrayComponent"; | ||
import type { ControlData, ControlProps } from "./BaseControl"; | ||
import BaseControl from "./BaseControl"; | ||
|
||
class ArrayControl extends BaseControl<ControlProps> { | ||
render() { | ||
return ( | ||
<ArrayComponent | ||
pairs={this.props.propertyValue} | ||
updatePairs={this.updateItems} | ||
/> | ||
); | ||
} | ||
|
||
updateItems = (items: string[], isUpdatedViaKeyboard = false) => { | ||
this.updateProperty(this.props.propertyName, items, isUpdatedViaKeyboard); | ||
}; | ||
|
||
static getControlType() { | ||
return "ARRAY_INPUT"; | ||
} | ||
|
||
// TODO: Fix this the next time the file is edited | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
static canDisplayValueInUI(config: ControlData, value: any): boolean { | ||
if (isDynamicValue(value)) return false; | ||
|
||
try { | ||
const pairs: DropdownOption[] = JSON.parse(value); | ||
|
||
for (const x of pairs) { | ||
const keys = objectKeys(x); | ||
|
||
if (!keys.includes("label") || !keys.includes("value")) { | ||
return false; | ||
} | ||
} | ||
} catch { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
|
||
export default ArrayControl; |
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