2020-01-21 12:48:42 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import styled from "styled-components";
|
2021-02-16 12:15:17 +00:00
|
|
|
import { Alignment, Checkbox, Classes } from "@blueprintjs/core";
|
2021-09-08 06:15:11 +00:00
|
|
|
|
|
|
|
|
import { ComponentProps } from "components/designSystems/appsmith/BaseComponent";
|
2021-02-16 12:15:17 +00:00
|
|
|
import { AlignWidget } from "widgets/SwitchWidget";
|
2020-01-21 12:48:42 +00:00
|
|
|
|
2021-09-08 06:15:11 +00:00
|
|
|
type StyledCheckboxProps = {
|
|
|
|
|
rowSpace: number;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type StyledCheckboxContainerProps = {
|
|
|
|
|
isValid: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const CheckboxContainer = styled.div<StyledCheckboxContainerProps>`
|
2020-01-21 12:48:42 +00:00
|
|
|
&& {
|
2021-09-08 06:15:11 +00:00
|
|
|
align-items: center;
|
2020-01-21 12:48:42 +00:00
|
|
|
display: flex;
|
2021-09-08 06:15:11 +00:00
|
|
|
height: 100%;
|
2020-01-21 12:48:42 +00:00
|
|
|
justify-content: flex-start;
|
2021-09-08 06:15:11 +00:00
|
|
|
width: 100%;
|
2021-02-16 12:15:17 +00:00
|
|
|
&.${Alignment.RIGHT} {
|
|
|
|
|
justify-content: flex-end;
|
|
|
|
|
}
|
2021-09-08 06:15:11 +00:00
|
|
|
|
|
|
|
|
& .bp3-control-indicator {
|
2020-12-24 04:32:25 +00:00
|
|
|
border: ${(props) =>
|
2021-09-08 06:15:11 +00:00
|
|
|
!props.isValid && `1px solid ${props.theme.colors.error} !important`};
|
2020-11-26 11:21:23 +00:00
|
|
|
}
|
2021-09-08 06:15:11 +00:00
|
|
|
}
|
|
|
|
|
`;
|
2020-11-26 11:21:23 +00:00
|
|
|
|
2021-09-08 06:15:11 +00:00
|
|
|
export const StyledCheckbox = styled(Checkbox)<StyledCheckboxProps>`
|
|
|
|
|
height: ${({ rowSpace }) => rowSpace}px;
|
|
|
|
|
|
|
|
|
|
&.bp3-control input:checked ~ .bp3-control-indicator {
|
|
|
|
|
background-color: #03b365;
|
|
|
|
|
background-image: none;
|
|
|
|
|
box-shadow: none;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
&.bp3-control input:disabled ~ .bp3-control-indicator {
|
|
|
|
|
opacity: 0.5;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
&.bp3-control.bp3-checkbox .bp3-control-indicator {
|
|
|
|
|
border-radius: 0;
|
2020-01-21 12:48:42 +00:00
|
|
|
}
|
|
|
|
|
`;
|
2021-09-08 06:15:11 +00:00
|
|
|
|
2019-09-09 09:08:54 +00:00
|
|
|
class CheckboxComponent extends React.Component<CheckboxComponentProps> {
|
2019-03-21 12:10:32 +00:00
|
|
|
render() {
|
2021-02-16 12:15:17 +00:00
|
|
|
const checkboxAlignClass =
|
|
|
|
|
this.props.alignWidget === "RIGHT" ? Alignment.RIGHT : Alignment.LEFT;
|
2021-09-08 06:15:11 +00:00
|
|
|
|
2019-03-21 12:10:32 +00:00
|
|
|
return (
|
2020-11-26 11:21:23 +00:00
|
|
|
<CheckboxContainer
|
2021-02-16 12:15:17 +00:00
|
|
|
className={checkboxAlignClass}
|
2021-04-28 10:28:39 +00:00
|
|
|
isValid={!(this.props.isRequired && !this.props.isChecked)}
|
2020-11-26 11:21:23 +00:00
|
|
|
>
|
2021-09-08 06:15:11 +00:00
|
|
|
<StyledCheckbox
|
2021-02-16 12:15:17 +00:00
|
|
|
alignIndicator={checkboxAlignClass}
|
2021-04-28 10:28:39 +00:00
|
|
|
checked={this.props.isChecked}
|
2020-01-21 12:48:42 +00:00
|
|
|
className={
|
2021-02-16 12:15:17 +00:00
|
|
|
this.props.isLoading ? Classes.SKELETON : Classes.RUNNING_TEXT
|
2020-01-21 12:48:42 +00:00
|
|
|
}
|
|
|
|
|
disabled={this.props.isDisabled}
|
2021-04-28 10:28:39 +00:00
|
|
|
label={this.props.label}
|
|
|
|
|
onChange={this.onCheckChange}
|
2021-09-08 06:15:11 +00:00
|
|
|
rowSpace={this.props.rowSpace}
|
2020-01-21 12:48:42 +00:00
|
|
|
/>
|
|
|
|
|
</CheckboxContainer>
|
2019-09-05 17:47:50 +00:00
|
|
|
);
|
2019-03-21 12:10:32 +00:00
|
|
|
}
|
2019-11-05 05:09:50 +00:00
|
|
|
|
2020-03-13 07:24:03 +00:00
|
|
|
onCheckChange = () => {
|
|
|
|
|
this.props.onCheckChange(!this.props.isChecked);
|
2019-11-05 05:09:50 +00:00
|
|
|
};
|
2019-03-21 12:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-09 09:08:54 +00:00
|
|
|
export interface CheckboxComponentProps extends ComponentProps {
|
2021-09-08 06:15:11 +00:00
|
|
|
alignWidget?: AlignWidget;
|
2020-03-13 07:24:03 +00:00
|
|
|
isChecked: boolean;
|
2019-12-03 04:41:10 +00:00
|
|
|
isLoading: boolean;
|
2020-11-26 11:21:23 +00:00
|
|
|
isRequired?: boolean;
|
2021-09-08 06:15:11 +00:00
|
|
|
label: string;
|
|
|
|
|
onCheckChange: (isChecked: boolean) => void;
|
|
|
|
|
rowSpace: number;
|
2019-03-21 12:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-05 17:47:50 +00:00
|
|
|
export default CheckboxComponent;
|