UNPKG

lucid-ui

Version:

A UI component library from AppNexus.

91 lines (82 loc) 4.99 kB
import _get from "lodash/get"; import _keys from "lodash/keys"; import _map from "lodash/map"; import _first from "lodash/first"; function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } import React from 'react'; import PropTypes from 'react-peek/prop-types'; import { lucidClassNames } from '../../util/style-helpers'; import { findTypes, omitProps } from '../../util/component-types'; import Checkbox, { defaultProps as defaultPropsCheckbox } from '../Checkbox/Checkbox'; var cx = lucidClassNames.bind('&-CheckboxLabeled'); var any = PropTypes.any, node = PropTypes.node, object = PropTypes.object, string = PropTypes.string, bool = PropTypes.bool, func = PropTypes.func; var Label = function Label(props) { return null; }; Label.displayName = 'CheckboxLabeled.Label'; Label.peek = { description: "\n\t\tRenders a `label` for the `Checkbox`\n\t", categories: ['controls', 'toggles'], madeFrom: ['Checkbox'] }; Label.propName = 'Label'; Label.propTypes = { children: node }; export var CheckboxLabeled = function CheckboxLabeled(props) { var className = props.className, isIndeterminate = props.isIndeterminate, isDisabled = props.isDisabled, isSelected = props.isSelected, onSelect = props.onSelect, style = props.style, passThroughs = _objectWithoutProperties(props, ["className", "isIndeterminate", "isDisabled", "isSelected", "onSelect", "style"]); var labelChildProps = _first(_map(findTypes(props, CheckboxLabeled.Label), 'props')); return /*#__PURE__*/React.createElement("label", { className: cx('&', { '&-is-disabled': isDisabled, '&-is-selected': isIndeterminate || isSelected }, className), style: style }, /*#__PURE__*/React.createElement(Checkbox, _extends({ className: cx('&-Checkbox', className), isDisabled: isDisabled, isIndeterminate: isIndeterminate, isSelected: isSelected, onSelect: onSelect }, omitProps(passThroughs, undefined, _keys(CheckboxLabeled.propTypes), false))), /*#__PURE__*/React.createElement("div", _extends({}, labelChildProps, { className: cx('&-label', _get(labelChildProps, 'className', null)) }))); }; CheckboxLabeled.displayName = 'CheckboxLabeled'; CheckboxLabeled.peek = { description: "\n\t", notes: { overview: "\n\t\t\tA square two-state toggle with a label that explains the action or selection. This is a composite of `Checkbox` and the native\n\t\t\t`label` element.\n\t\t", intendedUse: "\n\t\t\tUse checkboxes to allow users to select one or more items. Commonly used to select filters or settings. For interactions where users can only select one option, use `RadioButtonLabeled`.\n\t\t", technicalRecommendations: "\n\t\t\t- Use the styles on the `CheckboxLabeled` parent container to ensure only the checkboxes and their labels are clickable.\n\t\t\t- Use the Selected state when a filter or setting will be applied.\n\t\t\t- Use the Unselected state when a filter or setting will not be applied.\n\t\t\t- Use the Indeterminate state for parent checkboxes where some of the child checkboxes are Selected and some are Unselected. For example, the master checkbox in the header row of the interactive table example in `DataTable`.\n\t\t\t- You can have the label as a child or a prop depending on the needs of your application. \n\t\t" }, categories: ['controls', 'toggles'], madeFrom: ['Checkbox'] }; CheckboxLabeled.defaultProps = defaultPropsCheckbox; // Can't just `...Checkbox.propTypes` anymore because of the way we have to // handle default props. They are duplicated here on purpose which is okay // since in the future we'll be removing proptypes in favor is just typescript. CheckboxLabeled.propTypes = { isIndeterminate: bool, isDisabled: bool, isSelected: bool, onSelect: func, className: string, style: object, Label: any }; CheckboxLabeled.Label = Label; export default CheckboxLabeled;