2020-01-21 12:48:42 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import styled from "styled-components";
|
2021-09-09 15:10:22 +00:00
|
|
|
import { ComponentProps } from "widgets/BaseComponent";
|
2021-02-16 12:15:17 +00:00
|
|
|
import { Alignment, Checkbox, Classes } from "@blueprintjs/core";
|
2021-09-09 15:10:22 +00:00
|
|
|
import { AlignWidget } from "widgets/constants";
|
2021-11-11 17:41:43 +00:00
|
|
|
import { Colors } from "constants/Colors";
|
2020-01-21 12:48:42 +00:00
|
|
|
|
2021-09-08 06:15:11 +00:00
|
|
|
type StyledCheckboxProps = {
|
2021-11-11 17:41:43 +00:00
|
|
|
checked?: boolean;
|
2022-01-11 13:26:50 +00:00
|
|
|
disabled?: boolean;
|
|
|
|
|
indeterminate?: boolean;
|
|
|
|
|
rowSpace: number;
|
2021-09-08 06:15:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type StyledCheckboxContainerProps = {
|
|
|
|
|
isValid: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const CheckboxContainer = styled.div<StyledCheckboxContainerProps>`
|
2020-01-21 12:48:42 +00:00
|
|
|
&& {
|
2021-11-11 17:41:43 +00:00
|
|
|
padding: 9px 12px;
|
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;
|
2021-11-11 17:41:43 +00:00
|
|
|
color: ${({ checked }) => (checked ? Colors.GREY_10 : Colors.GREY_9)};
|
2021-09-08 06:15:11 +00:00
|
|
|
|
2021-11-11 17:41:43 +00:00
|
|
|
&.bp3-control.bp3-checkbox .bp3-control-indicator {
|
|
|
|
|
border-radius: 0;
|
|
|
|
|
border: 1px solid ${Colors.GREY_3};
|
2022-01-11 13:26:50 +00:00
|
|
|
box-shadow: none !important;
|
2021-11-11 17:41:43 +00:00
|
|
|
outline: none !important;
|
|
|
|
|
background: transparent;
|
2021-09-08 06:15:11 +00:00
|
|
|
|
2022-01-11 13:26:50 +00:00
|
|
|
${({ checked, indeterminate }) =>
|
|
|
|
|
checked || indeterminate
|
2021-11-11 17:41:43 +00:00
|
|
|
? `
|
|
|
|
|
background-color: ${Colors.GREEN_SOLID} !important;
|
|
|
|
|
background-image: none;
|
|
|
|
|
border: none !important;
|
|
|
|
|
`
|
|
|
|
|
: ``}
|
2022-01-11 13:26:50 +00:00
|
|
|
|
|
|
|
|
${({ checked }) =>
|
|
|
|
|
checked &&
|
|
|
|
|
`
|
|
|
|
|
&::before {
|
|
|
|
|
background-image: url("data:image/svg+xml,%3Csvg width='16' height='16' viewBox='0 0 14 14' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Crect width='14' height='14' /%3E%3Cpath d='M10.1039 3.5L11 4.40822L5.48269 10L2.5 6.97705L3.39613 6.06883L5.48269 8.18305L10.1039 3.5Z' fill='white'/%3E%3C/svg%3E%0A") !important;
|
|
|
|
|
}
|
|
|
|
|
`}
|
|
|
|
|
|
2021-11-11 17:41:43 +00:00
|
|
|
${({ disabled }) => (disabled ? `opacity: 0.5;` : ``)}
|
2021-09-08 06:15:11 +00:00
|
|
|
}
|
|
|
|
|
|
2021-11-11 17:41:43 +00:00
|
|
|
&:hover {
|
|
|
|
|
&.bp3-control.bp3-checkbox .bp3-control-indicator {
|
|
|
|
|
${({ disabled }) =>
|
|
|
|
|
disabled ? "" : `border: 1px solid ${Colors.GREY_5}`};
|
2022-01-11 13:26:50 +00:00
|
|
|
${({ checked, indeterminate }) =>
|
|
|
|
|
checked || indeterminate
|
2021-11-11 17:41:43 +00:00
|
|
|
? `
|
|
|
|
|
background-image: linear-gradient(
|
|
|
|
|
0deg,
|
|
|
|
|
rgba(0, 0, 0, 0.2),
|
|
|
|
|
rgba(0, 0, 0, 0.2)
|
|
|
|
|
);
|
|
|
|
|
`
|
|
|
|
|
: ""};
|
|
|
|
|
}
|
2020-01-21 12:48:42 +00:00
|
|
|
}
|
2021-11-15 06:29:06 +00:00
|
|
|
|
|
|
|
|
&.${Classes.CONTROL}.${Classes.DISABLED} {
|
|
|
|
|
color: ${Colors.GREY_8};
|
|
|
|
|
}
|
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;
|