UNPKG

@geezee/react-ui

Version:

Modern and minimalist React UI library.

81 lines (72 loc) 5.39 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"; import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; import _JSXStyle from "styled-jsx/style"; import React, { useCallback, useMemo, useState } from 'react'; import useTheme from '../styles/use-theme'; import withDefaults from '../utils/with-defaults'; import useCurrentState from '../utils/use-current-state'; import { FieldsetContext } from './fieldset-context'; import useWarning from '../utils/use-warning'; var defaultProps = { className: '' }; var FieldsetGroup = function FieldsetGroup(_ref) { var className = _ref.className, children = _ref.children, value = _ref.value, onChange = _ref.onChange, props = _objectWithoutProperties(_ref, ["className", "children", "value", "onChange"]); var theme = useTheme(); var _useState = useState(value), _useState2 = _slicedToArray(_useState, 2), selfVal = _useState2[0], setSelfVal = _useState2[1]; var _useCurrentState = useCurrentState([]), _useCurrentState2 = _slicedToArray(_useCurrentState, 3), items = _useCurrentState2[0], setItems = _useCurrentState2[1], ref = _useCurrentState2[2]; var register = function register(newItem) { var hasItem = ref.current.find(function (item) { return item.value === newItem.value; }); if (hasItem) { useWarning('The "value" of each "Fieldset" must be unique.', 'Fieldset'); } setItems([].concat(_toConsumableArray(ref.current), [newItem])); }; var providerValue = useMemo(function () { return { currentValue: selfVal, inGroup: true, register: register }; }, [selfVal]); var clickHandle = useCallback(function (nextValue) { setSelfVal(nextValue); onChange && onChange(nextValue); }, [onChange]); return /*#__PURE__*/React.createElement(FieldsetContext.Provider, { value: providerValue }, /*#__PURE__*/React.createElement("div", _extends({}, props, { className: _JSXStyle.dynamic([["1142704358", [theme.palette.accents_3, theme.palette.accents_1, theme.layout.gap, theme.palette.border, theme.palette.background, theme.palette.foreground, theme.expressiveness.R2, theme.expressiveness.R2]]]) + " " + (props && props.className != null && props.className || " ".concat(className)) }), /*#__PURE__*/React.createElement("div", { className: _JSXStyle.dynamic([["1142704358", [theme.palette.accents_3, theme.palette.accents_1, theme.layout.gap, theme.palette.border, theme.palette.background, theme.palette.foreground, theme.expressiveness.R2, theme.expressiveness.R2]]]) + " " + "group" }, items.map(function (item) { return /*#__PURE__*/React.createElement("button", { onClick: function onClick() { return clickHandle(item.value); }, key: item.value, className: _JSXStyle.dynamic([["1142704358", [theme.palette.accents_3, theme.palette.accents_1, theme.layout.gap, theme.palette.border, theme.palette.background, theme.palette.foreground, theme.expressiveness.R2, theme.expressiveness.R2]]]) + " " + ((selfVal === item.value ? 'active' : '') || "") }, item.label); })), /*#__PURE__*/React.createElement("div", { className: _JSXStyle.dynamic([["1142704358", [theme.palette.accents_3, theme.palette.accents_1, theme.layout.gap, theme.palette.border, theme.palette.background, theme.palette.foreground, theme.expressiveness.R2, theme.expressiveness.R2]]]) + " " + "group-content" }, children), /*#__PURE__*/React.createElement(_JSXStyle, { id: "1142704358", dynamic: [theme.palette.accents_3, theme.palette.accents_1, theme.layout.gap, theme.palette.border, theme.palette.background, theme.palette.foreground, theme.expressiveness.R2, theme.expressiveness.R2] }, ".group.__jsx-style-dynamic-selector{white-space:nowrap;overflow-y:hidden;overflow-x:auto;margin-bottom:-1px;}.group-content.__jsx-style-dynamic-selector{border-top-left-radius:0;overflow:hidden;}.group-content.__jsx-style-dynamic-selector .fieldset{border-top-left-radius:0;}button.__jsx-style-dynamic-selector{height:34px;text-align:center;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;color:".concat(theme.palette.accents_3, ";background-color:").concat(theme.palette.accents_1, ";font-size:0.875rem;white-space:nowrap;text-transform:capitalize;line-height:0;-webkit-appearance:none;cursor:pointer;margin:0;padding:0 ").concat(theme.layout.gap, ";overflow:hidden;-webkit-transition:all 0.2s ease 0s;transition:all 0.2s ease 0s;border-radius:0;border:1px solid ").concat(theme.palette.border, ";-webkit-text-decoration:none;text-decoration:none;outline:none;}button.active.__jsx-style-dynamic-selector{border-bottom-color:transparent;background-color:").concat(theme.palette.background, ";color:").concat(theme.palette.foreground, ";cursor:default;}button.__jsx-style-dynamic-selector:first-of-type{border-top-left-radius:").concat(theme.expressiveness.R2, ";}button.__jsx-style-dynamic-selector:last-of-type{border-top-right-radius:").concat(theme.expressiveness.R2, ";}button.__jsx-style-dynamic-selector+button.__jsx-style-dynamic-selector{border-left:0;}")))); }; export default withDefaults(FieldsetGroup, defaultProps);