in karavan-space/src/designer/property/property/KameletPropertyField.tsx [53:204]
export function KameletPropertyField(props: Props) {
const {onParametersChange} = usePropertiesHook();
const [dark] = useDesignerStore((s) => [s.dark], shallow)
const [showEditor, setShowEditor] = useState<boolean>(false);
const [infrastructureSelector, setInfrastructureSelector] = useState<boolean>(false);
const [infrastructureSelectorProperty, setInfrastructureSelectorProperty] = useState<string | undefined>(undefined);
const [selectStatus, setSelectStatus] = useState<Map<string, boolean>>(new Map<string, boolean>());
const ref = useRef<any>(null);
const [textValue, setTextValue] = useState<any>();
const [checkChanges, setCheckChanges] = useState<boolean>(false);
useEffect(()=> setTextValue(value), [])
useEffect(()=> {
if (checkChanges) {
const interval = setInterval(() => {
if (props.value !== textValue) {
onParametersChange(property.id, textValue);
}
}, 700);
return () => {
clearInterval(interval)
}
}
}, [checkChanges, textValue])
function parametersChanged (parameter: string, value: string | number | boolean | any, pathParameter?: boolean) {
setCheckChanges(false);
onParametersChange(parameter, value, pathParameter);
setSelectStatus(new Map<string, boolean>([[parameter, false]]))
}
function openSelect(propertyName: string, isExpanded: boolean) {
setSelectStatus(new Map<string, boolean>([[propertyName, isExpanded]]))
}
function isSelectOpen(propertyName: string): boolean {
return selectStatus.has(propertyName) && selectStatus.get(propertyName) === true;
}
function selectInfrastructure (value: string) {
// check if there is a selection
const textVal = ref.current;
const cursorStart = textVal.selectionStart;
const cursorEnd = textVal.selectionEnd;
if (cursorStart !== cursorEnd){
const prevValue = props.value;
const selectedText = prevValue.substring(cursorStart, cursorEnd)
value = prevValue.replace(selectedText, value);
}
const propertyId = infrastructureSelectorProperty;
if (propertyId){
if (value.startsWith("config") || value.startsWith("secret")) value = "{{" + value + "}}";
setTextValue(value);
parametersChanged(propertyId, value);
setInfrastructureSelector(false);
setInfrastructureSelectorProperty(undefined);
}
}
function openInfrastructureSelector (propertyName: string) {
setInfrastructureSelector(true);
setInfrastructureSelectorProperty(propertyName);
}
function getInfrastructureSelectorModal() {
return (
<InfrastructureSelector
dark={false}
isOpen={infrastructureSelector}
onClose={() => setInfrastructureSelector(false)}
onSelect={selectInfrastructure}/>)
}
function getSpecialStringInput() {
const {property, value} = props;
const prefix = "parameters";
const id = prefix + "-" + property.id;
const inInfrastructure = InfrastructureAPI.infrastructure !== 'local';
const noInfraSelectorButton = ["uri", "id", "description", "group"].includes(property.id);
const icon = InfrastructureAPI.infrastructure === 'kubernetes' ? KubernetesIcon("infra-button") : <DockerIcon/>
const showInfraSelectorButton = inInfrastructure && !showEditor && !noInfraSelectorButton;
const showEditorButton = property.type === 'string' && property.format !== "password";
const selectFromList: boolean = property.enum !== undefined && property?.enum?.length > 0;
const selectOptions: JSX.Element[] = [];
if (selectFromList && property.enum) {
selectOptions.push(...property.enum.map((value: string) =>
<SelectOption key={value} value={value ? value.trim() : value}/>));
}
return <InputGroup className={valueChangedClassName}>
{showInfraSelectorButton &&
<Tooltip position="bottom-end" content={"Select from " + capitalize(InfrastructureAPI.infrastructure)}>
<Button variant="control" onClick={e => openInfrastructureSelector(property.id)}>
{icon}
</Button>
</Tooltip>}
{selectFromList &&
<Select
id={id} name={id}
placeholderText="Select or type an URI"
variant={SelectVariant.typeahead}
aria-label={property.id}
onToggle={(_event, isExpanded) => {
openSelect(property.id, isExpanded)
}}
onSelect={(e, value, isPlaceholder) => {
parametersChanged(property.id, value);
setCheckChanges(false);
}}
selections={value}
isOpen={isSelectOpen(property.id)}
isCreatable={true}
createText=""
isInputFilterPersisted={true}
aria-labelledby={property.id}
direction={SelectDirection.down}>
{selectOptions}
</Select>
}
{((!selectFromList && !showEditor) || property.format === "password") &&
<TextInput
ref={ref}
className="text-field" isRequired
type='text'
validated={validated}
autoComplete="off"
id={id} name={id}
value={textValue || ''}
onBlur={_ => {
if (isNumeric((textValue))) {
parametersChanged(property.id, Number(textValue))
} else {
parametersChanged(property.id, textValue)
}
}}
onChange={(_, v) => {
setTextValue(v);
setCheckChanges(true);
}}
customIcon={property.type !== 'string' ?
<Text component={TextVariants.p}>{property.type}</Text> : undefined}
/>
}
{showEditorButton && <InputGroupItem>
<Tooltip position="bottom-end" content={"Show Editor"}>
<Button variant="control" onClick={e => setShowEditor(!showEditor)}>
<EditorIcon/>
</Button>
</Tooltip>
</InputGroupItem>}