PromucFlow_constructor/app/client/src/components/formControls/SegmentedControl.tsx

151 lines
4.3 KiB
TypeScript
Raw Normal View History

fix: Update connection mode DropDown by Segmented Control (#23730) ## Description > Update the Drop-Down component to Segemented Control as per the designs in datasource configuration. >Here is the FIGMA [link](https://www.figma.com/file/xlYswIZx9noaldAPmv3ji4/Multiple-Environments-V2?type=design&node-id=79-36425&t=egGDXQsYmAFAWjBU-0) >Current state ><img width="393" alt="Screenshot 2023-05-25 at 3 08 38 PM" src="https://github.com/appsmithorg/appsmith-ee/assets/104058110/5f623fee-5ba7-464d-be34-a106f0e7c438"> >Updated State ><img width="393" alt="Screenshot 2023-05-25 at 3 08 38 PM" src="https://user-images.githubusercontent.com/104058110/237018777-132f6607-a757-41c0-9d52-c00e70745f36.png"> #### PR fixes following issue(s) Fixes # #### Type of change > Please delete options that are not relevant. - Bug fix (non-breaking change which fixes an issue) > ## Testing > #### How Has This Been Tested? - [x] Manual > > #### 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 - [x] I have performed a self-review of my own code - [x] 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 - [x] New and existing unit tests pass locally with my changes - [ ] PR is being merged under a feature flag #### QA activity: - [ ] [Speedbreak features](https://github.com/appsmithorg/TestSmith/wiki/Test-plan-implementation#speedbreaker-features-to-consider-for-every-change) have been covered - [x] Test plan covers all impacted features and [areas of interest](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans/_edit#areas-of-interest) - [x] Test plan has been peer reviewed by project stakeholders and other QA members - [x] Manually tested functionality on DP - [ ] We had an implementation alignment call with stakeholders post QA Round 2 - [ ] Cypress test cases have been added and approved by SDET/manual QA - [ ] Added `Test Plan Approved` label after Cypress tests were reviewed - [ ] Added `Test Plan Approved` label after JUnit tests were reviewed
2023-05-29 11:15:52 +00:00
import React from "react";
import type { ControlProps } from "./BaseControl";
import BaseControl from "./BaseControl";
import styled from "styled-components";
import type { ControlType } from "constants/PropertyControlConstants";
import { isNil } from "lodash";
import type { WrappedFieldInputProps, WrappedFieldMetaProps } from "redux-form";
import { Field } from "redux-form";
import { connect } from "react-redux";
import type { AppState } from "@appsmith/reducers";
import { getDynamicFetchedValues } from "selectors/formSelectors";
import { change, getFormValues } from "redux-form";
import type { Action } from "entities/Action";
import type { SelectOptionProps } from "design-system";
import { SegmentedControl } from "design-system";
const SegmentedControlWrapper = styled.div<{
width: string;
}>`
/* font-size: 14px; */
width: ${(props) => (props?.width ? props?.width : "270px")};
`;
class SegementedControl extends BaseControl<Props> {
render() {
const styles = {
// width: "280px",
...("customStyles" in this.props &&
typeof this.props.customStyles === "object"
? this.props.customStyles
: {}),
};
return (
<SegmentedControlWrapper
className={`t--${this?.props?.configProperty}`}
data-testid={this.props.configProperty}
style={styles}
width={styles.width}
>
<Field
component={renderSegementedControl}
name={this.props.configProperty}
props={{ ...this.props, width: styles.width }}
/>
</SegmentedControlWrapper>
);
}
getControlType(): ControlType {
return "SEGMENTED_CONTROL";
}
}
function renderSegementedControl(
props: {
input?: WrappedFieldInputProps;
meta?: Partial<WrappedFieldMetaProps>;
width: string;
} & SegmentedControlProps,
): JSX.Element {
let selectedValue: string;
//Update selected value
if (isNil(props.input?.value)) {
selectedValue = props?.initialValue ? (props.initialValue as string) : "";
} else {
selectedValue = props.input?.value;
}
let options: SelectOptionProps[] = [];
if (typeof props.options === "object" && Array.isArray(props.options)) {
options = props.options;
}
//Function to handle selection of options
const onSelectOptions = (value: string | undefined) => {
if (!isNil(value)) {
if (!(selectedValue === value)) {
selectedValue = value;
props.input?.onChange(selectedValue);
}
}
};
const segmentedOptions = options.map((e) => {
return { label: e.label, value: e.value };
});
return (
<SegmentedControl
defaultValue={props.initialValue as string}
isFullWidth
onChange={(value) => onSelectOptions(value)}
options={segmentedOptions}
value={selectedValue}
/>
);
}
export interface SegmentedControlProps extends ControlProps {
options: SelectOptionProps[];
optionWidth?: string;
placeholderText: string;
propertyValue: string;
fetchOptionsConditionally?: boolean;
isLoading: boolean;
formValues: Partial<Action>;
}
type ReduxDispatchProps = {
updateConfigPropertyValue: (
formName: string,
field: string,
value: any,
) => void;
};
type Props = SegmentedControlProps & ReduxDispatchProps;
const mapStateToProps = (
state: AppState,
ownProps: SegmentedControlProps,
): {
isLoading: boolean;
options: SelectOptionProps[];
formValues: Partial<Action>;
} => {
// Added default options to prevent error when options is undefined
let isLoading = false;
let options = ownProps.fetchOptionsConditionally ? [] : ownProps.options;
const formValues: Partial<Action> = getFormValues(ownProps.formName)(state);
try {
if (ownProps.fetchOptionsConditionally) {
const dynamicFetchedValues = getDynamicFetchedValues(state, ownProps);
isLoading = dynamicFetchedValues.isLoading;
options = dynamicFetchedValues.data;
}
} catch (e) {
} finally {
return { isLoading, options, formValues };
}
};
const mapDispatchToProps = (dispatch: any): ReduxDispatchProps => ({
updateConfigPropertyValue: (formName: string, field: string, value: any) => {
dispatch(change(formName, field, value));
},
});
// Connecting this component to the state to allow for dynamic fetching of options to be updated.
export default connect(mapStateToProps, mapDispatchToProps)(SegementedControl);