PromucFlow_constructor/app/client/src/widgets/CheckboxWidget/component/index.tsx
balajisoundar 2608e3dbd3
chore: Move the widget config to widget class (#26073)
## Description
- Remove the config objects from widget and config maps from the widget
factory.
- Introduce methods in widget development API to dynamically fetch this
items.
- freeze the widget configuration.

#### PR fixes following issue(s)
Fixes https://github.com/appsmithorg/appsmith/issues/26008
> if no issue exists, please create an issue and ask the maintainers
about this first
>
>
#### Media
> A video or a GIF is preferred. when using Loom, don’t embed because it
looks like it’s a GIF. instead, just link to the video
>
>
#### Type of change
> Please delete options that are not relevant.
- Bug fix (non-breaking change which fixes an issue)
- New feature (non-breaking change which adds functionality)
- Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- Chore (housekeeping or task changes that don't impact user perception)
- This change requires a documentation update
>
>
>
## Testing
>
#### How Has This Been Tested?
> Please describe the tests that you ran to verify your changes. Also
list any relevant details for your test configuration.
> Delete anything that is not relevant
- [x] Manual
- [ ] Jest
- [ ] Cypress
>
>
#### 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
- [ ] 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
- [ ] 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:
- [ ] [Speedbreak
features](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#speedbreakers-)
have been covered
- [x] Test plan covers all impacted features and [areas of
interest](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#areas-of-interest-)
- [ ] 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-09-06 17:45:04 +05:30

164 lines
4.8 KiB
TypeScript

import React from "react";
import styled from "styled-components";
import type { ComponentProps } from "widgets/BaseComponent";
import { AlignWidgetTypes } from "WidgetProvider/constants";
import { Classes } from "@blueprintjs/core";
import { Colors } from "constants/Colors";
import { LabelPosition } from "components/constants";
import { FontStyleTypes } from "constants/WidgetConstants";
import { Checkbox } from "components/wds";
type StyledCheckboxContainerProps = {
isValid: boolean;
noContainerPadding?: boolean;
labelPosition?: LabelPosition;
minHeight?: number;
};
const DEFAULT_BORDER_RADIUS = "0";
const DEFAULT_BACKGROUND_COLOR = Colors.GREEN_SOLID;
const CheckboxContainer = styled.div<StyledCheckboxContainerProps>`
&& {
align-items: center;
display: flex;
height: 100%;
justify-content: start;
width: 100%;
${({ minHeight }) => `
${minHeight ? `min-height: ${minHeight}px;` : ""}`};
.${Classes.CHECKBOX} {
width: 100%;
}
}
`;
export const CheckboxLabel = styled.div<{
disabled?: boolean;
alignment: AlignWidgetTypes;
labelTextColor?: string;
labelTextSize?: string;
labelStyle?: string;
isDynamicHeightEnabled?: boolean;
isLabelInline?: boolean;
}>`
width: 100%;
display: inline-block;
vertical-align: top;
text-align: ${({ alignment }) => alignment.toLowerCase()};
${({ disabled, labelStyle, labelTextColor, labelTextSize }) => `
color: ${
disabled ? "var(--wds-color-text-disabled)" : labelTextColor || "inherit"
};
font-size: ${labelTextSize ?? "inherit"};
font-weight: ${labelStyle?.includes(FontStyleTypes.BOLD) ? "bold" : "normal"};
font-style: ${
labelStyle?.includes(FontStyleTypes.ITALIC) ? "italic" : "normal"
};
`}
${({ isDynamicHeightEnabled }) =>
isDynamicHeightEnabled ? "&& { word-break: break-all; }" : ""};
${({ isLabelInline }) =>
isLabelInline &&
`
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
word-wrap: normal;
`}
`;
export const StyledCheckbox = styled(Checkbox)`
&.bp3-control.bp3-align-right {
padding-right: 0px;
}
`;
class CheckboxComponent extends React.Component<CheckboxComponentProps> {
render() {
/**
* When the label position is left align checkbox to the right
* When the label position is right align checkbox to the left
*/
const checkboxAlignClass =
this.props.labelPosition === LabelPosition.Right ? "left" : "right";
// If the prop isValid has a value true/false (it was explicitly passed to this component),
// it take priority over the internal logic to determine if the field is valid or not.
const isValid = (() => {
if (this.props.isValid !== undefined) {
return this.props.isValid;
}
return !(this.props.isRequired && !this.props.isChecked);
})();
return (
<CheckboxContainer
isValid={isValid}
minHeight={this.props.minHeight}
noContainerPadding={this.props.noContainerPadding}
>
<StyledCheckbox
accentColor={this.props.accentColor || DEFAULT_BACKGROUND_COLOR}
alignIndicator={checkboxAlignClass}
borderRadius={this.props.borderRadius || DEFAULT_BORDER_RADIUS}
checked={this.props.isChecked}
className={
this.props.isLoading ? Classes.SKELETON : Classes.RUNNING_TEXT
}
disabled={this.props.isDisabled}
hasError={!isValid}
inputRef={this.props.inputRef}
labelElement={
<CheckboxLabel
alignment={this.props.alignWidget || AlignWidgetTypes.LEFT}
className="t--checkbox-widget-label"
disabled={this.props.isDisabled}
isDynamicHeightEnabled={this.props.isDynamicHeightEnabled}
isLabelInline={this.props.isLabelInline}
labelStyle={this.props.labelStyle}
labelTextColor={this.props.labelTextColor}
labelTextSize={this.props.labelTextSize}
>
{this.props.label}
</CheckboxLabel>
}
onChange={this.onCheckChange}
/>
</CheckboxContainer>
);
}
onCheckChange = () => {
this.props.onCheckChange(!this.props.isChecked);
};
}
export interface CheckboxComponentProps extends ComponentProps {
alignWidget?: AlignWidgetTypes;
noContainerPadding?: boolean;
isChecked: boolean;
isLoading: boolean;
isRequired?: boolean;
isValid?: boolean;
label: string;
onCheckChange: (isChecked: boolean) => void;
inputRef?: (el: HTMLInputElement | null) => any;
accentColor: string;
borderRadius: string;
isDynamicHeightEnabled?: boolean;
labelPosition: LabelPosition;
labelTextColor?: string;
labelTextSize?: string;
labelStyle?: string;
isLabelInline?: boolean;
minHeight?: number;
}
export default CheckboxComponent;