2021-12-27 12:04:45 +00:00
|
|
|
import React, { useEffect, useCallback } from "react";
|
chore: upgrade to prettier v2 + enforce import types (#21013)Co-authored-by: Satish Gandham <hello@satishgandham.com> Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
## Description
This PR upgrades Prettier to v2 + enforces TypeScript’s [`import
type`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export)
syntax where applicable. It’s submitted as a separate PR so we can merge
it easily.
As a part of this PR, we reformat the codebase heavily:
- add `import type` everywhere where it’s required, and
- re-format the code to account for Prettier 2’s breaking changes:
https://prettier.io/blog/2020/03/21/2.0.0.html#breaking-changes
This PR is submitted against `release` to make sure all new code by team
members will adhere to new formatting standards, and we’ll have fewer
conflicts when merging `bundle-optimizations` into `release`. (I’ll
merge `release` back into `bundle-optimizations` once this PR is
merged.)
### Why is this needed?
This PR is needed because, for the Lodash optimization from
https://github.com/appsmithorg/appsmith/commit/7cbb12af886621256224be0c93e6a465dd710ad3,
we need to use `import type`. Otherwise, `babel-plugin-lodash` complains
that `LoDashStatic` is not a lodash function.
However, just using `import type` in the current codebase will give you
this:
<img width="962" alt="Screenshot 2023-03-08 at 17 45 59"
src="https://user-images.githubusercontent.com/2953267/223775744-407afa0c-e8b9-44a1-90f9-b879348da57f.png">
That’s because Prettier 1 can’t parse `import type` at all. To parse it,
we need to upgrade to Prettier 2.
### Why enforce `import type`?
Apart from just enabling `import type` support, this PR enforces
specifying `import type` everywhere it’s needed. (Developers will get
immediate TypeScript and ESLint errors when they forget to do so.)
I’m doing this because I believe `import type` improves DX and makes
refactorings easier.
Let’s say you had a few imports like below. Can you tell which of these
imports will increase the bundle size? (Tip: it’s not all of them!)
```ts
// app/client/src/workers/Linting/utils.ts
import { Position } from "codemirror";
import { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
It’s pretty hard, right?
What about now?
```ts
// app/client/src/workers/Linting/utils.ts
import type { Position } from "codemirror";
import type { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
Now, it’s clear that only `lodash` will be bundled.
This helps developers to see which imports are problematic, but it
_also_ helps with refactorings. Now, if you want to see where
`codemirror` is bundled, you can just grep for `import \{.*\} from
"codemirror"` – and you won’t get any type-only imports.
This also helps (some) bundlers. Upon transpiling, TypeScript erases
type-only imports completely. In some environment (not ours), this makes
the bundle smaller, as the bundler doesn’t need to bundle type-only
imports anymore.
## Type of change
- Chore (housekeeping or task changes that don't impact user perception)
## How Has This Been Tested?
This was tested to not break the build.
### Test Plan
> Add Testsmith test cases links that relate to this PR
### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)
## Checklist:
### Dev activity
- [x] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag
### QA activity:
- [ ] Test plan has been approved by relevant developers
- [ ] Test plan has been peer reviewed by QA
- [ ] Cypress test cases have been added and approved by either SDET or
manual QA
- [ ] Organized project review call with relevant stakeholders after
Round 1/2 of QA
- [ ] Added Test Plan Approved label after reveiwing all Cypress test
---------
Co-authored-by: Satish Gandham <hello@satishgandham.com>
Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
2023-03-16 11:41:47 +00:00
|
|
|
import type {
|
2021-12-27 12:04:45 +00:00
|
|
|
WrappedFieldArrayProps,
|
|
|
|
|
WrappedFieldMetaProps,
|
|
|
|
|
WrappedFieldInputProps,
|
|
|
|
|
} from "redux-form";
|
chore: upgrade to prettier v2 + enforce import types (#21013)Co-authored-by: Satish Gandham <hello@satishgandham.com> Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
## Description
This PR upgrades Prettier to v2 + enforces TypeScript’s [`import
type`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export)
syntax where applicable. It’s submitted as a separate PR so we can merge
it easily.
As a part of this PR, we reformat the codebase heavily:
- add `import type` everywhere where it’s required, and
- re-format the code to account for Prettier 2’s breaking changes:
https://prettier.io/blog/2020/03/21/2.0.0.html#breaking-changes
This PR is submitted against `release` to make sure all new code by team
members will adhere to new formatting standards, and we’ll have fewer
conflicts when merging `bundle-optimizations` into `release`. (I’ll
merge `release` back into `bundle-optimizations` once this PR is
merged.)
### Why is this needed?
This PR is needed because, for the Lodash optimization from
https://github.com/appsmithorg/appsmith/commit/7cbb12af886621256224be0c93e6a465dd710ad3,
we need to use `import type`. Otherwise, `babel-plugin-lodash` complains
that `LoDashStatic` is not a lodash function.
However, just using `import type` in the current codebase will give you
this:
<img width="962" alt="Screenshot 2023-03-08 at 17 45 59"
src="https://user-images.githubusercontent.com/2953267/223775744-407afa0c-e8b9-44a1-90f9-b879348da57f.png">
That’s because Prettier 1 can’t parse `import type` at all. To parse it,
we need to upgrade to Prettier 2.
### Why enforce `import type`?
Apart from just enabling `import type` support, this PR enforces
specifying `import type` everywhere it’s needed. (Developers will get
immediate TypeScript and ESLint errors when they forget to do so.)
I’m doing this because I believe `import type` improves DX and makes
refactorings easier.
Let’s say you had a few imports like below. Can you tell which of these
imports will increase the bundle size? (Tip: it’s not all of them!)
```ts
// app/client/src/workers/Linting/utils.ts
import { Position } from "codemirror";
import { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
It’s pretty hard, right?
What about now?
```ts
// app/client/src/workers/Linting/utils.ts
import type { Position } from "codemirror";
import type { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
Now, it’s clear that only `lodash` will be bundled.
This helps developers to see which imports are problematic, but it
_also_ helps with refactorings. Now, if you want to see where
`codemirror` is bundled, you can just grep for `import \{.*\} from
"codemirror"` – and you won’t get any type-only imports.
This also helps (some) bundlers. Upon transpiling, TypeScript erases
type-only imports completely. In some environment (not ours), this makes
the bundle smaller, as the bundler doesn’t need to bundle type-only
imports anymore.
## Type of change
- Chore (housekeeping or task changes that don't impact user perception)
## How Has This Been Tested?
This was tested to not break the build.
### Test Plan
> Add Testsmith test cases links that relate to this PR
### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)
## Checklist:
### Dev activity
- [x] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag
### QA activity:
- [ ] Test plan has been approved by relevant developers
- [ ] Test plan has been peer reviewed by QA
- [ ] Cypress test cases have been added and approved by either SDET or
manual QA
- [ ] Organized project review call with relevant stakeholders after
Round 1/2 of QA
- [ ] Added Test Plan Approved label after reveiwing all Cypress test
---------
Co-authored-by: Satish Gandham <hello@satishgandham.com>
Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
2023-03-16 11:41:47 +00:00
|
|
|
import { Field, FieldArray } from "redux-form";
|
2020-04-28 06:52:53 +00:00
|
|
|
import styled from "styled-components";
|
chore: upgrade to prettier v2 + enforce import types (#21013)Co-authored-by: Satish Gandham <hello@satishgandham.com> Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
## Description
This PR upgrades Prettier to v2 + enforces TypeScript’s [`import
type`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export)
syntax where applicable. It’s submitted as a separate PR so we can merge
it easily.
As a part of this PR, we reformat the codebase heavily:
- add `import type` everywhere where it’s required, and
- re-format the code to account for Prettier 2’s breaking changes:
https://prettier.io/blog/2020/03/21/2.0.0.html#breaking-changes
This PR is submitted against `release` to make sure all new code by team
members will adhere to new formatting standards, and we’ll have fewer
conflicts when merging `bundle-optimizations` into `release`. (I’ll
merge `release` back into `bundle-optimizations` once this PR is
merged.)
### Why is this needed?
This PR is needed because, for the Lodash optimization from
https://github.com/appsmithorg/appsmith/commit/7cbb12af886621256224be0c93e6a465dd710ad3,
we need to use `import type`. Otherwise, `babel-plugin-lodash` complains
that `LoDashStatic` is not a lodash function.
However, just using `import type` in the current codebase will give you
this:
<img width="962" alt="Screenshot 2023-03-08 at 17 45 59"
src="https://user-images.githubusercontent.com/2953267/223775744-407afa0c-e8b9-44a1-90f9-b879348da57f.png">
That’s because Prettier 1 can’t parse `import type` at all. To parse it,
we need to upgrade to Prettier 2.
### Why enforce `import type`?
Apart from just enabling `import type` support, this PR enforces
specifying `import type` everywhere it’s needed. (Developers will get
immediate TypeScript and ESLint errors when they forget to do so.)
I’m doing this because I believe `import type` improves DX and makes
refactorings easier.
Let’s say you had a few imports like below. Can you tell which of these
imports will increase the bundle size? (Tip: it’s not all of them!)
```ts
// app/client/src/workers/Linting/utils.ts
import { Position } from "codemirror";
import { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
It’s pretty hard, right?
What about now?
```ts
// app/client/src/workers/Linting/utils.ts
import type { Position } from "codemirror";
import type { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
Now, it’s clear that only `lodash` will be bundled.
This helps developers to see which imports are problematic, but it
_also_ helps with refactorings. Now, if you want to see where
`codemirror` is bundled, you can just grep for `import \{.*\} from
"codemirror"` – and you won’t get any type-only imports.
This also helps (some) bundlers. Upon transpiling, TypeScript erases
type-only imports completely. In some environment (not ours), this makes
the bundle smaller, as the bundler doesn’t need to bundle type-only
imports anymore.
## Type of change
- Chore (housekeeping or task changes that don't impact user perception)
## How Has This Been Tested?
This was tested to not break the build.
### Test Plan
> Add Testsmith test cases links that relate to this PR
### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)
## Checklist:
### Dev activity
- [x] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag
### QA activity:
- [ ] Test plan has been approved by relevant developers
- [ ] Test plan has been peer reviewed by QA
- [ ] Cypress test cases have been added and approved by either SDET or
manual QA
- [ ] Organized project review call with relevant stakeholders after
Round 1/2 of QA
- [ ] Added Test Plan Approved label after reveiwing all Cypress test
---------
Co-authored-by: Satish Gandham <hello@satishgandham.com>
Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
2023-03-16 11:41:47 +00:00
|
|
|
import type { ControlProps, ControlData } from "./BaseControl";
|
|
|
|
|
import BaseControl from "./BaseControl";
|
|
|
|
|
import type { ControlType } from "constants/PropertyControlConstants";
|
2020-04-28 06:52:53 +00:00
|
|
|
import DynamicTextField from "components/editorComponents/form/fields/DynamicTextField";
|
2024-08-09 14:20:29 +00:00
|
|
|
import type { InputProps } from "@appsmith/ads";
|
2022-12-30 10:23:24 +00:00
|
|
|
import { setDefaultKeyValPairFlag } from "actions/datasourceActions";
|
|
|
|
|
import { useDispatch } from "react-redux";
|
2024-08-09 14:20:29 +00:00
|
|
|
import { Button, Icon, Input, Text, Tooltip } from "@appsmith/ads";
|
2021-12-27 12:04:45 +00:00
|
|
|
export interface KeyValueArrayControlProps extends ControlProps {
|
|
|
|
|
name: string;
|
|
|
|
|
label: string;
|
|
|
|
|
maxLen?: number;
|
|
|
|
|
description?: string;
|
2024-07-31 15:41:28 +00:00
|
|
|
// TODO: Fix this the next time the file is edited
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2021-12-27 12:04:45 +00:00
|
|
|
actionConfig?: any;
|
|
|
|
|
extraData?: ControlData[];
|
|
|
|
|
isRequired?: boolean;
|
2023-06-14 07:27:57 +00:00
|
|
|
showHeader?: boolean;
|
2023-06-14 14:09:38 +00:00
|
|
|
headerTooltips?: {
|
|
|
|
|
key?: string;
|
|
|
|
|
value?: string;
|
|
|
|
|
};
|
2021-12-27 12:04:45 +00:00
|
|
|
}
|
2020-04-28 06:52:53 +00:00
|
|
|
|
|
|
|
|
const FormRowWithLabel = styled.div`
|
|
|
|
|
display: flex;
|
|
|
|
|
flex: 1;
|
|
|
|
|
flex-direction: row;
|
2023-05-19 18:37:06 +00:00
|
|
|
align-items: start;
|
|
|
|
|
margin-bottom: 5px;
|
2020-04-28 06:52:53 +00:00
|
|
|
& svg {
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
}
|
2023-05-19 18:37:06 +00:00
|
|
|
.form-input-field {
|
|
|
|
|
width: 270px;
|
|
|
|
|
+ .form-input-field {
|
|
|
|
|
margin-left: 5px;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-28 06:52:53 +00:00
|
|
|
`;
|
|
|
|
|
|
2023-05-19 18:37:06 +00:00
|
|
|
const StyledInput = styled(Input)`
|
2020-07-01 07:21:02 +00:00
|
|
|
input[type="number"]::-webkit-inner-spin-button,
|
|
|
|
|
input[type="number"]::-webkit-outer-spin-button {
|
2021-12-27 12:04:45 +00:00
|
|
|
margin: 0px;
|
2020-07-01 07:21:02 +00:00
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2023-05-19 18:37:06 +00:00
|
|
|
const StyledButton = styled(Button)`
|
|
|
|
|
margin-left: 5px;
|
2022-12-13 14:39:16 +00:00
|
|
|
`;
|
2023-05-19 18:37:06 +00:00
|
|
|
const AddMoreButton = styled(Button)``;
|
2022-12-13 14:39:16 +00:00
|
|
|
|
2023-06-14 07:27:57 +00:00
|
|
|
const FlexContainer = styled.div`
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
width: calc(100% - 30px);
|
2023-06-14 14:09:38 +00:00
|
|
|
margin-bottom: 8px;
|
|
|
|
|
|
2023-06-14 07:27:57 +00:00
|
|
|
.key-value {
|
2023-06-14 14:09:38 +00:00
|
|
|
line-height: 1;
|
|
|
|
|
flex: 1;
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
|
|
|
|
.ads-v2-icon {
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
margin-left: 8px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
label:first-child {
|
|
|
|
|
font-weight: normal;
|
|
|
|
|
}
|
2023-06-14 07:27:57 +00:00
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2021-12-27 12:04:45 +00:00
|
|
|
function KeyValueRow(
|
|
|
|
|
props: KeyValueArrayControlProps & WrappedFieldArrayProps,
|
|
|
|
|
) {
|
2020-04-28 06:52:53 +00:00
|
|
|
const { extraData = [] } = props;
|
2021-12-27 12:04:45 +00:00
|
|
|
const keyName = getFieldName(extraData[0]?.configProperty);
|
|
|
|
|
const valueName = getFieldName(extraData[1]?.configProperty);
|
2020-08-31 05:15:04 +00:00
|
|
|
const keyFieldProps = extraData[0];
|
2022-12-30 10:23:24 +00:00
|
|
|
const dispatch = useDispatch();
|
2020-08-31 05:15:04 +00:00
|
|
|
|
2022-12-13 14:39:16 +00:00
|
|
|
const addRow = useCallback(() => {
|
|
|
|
|
if (keyName && valueName) {
|
|
|
|
|
props.fields.push({ [keyName[1]]: "", [valueName[1]]: "" });
|
|
|
|
|
} else {
|
|
|
|
|
props.fields.push({ key: "", value: "" });
|
|
|
|
|
}
|
|
|
|
|
}, [keyName, valueName]);
|
|
|
|
|
|
2020-04-28 06:52:53 +00:00
|
|
|
useEffect(() => {
|
2020-05-19 07:03:11 +00:00
|
|
|
// Always maintain 1 row
|
|
|
|
|
if (props.fields.length < 1) {
|
|
|
|
|
for (let i = props.fields.length; i < 1; i += 1) {
|
2022-12-13 14:39:16 +00:00
|
|
|
addRow();
|
2022-12-30 10:23:24 +00:00
|
|
|
// Since we are initializing one default key value pair, it needs to stored in redux store
|
|
|
|
|
// so that it can be used to initilize datasource config form as well
|
|
|
|
|
dispatch(setDefaultKeyValPairFlag(props.configProperty));
|
2020-04-28 06:52:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
2020-06-04 13:49:22 +00:00
|
|
|
}, [props.fields, keyName, valueName]);
|
2020-04-28 06:52:53 +00:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (typeof props.fields.getAll() === "string") {
|
2024-07-31 15:41:28 +00:00
|
|
|
// TODO: Fix this the next time the file is edited
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2020-10-12 13:06:05 +00:00
|
|
|
const fieldsValue: any[] = JSON.parse(`${props.fields.getAll()}`);
|
2024-09-18 16:35:28 +00:00
|
|
|
|
2020-04-28 06:52:53 +00:00
|
|
|
props.fields.removeAll();
|
|
|
|
|
fieldsValue.forEach((value, index) => {
|
|
|
|
|
props.fields.insert(index, value);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, [props.fields]);
|
|
|
|
|
|
2023-05-19 18:37:06 +00:00
|
|
|
const isKeyFieldValid = useCallback(
|
2020-08-31 05:15:04 +00:00
|
|
|
(value: string) => {
|
2021-12-27 12:04:45 +00:00
|
|
|
if (value && keyFieldProps?.validationRegex) {
|
|
|
|
|
const regex = new RegExp(keyFieldProps?.validationRegex);
|
2020-08-31 05:15:04 +00:00
|
|
|
|
2022-05-18 17:35:03 +00:00
|
|
|
return regex.test(value)
|
|
|
|
|
? { isValid: true }
|
|
|
|
|
: { isValid: false, message: keyFieldProps.validationMessage };
|
2020-08-31 05:15:04 +00:00
|
|
|
}
|
2024-09-18 16:35:28 +00:00
|
|
|
|
2023-05-19 18:37:06 +00:00
|
|
|
return { isValid: true };
|
2020-08-31 05:15:04 +00:00
|
|
|
},
|
2021-12-27 12:04:45 +00:00
|
|
|
[keyFieldProps?.validationRegex, keyFieldProps?.validationMessage],
|
2020-08-31 05:15:04 +00:00
|
|
|
);
|
2020-04-28 06:52:53 +00:00
|
|
|
|
2021-04-28 10:28:39 +00:00
|
|
|
return typeof props.fields.getAll() === "object" ? (
|
|
|
|
|
<>
|
2023-06-14 07:27:57 +00:00
|
|
|
{props.showHeader && (
|
|
|
|
|
<FlexContainer>
|
2023-06-14 14:09:38 +00:00
|
|
|
<div className="key-value">
|
|
|
|
|
<Text kind="body-m" renderAs="label">
|
|
|
|
|
Key
|
|
|
|
|
</Text>
|
|
|
|
|
{props.headerTooltips && (
|
|
|
|
|
<Tooltip
|
|
|
|
|
content={props.headerTooltips.key}
|
|
|
|
|
placement="right"
|
|
|
|
|
trigger="hover"
|
|
|
|
|
>
|
|
|
|
|
<Icon
|
|
|
|
|
className={"help-icon"}
|
|
|
|
|
color="var(--ads-v2-color-fg)"
|
|
|
|
|
name="question-line"
|
|
|
|
|
size="md"
|
|
|
|
|
/>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
<div className="key-value">
|
|
|
|
|
<Text kind="body-m" renderAs="label">
|
|
|
|
|
Value
|
|
|
|
|
</Text>
|
|
|
|
|
{props.headerTooltips && (
|
|
|
|
|
<Tooltip
|
|
|
|
|
content={props.headerTooltips.value}
|
|
|
|
|
placement="right"
|
|
|
|
|
trigger="hover"
|
|
|
|
|
>
|
|
|
|
|
<Icon
|
|
|
|
|
className={"help-icon"}
|
|
|
|
|
color="var(--ads-v2-color-fg)"
|
|
|
|
|
name="question-line"
|
|
|
|
|
size="md"
|
|
|
|
|
/>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2023-06-14 07:27:57 +00:00
|
|
|
</FlexContainer>
|
|
|
|
|
)}
|
2024-07-31 15:41:28 +00:00
|
|
|
{/* TODO: Fix this the next time the file is edited */}
|
|
|
|
|
{/* eslint-disable-next-line @typescript-eslint/no-explicit-any */}
|
2021-04-28 10:28:39 +00:00
|
|
|
{props.fields.map((field: any, index: number) => {
|
2021-12-27 12:04:45 +00:00
|
|
|
let keyTextFieldName = `${field}.key`;
|
|
|
|
|
let valueTextFieldName = `${field}.value`;
|
|
|
|
|
|
|
|
|
|
if (keyName && Array.isArray(keyName) && keyName?.length)
|
|
|
|
|
keyTextFieldName = `${field}.${keyName[1]}`;
|
|
|
|
|
|
|
|
|
|
if (valueName && Array.isArray(valueName) && valueName?.length)
|
|
|
|
|
valueTextFieldName = `${field}.${valueName[1]}`;
|
|
|
|
|
|
2021-04-28 10:28:39 +00:00
|
|
|
return (
|
2023-05-19 18:37:06 +00:00
|
|
|
<FormRowWithLabel key={index}>
|
2021-12-07 09:45:18 +00:00
|
|
|
<div
|
2023-05-19 18:37:06 +00:00
|
|
|
className="form-input-field"
|
2023-08-10 13:55:06 +00:00
|
|
|
data-location-id={btoa(keyTextFieldName)}
|
2021-12-07 09:45:18 +00:00
|
|
|
>
|
2021-12-27 12:04:45 +00:00
|
|
|
<Field
|
2023-05-19 18:37:06 +00:00
|
|
|
component={renderInput}
|
2021-12-27 12:04:45 +00:00
|
|
|
name={keyTextFieldName}
|
|
|
|
|
props={{
|
|
|
|
|
dataType: getType(extraData[0]?.dataType),
|
2022-03-16 14:26:57 +00:00
|
|
|
defaultValue: extraData[0]?.initialValue,
|
2023-05-19 18:37:06 +00:00
|
|
|
isKeyFieldValid: isKeyFieldValid,
|
2021-12-27 12:04:45 +00:00
|
|
|
placeholder: props.extraData
|
2023-06-16 12:34:25 +00:00
|
|
|
? props.extraData[0]?.placeholderText
|
2023-06-14 14:09:38 +00:00
|
|
|
: `Key ${index + 1}`,
|
2021-12-27 12:04:45 +00:00
|
|
|
isRequired: extraData[0]?.isRequired,
|
|
|
|
|
name: keyTextFieldName,
|
|
|
|
|
}}
|
2021-04-28 10:28:39 +00:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
{!props.actionConfig && (
|
2023-05-19 18:37:06 +00:00
|
|
|
<div className="form-input-field">
|
2021-12-27 12:04:45 +00:00
|
|
|
<div
|
2023-08-10 13:55:06 +00:00
|
|
|
data-location-id={btoa(valueTextFieldName)}
|
2021-12-27 12:04:45 +00:00
|
|
|
style={{ display: "flex", flexDirection: "row" }}
|
|
|
|
|
>
|
|
|
|
|
<Field
|
2023-05-19 18:37:06 +00:00
|
|
|
component={renderInput}
|
2021-12-27 12:04:45 +00:00
|
|
|
name={valueTextFieldName}
|
|
|
|
|
props={{
|
|
|
|
|
dataType: getType(extraData[1]?.dataType),
|
2022-03-16 14:26:57 +00:00
|
|
|
defaultValue: extraData[1]?.initialValue,
|
2021-12-27 12:04:45 +00:00
|
|
|
placeholder: props.extraData
|
|
|
|
|
? props.extraData[1]?.placeholderText
|
2023-06-14 14:09:38 +00:00
|
|
|
: `Value ${index + 1}`,
|
2021-12-27 12:04:45 +00:00
|
|
|
name: valueTextFieldName,
|
|
|
|
|
isRequired: extraData[1]?.isRequired,
|
|
|
|
|
}}
|
|
|
|
|
/>
|
2021-04-28 10:28:39 +00:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2023-05-19 18:37:06 +00:00
|
|
|
{!props.actionConfig && (
|
|
|
|
|
<StyledButton
|
|
|
|
|
className="t--delete-field"
|
|
|
|
|
isIconButton
|
|
|
|
|
kind="tertiary"
|
|
|
|
|
onClick={() => props.fields.remove(index)}
|
|
|
|
|
size="md"
|
|
|
|
|
startIcon="delete"
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2021-04-28 10:28:39 +00:00
|
|
|
|
|
|
|
|
{props.actionConfig && (
|
|
|
|
|
<DynamicTextField
|
|
|
|
|
name={`${field}.value`}
|
|
|
|
|
placeholder={
|
|
|
|
|
props.actionConfig[index].mandatory &&
|
|
|
|
|
props.actionConfig[index].type
|
|
|
|
|
? `Value (Type: ${props.actionConfig[index].type})`
|
|
|
|
|
: `Value (optional)`
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</FormRowWithLabel>
|
|
|
|
|
);
|
|
|
|
|
})}
|
2023-05-19 18:37:06 +00:00
|
|
|
<AddMoreButton
|
|
|
|
|
className="t--add-field t--addApiHeader btn-add-more"
|
|
|
|
|
kind="tertiary"
|
|
|
|
|
onClick={addRow}
|
|
|
|
|
size="md"
|
|
|
|
|
startIcon="add-more"
|
|
|
|
|
>
|
|
|
|
|
Add more
|
|
|
|
|
</AddMoreButton>
|
2021-04-28 10:28:39 +00:00
|
|
|
</>
|
|
|
|
|
) : null;
|
|
|
|
|
}
|
2020-04-28 06:52:53 +00:00
|
|
|
|
2021-12-27 12:04:45 +00:00
|
|
|
class KeyValueArrayControl extends BaseControl<KeyValueArrayControlProps> {
|
2020-04-28 06:52:53 +00:00
|
|
|
render() {
|
|
|
|
|
const name = getFieldName(this.props.configProperty);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<FieldArray
|
|
|
|
|
component={KeyValueRow}
|
|
|
|
|
rerenderOnEveryChange={false}
|
|
|
|
|
{...this.props}
|
2021-12-27 12:04:45 +00:00
|
|
|
name={name ? name[0] : ""}
|
2020-04-28 06:52:53 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getControlType(): ControlType {
|
|
|
|
|
return "KEYVALUE_ARRAY";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-27 12:04:45 +00:00
|
|
|
const getFieldName = (configProperty: string): string[] | undefined => {
|
|
|
|
|
if (configProperty) return configProperty.split("[*].");
|
2020-04-28 06:52:53 +00:00
|
|
|
};
|
|
|
|
|
|
2021-09-09 15:10:22 +00:00
|
|
|
const getType = (dataType: string | undefined) => {
|
2020-04-28 06:52:53 +00:00
|
|
|
switch (dataType) {
|
|
|
|
|
case "PASSWORD":
|
|
|
|
|
return "password";
|
|
|
|
|
case "NUMBER":
|
|
|
|
|
return "number";
|
|
|
|
|
default:
|
|
|
|
|
return "text";
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-05-19 18:37:06 +00:00
|
|
|
function renderInput(
|
|
|
|
|
props: InputProps & {
|
2021-12-27 12:04:45 +00:00
|
|
|
dataType?: "text" | "number" | "password";
|
|
|
|
|
placeholder?: string;
|
|
|
|
|
defaultValue: string | number;
|
|
|
|
|
isRequired: boolean;
|
2023-05-19 18:37:06 +00:00
|
|
|
isKeyFieldValid?: (value: string) => { isValid: boolean; message: string };
|
2021-12-27 12:04:45 +00:00
|
|
|
helperText?: string;
|
|
|
|
|
} & {
|
|
|
|
|
meta: Partial<WrappedFieldMetaProps>;
|
|
|
|
|
input: Partial<WrappedFieldInputProps>;
|
|
|
|
|
},
|
|
|
|
|
): JSX.Element {
|
|
|
|
|
return (
|
2023-05-19 18:37:06 +00:00
|
|
|
<StyledInput
|
|
|
|
|
aria-label={
|
|
|
|
|
props.helperText || props.defaultValue || props.placeholder || "label"
|
|
|
|
|
}
|
2021-12-27 12:04:45 +00:00
|
|
|
defaultValue={props.defaultValue}
|
2023-05-19 18:37:06 +00:00
|
|
|
description={props.helperText}
|
|
|
|
|
errorMessage={props.isKeyFieldValid?.(props.input.value).message}
|
|
|
|
|
isValid={props.isKeyFieldValid?.(props.input.value).isValid}
|
2021-12-27 12:04:45 +00:00
|
|
|
name={props.input?.name}
|
|
|
|
|
onChange={props.input.onChange}
|
|
|
|
|
placeholder={props.placeholder}
|
2023-05-19 18:37:06 +00:00
|
|
|
size="md"
|
|
|
|
|
type={props.dataType}
|
2021-12-27 12:04:45 +00:00
|
|
|
value={props.input.value}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2020-04-28 06:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
2021-12-27 12:04:45 +00:00
|
|
|
export default KeyValueArrayControl;
|