/* * Copyright (c) 2018-present, Revolut LTD. * * This source code is licensed under the Apache 2.0 license found in the * LICENSE file in the root directory of this source tree. */ import * as React from 'react' import { CheckboxInput } from './CheckboxInput' import { CheckboxContent } from './CheckboxContent' import { SwitcherInput } from './SwitcherInput' import { SwitcherContent } from './SwitcherContent' const TYPE_LIST = { checkbox: { name: 'checkbox', renderControl: CheckboxInput, renderLabelContent: CheckboxContent, }, switcher: { name: 'switcher', renderControl: SwitcherInput, renderLabelContent: SwitcherContent, }, } // @ts-ignore function getTypeConfig(type) { // @ts-ignore return TYPE_LIST[type] || TYPE_LIST[Object.values(TYPE_LIST)[0].name] } type renderDefaultProps = { renderControl?: (...args: any[]) => any renderLabelContent?: (...args: any[]) => any parentProps?: object label?: string className?: string } const renderDefault: React.SFC = ({ renderControl, renderLabelContent, className, ...restProps }) => { // @ts-ignore const control = renderControl(restProps) // @ts-ignore const labelContent = renderLabelContent(restProps) return (
) } type CheckboxProps = { id?: string | number onChange?: (...args: any[]) => any | void checked?: boolean indeterminate?: boolean disabled?: boolean name?: string value?: string | boolean label?: string renderLabelContent?: (...args: any[]) => React.ReactNode renderControl?: (...args: any[]) => React.ReactNode type?: any render?: (...args: any[]) => React.ReactNode className?: string } export class Checkbox extends React.Component { // This export means that these guys may be used outside of the component, so we need to be sure // TODO: All of them should be refactored to apply all props, now they are wrong. static CheckboxInput = CheckboxInput static CheckboxLabel = CheckboxContent static SwitcherInput = SwitcherInput static SwitcherLabel = SwitcherContent static defaultProps = { label: '', type: TYPE_LIST.checkbox.name, checked: false, render: renderDefault, } // @ts-ignore handleChange = (event, value) => { const isChecked = value !== undefined ? value : event.target.checked // tslint:disable-next-line this.props.onChange && this.props.onChange(event, isChecked) } render() { const { render, type, renderLabelContent, renderControl } = this.props const defaultRenderer = getTypeConfig(type) return render({ ...this.props, handleChange: this.handleChange, renderControl: renderControl || defaultRenderer.renderControl, renderLabelContent: renderLabelContent || defaultRenderer.renderLabelContent, }) } }