Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

chore: Update the checkbox component to a functional component (#3440)

Igor Dykhtab008a1dd62a6
diff --git a/src/components/src/common/checkbox.tsx b/src/components/src/common/checkbox.tsx
index fd46ee329..f855ee194 100644
--- a/src/components/src/common/checkbox.tsx
+++ b/src/components/src/common/checkbox.tsx
@@ -2,8 +2,7 @@
// Copyright contributors to the kepler.gl project
import classnames from 'classnames';
-import pick from 'lodash/pick';
-import React, {ChangeEventHandler, Component, FocusEventHandler, ReactNode} from 'react';
+import React, {ChangeEventHandler, FC, FocusEventHandler, ReactNode, useCallback} from 'react';
import styled from 'styled-components';
import {shouldForwardProp} from './styled-components';
@@ -19,11 +18,11 @@ const StyledSwitchInput = styled.label.withConfig({shouldForwardProp})<StyledSwi
${props => (props.secondary ? props.theme.secondarySwitch : props.theme.inputSwitch)};
`;
-const StyledCheckboxInput = styled.label`
+const StyledCheckboxInput = styled.label.withConfig({shouldForwardProp})`
${props => props.theme.inputCheckbox};
`;
-const StyledRadiuInput = styled.label.withConfig({shouldForwardProp})<StyledSwitchInputProps>`
+const StyledRadioInput = styled.label.withConfig({shouldForwardProp})<StyledSwitchInputProps>`
${props => (props.secondary ? props.theme.secondaryRadio : props.theme.inputRadio)};
`;
@@ -65,66 +64,80 @@ interface CheckboxProps {
switch?: boolean;
activeColor?: string;
secondary?: boolean;
- onBlur: FocusEventHandler<HTMLInputElement>;
+ onBlur?: FocusEventHandler<HTMLInputElement>;
onChange?: ChangeEventHandler<HTMLInputElement>;
- onFocus: FocusEventHandler<HTMLInputElement>;
+ onFocus?: FocusEventHandler<HTMLInputElement>;
}
-export default class Checkbox extends Component<CheckboxProps> {
- static defaultProps = {
- disabled: false,
- checked: false,
- onBlur: noop,
- onChange: noop,
- onFocus: noop,
- label: ''
+const Checkbox: FC<CheckboxProps> = ({
+ id,
+ type,
+ label = '',
+ name,
+ className,
+ value,
+ checked = false,
+ disabled = false,
+ secondary,
+ onBlur = noop,
+ onChange = noop,
+ onFocus = noop
+}) => {
+ const handleFocus: FocusEventHandler<HTMLInputElement> = useCallback(
+ args => {
+ onFocus(args);
+ },
+ [onFocus]
+ );
+
+ const handleBlur: FocusEventHandler<HTMLInputElement> = useCallback(
+ args => {
+ onBlur(args);
+ },
+ [onBlur]
+ );
+
+ const inputType = type === 'radio' ? 'radio' : 'checkbox';
+
+ const inputProps = {
+ checked,
+ disabled,
+ id,
+ name,
+ onChange,
+ value,
+ secondary,
+ type: inputType,
+ onFocus: handleFocus,
+ onBlur: handleBlur
};
- state = {
- focused: false
+ const labelProps = {
+ checked,
+ disabled,
+ secondary,
+ htmlFor: id
};
- handleFocus: FocusEventHandler<HTMLInputElement> = args => {
- this.setState({focused: true});
- this.props.onFocus(args);
- };
-
- handleBlur: FocusEventHandler<HTMLInputElement> = args => {
- this.setState({focused: false});
- this.props.onBlur(args);
- };
-
- render() {
- const inputProps = {
- ...pick(this.props, ['checked', 'disabled', 'id', 'onChange', 'value', 'secondary']),
- type: 'checkbox',
- onFocus: this.handleFocus,
- onBlur: this.handleBlur
- };
-
- const labelProps = {
- ...pick(this.props, ['checked', 'disabled', 'secondary']),
- htmlFor: this.props.id
- };
-
- const LabelElement =
- this.props.type === 'checkbox'
- ? StyledCheckboxInput
- : this.props.type === 'radio'
- ? StyledRadiuInput
- : StyledSwitchInput;
-
- return (
- <StyledCheckbox
- type={this.props.type}
- className={classnames('kg-checkbox', this.props.className)}
- disabled={this.props.disabled}
- >
- <HiddenInput {...inputProps} />
- <LabelElement className="kg-checkbox__label" {...labelProps}>
- {this.props.label}
- </LabelElement>
- </StyledCheckbox>
- );
- }
-}
+ const LabelElement =
+ type === 'checkbox'
+ ? StyledCheckboxInput
+ : type === 'radio'
+ ? StyledRadioInput
+ : StyledSwitchInput;
+
+ return (
+ <StyledCheckbox
+ type={type}
+ className={classnames('kg-checkbox', className)}
+ disabled={disabled}
+ >
+ <HiddenInput {...inputProps} />
+ <LabelElement className="kg-checkbox__label" {...labelProps}>
+ {label}
+ </LabelElement>
+ </StyledCheckbox>
+ );
+};
+
+export default Checkbox;