UNPKG

@fluentui/react-northstar

Version:
188 lines (186 loc) 6.51 kB
import _invoke from "lodash/invoke"; import * as PropTypes from 'prop-types'; import * as React from 'react'; import * as customPropTypes from '@fluentui/react-proptypes'; import { pillBehavior } from '@fluentui/accessibility'; import { commonPropTypes, createShorthand } from '../../utils'; import { useAccessibility, getElementType, useStyles, useTelemetry, useFluentContext, useUnhandledProps, useAutoControlled } from '@fluentui/react-bindings'; import { PillContent } from './PillContent'; import { PillAction } from './PillAction'; import { usePillContext } from './pillContext'; import { PillImage } from './PillImage'; import { PillIcon } from './PillIcon'; import { CheckmarkCircleIcon, AcceptIcon } from '@fluentui/react-icons-northstar'; export var pillClassName = 'ui-pill'; /** * Pills should be used when representing an input, as a way to filter content, or to represent an attribute. */ export var Pill = /*#__PURE__*/function () { var Pill = /*#__PURE__*/React.forwardRef(function (props, ref) { var context = useFluentContext(); var _useTelemetry = useTelemetry(Pill.displayName, context.telemetry), setStart = _useTelemetry.setStart, setEnd = _useTelemetry.setEnd; setStart(); var parentProps = usePillContext(); var className = props.className, design = props.design, styles = props.styles, variables = props.variables, appearance = props.appearance, size = props.size, rectangular = props.rectangular, children = props.children, content = props.content, disabled = props.disabled, action = props.action, actionable = props.actionable, image = props.image, icon = props.icon, selectable = props.selectable, selectedIndicator = props.selectedIndicator, role = props.role, onDismiss = props.onDismiss; var _useAutoControlled = useAutoControlled({ defaultValue: props.defaultSelected, value: props.selected, initialValue: false }), selected = _useAutoControlled[0], setSelected = _useAutoControlled[1]; var ElementType = getElementType(props); var unhandledProps = useUnhandledProps(Pill.handledProps, props); var handleDismiss = function handleDismiss(e) { _invoke(props, 'onDismiss', e, props); }; var handleClick = function handleClick(e) { if (selectable) { setSelected(function (prevSelected) { return !prevSelected; }); _invoke(props, 'onSelectionChange', e, Object.assign({}, props, { selected: !selected })); } _invoke(props, 'onClick', e, props); }; var getA11yProps = useAccessibility(props.accessibility || parentProps.pillBehavior || pillBehavior, { debugName: Pill.displayName, actionHandlers: { performDismiss: handleDismiss, performClick: handleClick }, mapPropsToBehavior: function mapPropsToBehavior() { return { actionable: actionable, selectable: selectable, selected: selected, role: role, dismissible: Boolean(onDismiss) }; }, rtl: context.rtl }); var _useStyles = useStyles(Pill.displayName, { className: pillClassName, mapPropsToStyles: function mapPropsToStyles() { return { appearance: appearance, size: size, rectangular: rectangular, disabled: disabled, selectable: selectable, selected: selected, actionable: actionable }; }, mapPropsToInlineStyles: function mapPropsToInlineStyles() { return { className: className, design: design, styles: styles, variables: variables }; }, rtl: context.rtl }), classes = _useStyles.classes; var getSelectedIndicator = function getSelectedIndicator() { if (!!selectedIndicator) { return selectedIndicator; } if (!!image) { return /*#__PURE__*/React.createElement(CheckmarkCircleIcon, null); } return /*#__PURE__*/React.createElement(AcceptIcon, null); }; var element = getA11yProps.unstable_wrapWithFocusZone( /*#__PURE__*/React.createElement(ElementType, getA11yProps('root', Object.assign({ className: classes.root, ref: ref }, (actionable || selectable) && { onClick: handleClick }, unhandledProps)), selectable && selected && createShorthand(PillIcon, getSelectedIndicator(), { defaultProps: function defaultProps() { return { size: size, selectable: selectable, image: image }; } }), !selected && createShorthand(PillImage, image, { defaultProps: function defaultProps() { return { size: size }; } }), !selected && createShorthand(PillIcon, icon, { defaultProps: function defaultProps() { return { size: size }; } }), createShorthand(PillContent, content || {}, { defaultProps: function defaultProps() { return { children: children, size: size, actionable: actionable }; } }), Boolean(onDismiss) && createShorthand(PillAction, action || {}, { overrideProps: function overrideProps(prevProps) { return { onClick: function onClick(e) { _invoke(prevProps, 'onClick', e); handleDismiss(e); } }; } }))); setEnd(); return element; }); Pill.defaultProps = { as: 'span', accessibility: pillBehavior }; Pill.propTypes = Object.assign({}, commonPropTypes.createCommon(), { content: customPropTypes.shorthandAllowingChildren, size: PropTypes.oneOf(['small', 'smaller', 'medium']), rectangular: PropTypes.bool, disabled: PropTypes.bool, appearance: PropTypes.oneOf(['filled', 'inverted', 'outline']), actionable: PropTypes.bool, action: customPropTypes.shorthandAllowingChildren, onDismiss: PropTypes.func, selectedIndicator: customPropTypes.shorthandAllowingChildren, selectable: PropTypes.bool, selected: PropTypes.bool, defaultSelected: PropTypes.bool, image: customPropTypes.shorthandAllowingChildren }); Pill.displayName = 'Pill'; Pill.handledProps = Object.keys(Pill.propTypes); return Pill; }(); //# sourceMappingURL=Pill.js.map