import React, { useEffect, useState } from 'react';
import Layout from '../../../layout';
import Utils from '../../../nodes/controls/utils';
import { Noodl } from '../../../types';
export interface CheckboxProps extends Noodl.ReactProps {
id: string;
enabled: boolean;
checked: boolean;
attrs: React.Attributes;
useLabel: boolean;
label: string;
labelSpacing: string;
labeltextStyle: Noodl.TextStyle;
useIcon: boolean;
iconPlacement: 'left' | 'right';
iconSpacing: string;
iconSourceType: 'image' | 'icon';
iconImageSource: Noodl.Image;
iconIconSource: Noodl.Icon;
iconSize: string;
iconColor: Noodl.Color;
checkedChanged: (checked: boolean) => void;
}
export function Checkbox(props: CheckboxProps) {
const [checked, setChecked] = useState(props.checked);
// Report initial values when mounted
useEffect(() => {
setChecked(!!props.checked);
}, []);
useEffect(() => {
setChecked(!!props.checked);
}, [props.checked]);
const style: React.CSSProperties = { ...props.style };
if (props.parentLayout === 'none') {
style.position = 'absolute';
}
Layout.align(style, props);
const inputProps = {
...props.attrs,
id: props.id,
className: [props.className, 'ndl-controls-checkbox-2'].join(' '),
disabled: !props.enabled,
style: {
width: props.styles.checkbox.width,
height: props.styles.checkbox.height
}
};
const inputWrapperStyle: React.CSSProperties = {
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
flexShrink: 0,
position: 'relative',
...props.styles.checkbox
};
if (!props.useLabel) {
Object.assign(inputProps, Utils.controlEvents(props));
Object.assign(inputWrapperStyle, style);
}
function _renderIcon() {
if (props.iconSourceType === 'image' && props.iconImageSource !== undefined)
return (
);
else if (props.iconSourceType === 'icon' && props.iconIconSource !== undefined) {
const style: React.CSSProperties = {
fontSize: props.iconSize,
color: props.iconColor,
position: 'absolute'
};
if (props.iconIconSource.codeAsClass === true) {
return (
);
} else {
return (
{props.iconIconSource.code}
);
}
}
return null;
}
const checkbox = (