From 3bce8c32118f7d9147bf5b6cf8cf1644f26fc3b5 Mon Sep 17 00:00:00 2001 From: Anuradha Aggarwal Date: Fri, 28 Jul 2023 16:22:37 +0530 Subject: [PATCH] feat(chip): add label prefix option for custom label --- .../atoms/_chip/__tests__/_chip.test.tsx | 10 +++ core/components/atoms/_chip/index.tsx | 21 ++++- core/components/atoms/chip/Chip.tsx | 7 +- .../chip/__stories__/CustomLabel.story.jsx | 85 ++++++------------- 4 files changed, 59 insertions(+), 64 deletions(-) diff --git a/core/components/atoms/_chip/__tests__/_chip.test.tsx b/core/components/atoms/_chip/__tests__/_chip.test.tsx index 408283ad3b..4d28207383 100644 --- a/core/components/atoms/_chip/__tests__/_chip.test.tsx +++ b/core/components/atoms/_chip/__tests__/_chip.test.tsx @@ -87,3 +87,13 @@ describe('Chip component', () => { expect(getByTestId('DesignSystem-GenericChip--clearButton')).toHaveClass('Chip-icon-disabled--right'); }); }); + +describe('Chip component label prefix test', () => { + it('renders label prefix text with prop:labelPrefix', () => { + const { getByTestId } = render(); + const labelPrefixElement = getByTestId('DesignSystem-GenericChip--LabelPrefix'); + expect(labelPrefixElement).toBeInTheDocument(); + expect(labelPrefixElement).toHaveTextContent('ChipLabelPrefix'); + expect(labelPrefixElement).toHaveClass('Text--medium'); + }); +}); diff --git a/core/components/atoms/_chip/index.tsx b/core/components/atoms/_chip/index.tsx index 4203b94885..05fef74eca 100644 --- a/core/components/atoms/_chip/index.tsx +++ b/core/components/atoms/_chip/index.tsx @@ -8,6 +8,7 @@ import { IconProps, TextProps } from '@/index.type'; export interface GenericChipProps extends BaseProps { label: string | React.ReactElement; + labelPrefix?: string; icon?: string; clearButton?: boolean; disabled?: boolean; @@ -18,7 +19,7 @@ export interface GenericChipProps extends BaseProps { } export const GenericChip = (props: GenericChipProps) => { - const { label, icon, clearButton, disabled, className, selected, onClose, onClick } = props; + const { label, icon, clearButton, disabled, className, selected, onClose, onClick, labelPrefix } = props; const baseProps = extractBaseProps(props); @@ -65,9 +66,21 @@ export const GenericChip = (props: GenericChipProps) => { const renderLabel = () => { if (typeof label === 'string') { return ( - - {label} - + <> + {labelPrefix && ( + + {labelPrefix} + + )} + + {label} + + ); } return label; diff --git a/core/components/atoms/chip/Chip.tsx b/core/components/atoms/chip/Chip.tsx index 7aa0614516..d8603dfc63 100644 --- a/core/components/atoms/chip/Chip.tsx +++ b/core/components/atoms/chip/Chip.tsx @@ -11,6 +11,10 @@ export interface ChipProps extends BaseProps { * Label of chip */ label: string | React.ReactElement; + /** + * Adds Prefix for label + */ + labelPrefix?: string; /** * Type of material `Icon` */ @@ -46,7 +50,7 @@ export interface ChipProps extends BaseProps { } export const Chip = (props: ChipProps) => { - const { label, icon, clearButton, type, disabled, selected, onClose, onClick, name, className } = props; + const { label, icon, clearButton, type, disabled, selected, onClose, onClick, name, className, labelPrefix } = props; const baseProps = extractBaseProps(props); @@ -85,6 +89,7 @@ export const Chip = (props: ChipProps) => { onClose={onCloseHandler} onClick={onClickHandler} name={name} + labelPrefix={labelPrefix} /> ); }; diff --git a/core/components/atoms/chip/__stories__/CustomLabel.story.jsx b/core/components/atoms/chip/__stories__/CustomLabel.story.jsx index 56e462dd54..6dc2f3674b 100644 --- a/core/components/atoms/chip/__stories__/CustomLabel.story.jsx +++ b/core/components/atoms/chip/__stories__/CustomLabel.story.jsx @@ -1,63 +1,39 @@ import * as React from 'react'; -import { Chip, Text } from '@/index'; +import { Chip } from '@/index'; export const customLabel = () => { const customLabelData1 = [ - ['Age=', '60yrs', { selected: true, type: 'selection', label1: 'primary-dark', label2: 'primary-dark' }], - ['Age:', '18yrs to 60yrs', { selected: false, type: 'action', label1: '', label2: '' }], - ['Age>=', '18yrs', { selected: false, type: 'input', label1: '', label2: '' }], + ['Age=', '60yrs', { selected: true }], + ['Age:', '18yrs to 60yrs', { selected: false }], + ['Age>=', '18yrs', { selected: false, disabled: true }], ]; const customLabelData2 = [ - [ - 'Provider City:', - 'San Diego', - { selected: true, type: 'selection', label1: 'primary-dark', label2: 'primary-dark' }, - ], - ['Provider City:', 'San Diego, Chicago', { selected: false, type: 'action', label1: '', label2: '' }], - ['Provider City:', '3 selected', { selected: false, type: 'input', label1: '', label2: '' }], + ['Provider City:', 'San Diego', { selected: true }], + ['Provider City:', 'San Diego, Chicago', { selected: false }], + ['Provider City:', '3 selected', { selected: false, disabled: true }], ]; const customLabelData3 = [ - [ - 'Creation Date>=', - '01/01/2020', - { selected: true, type: 'selection', label1: 'primary-dark', label2: 'primary-dark' }, - ], - ['Creation Date=', '01/01/2020', { icon: 'assessment', selected: false, type: 'action', label1: '', label2: '' }], - ['Creation Date:', 'last 6 months', { icon: 'assessment', selected: false, type: 'input', label1: '', label2: '' }], + ['Creation Date>=', '01/01/2020', { selected: true }], + ['Creation Date=', '01/01/2020', { icon: 'assessment', selected: false }], + ['Creation Date:', 'last 6 months', { icon: 'assessment', selected: false, disabled: true }], ]; const renderChips = ( <> -
- - Selection Chip - - - Action Chip - - - Input Chip - -
{customLabelData1.map((customLabel, index) => ( - - {customLabel[0]} - - {customLabel[1]} - - } + label={customLabel[1]} + labelPrefix={customLabel[0]} clearButton={true} - name={{ Custom: 'Label' }} - type={customLabel[2].type} + name={customLabel[0]} + type="selection" selected={customLabel[2].selected} + disabled={customLabel[2].disabled} /> ))}
@@ -67,18 +43,13 @@ export const customLabel = () => { icon={customLabel[2].icon} className="mb-6" key={index} - label={ - - - {customLabel[0]} - - {customLabel[1]} - - } + label={customLabel[1]} + labelPrefix={customLabel[0]} clearButton={true} - name={{ Custom: 'Label' }} - type={customLabel[2].type} + name={customLabel[0]} + type="selection" selected={customLabel[2].selected} + disabled={customLabel[2].disabled} /> ))} @@ -87,18 +58,14 @@ export const customLabel = () => { - - {customLabel[0]} - - {customLabel[1]} - - } + label={customLabel[1]} + labelPrefix={customLabel[0]} + icon={customLabel[2].icon} clearButton={true} - name={{ Custom: 'Label' }} - type={customLabel[2].type} + name={customLabel[0]} + type="selection" selected={customLabel[2].selected} + disabled={customLabel[2].disabled} /> ))}