/*
 * @Author: codingfly
 * @Description:
 * @Date: 2021-08-13 15:39:29
 * @LastEditTime: 2021-09-06 09:03:22
 * @FilePath: \young\components\radio\index.tsx
 */
import * as React from 'react';
import { View } from 'remax/one';
import classNames from 'classnames';
import { getPrefixCls } from './../common';
import Icon from '../icon';
var prefixCls = getPrefixCls('radio');

var Radio = function Radio(props) {
  var children = props.children,
      checked = props.checked,
      value = props.value,
      _props$color = props.color,
      color = _props$color === void 0 ? '#1890FF' : _props$color,
      extra = props.extra,
      style = props.style,
      onChange = props.onChange;

  var handleClick = function handleClick(e) {
    onChange === null || onChange === void 0 ? void 0 : onChange(!checked, e, value);
  };

  return /*#__PURE__*/React.createElement(View, {
    className: prefixCls,
    style: style
  }, /*#__PURE__*/React.createElement(View, {
    className: "".concat(prefixCls, "-container"),
    onTap: handleClick
  }, /*#__PURE__*/React.createElement(View, {
    className: classNames("".concat(prefixCls, "-out-round"))
  }, checked ? /*#__PURE__*/React.createElement(Icon, {
    className: "".concat(prefixCls, "-inner-round"),
    type: "roundcheckfill",
    size: 24,
    color: color
  }) : /*#__PURE__*/React.createElement(Icon, {
    className: "".concat(prefixCls, "-inner-round"),
    type: "round",
    size: 24,
    color: "#999"
  })), /*#__PURE__*/React.createElement(View, {
    className: "".concat(prefixCls, "-children")
  }, children)), /*#__PURE__*/React.createElement(View, {
    className: "".concat(prefixCls, "-extra")
  }, extra));
};

var getRadios = function getRadios(children, value, color, onChange) {
  var onGroupChange = function onGroupChange(checked, e, v) {
    var newValue = v;
    onChange === null || onChange === void 0 ? void 0 : onChange(newValue, e);
  };

  var radios = React.Children.map(children, function (radio) {
    var newRadio = radio;
    var checked = false;

    if (newRadio && newRadio.props) {
      if ((newRadio.props.value || newRadio.props.value === 0 || newRadio.props.value === false) && newRadio.props.value === value) {
        checked = true;
      } else {
        checked = false;
      }

      return /*#__PURE__*/React.createElement(Radio, Object.assign({
        color: color
      }, newRadio.props, {
        checked: checked,
        onChange: onGroupChange
      }));
    }

    return newRadio;
  });
  return radios;
};

Radio.Group = function (props) {
  var value = props.value,
      color = props.color,
      children = props.children,
      _props$direction = props.direction,
      direction = _props$direction === void 0 ? 'row' : _props$direction,
      onChange = props.onChange,
      style = props.style;
  var radios = getRadios(children, value, color, onChange);
  return /*#__PURE__*/React.createElement(View, {
    style: Object.assign({
      display: 'flex',
      flexDirection: direction
    }, style)
  }, radios);
};

export default Radio;