@geezee/react-ui
Version:
Modern and minimalist React UI library.
71 lines (66 loc) • 3.03 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _JSXStyle from "styled-jsx/style";
import React, { useEffect, useMemo, useState } from 'react';
import withDefaults from '../utils/with-defaults';
import { RadioContext } from './radio-context';
var defaultProps = {
disabled: false,
size: 'medium',
className: '',
useRow: false
};
export var getRadioSize = function getRadioSize(size) {
var sizes = {
mini: '12px',
small: '14px',
medium: '16px',
large: '18px'
};
return sizes[size];
};
var RadioGroup = function RadioGroup(_ref) {
var disabled = _ref.disabled,
onChange = _ref.onChange,
value = _ref.value,
size = _ref.size,
children = _ref.children,
className = _ref.className,
initialValue = _ref.initialValue,
useRow = _ref.useRow,
props = _objectWithoutProperties(_ref, ["disabled", "onChange", "value", "size", "children", "className", "initialValue", "useRow"]);
var _useState = useState(initialValue),
_useState2 = _slicedToArray(_useState, 2),
selfVal = _useState2[0],
setSelfVal = _useState2[1];
var updateState = function updateState(nextValue) {
setSelfVal(nextValue);
onChange && onChange(nextValue);
};
var providerValue = useMemo(function () {
return {
updateState: updateState,
disabledAll: disabled,
inGroup: true,
value: selfVal
};
}, [disabled, selfVal]);
var fontSize = useMemo(function () {
return getRadioSize(size);
}, [size]);
var groupGap = "calc(".concat(fontSize, " * 1)");
useEffect(function () {
if (value === undefined) return;
setSelfVal(value);
}, [value]);
return /*#__PURE__*/React.createElement(RadioContext.Provider, {
value: providerValue
}, /*#__PURE__*/React.createElement("div", _extends({}, props, {
className: _JSXStyle.dynamic([["3258981770", [useRow ? 'col' : 'column', useRow ? 0 : groupGap, useRow ? groupGap : 0, fontSize]]]) + " " + (props && props.className != null && props.className || "radio-group ".concat(className))
}), children), /*#__PURE__*/React.createElement(_JSXStyle, {
id: "3258981770",
dynamic: [useRow ? 'col' : 'column', useRow ? 0 : groupGap, useRow ? groupGap : 0, fontSize]
}, ".radio-group.__jsx-style-dynamic-selector{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-direction:".concat(useRow ? 'col' : 'column', ";-ms-flex-direction:").concat(useRow ? 'col' : 'column', ";flex-direction:").concat(useRow ? 'col' : 'column', ";}.radio-group.__jsx-style-dynamic-selector .radio{margin-top:").concat(useRow ? 0 : groupGap, ";margin-left:").concat(useRow ? groupGap : 0, ";--radio-size:").concat(fontSize, ";}.radio-group.__jsx-style-dynamic-selector .radio:first-of-type{margin:0;}")));
};
export default withDefaults(RadioGroup, defaultProps);