Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: initial badge component #1004

Merged
merged 31 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a8aaad9
feat: initial badge component
dkonieczek Feb 16, 2024
af8eac5
refactor: separate badge components and wrappers, general cleanup
dkonieczek Feb 22, 2024
781d8f0
refactor: fix recs, cleanup
dkonieczek Feb 23, 2024
fa9c638
feat: badge component cleanup, add tests, storybook files, docs
dkonieczek Feb 28, 2024
3dac81e
docs: add last missing docs, fix failing test
dkonieczek Feb 29, 2024
1f7516b
chore: merge develop and resolve conflicts
dkonieczek Mar 6, 2024
0c028b1
chore: fix failing build and tests
dkonieczek Mar 6, 2024
c292db2
refactor: remove SearchBadgeStore from root stores
dkonieczek Mar 26, 2024
fe1dc82
chore: merge in develop
dkonieczek Mar 26, 2024
2d4b918
refactor: use new meta types, pairing refactor
dkonieczek Mar 28, 2024
4f30e6e
refactor: pr feedback: add MetaStore and SearchResultStore tests, pri…
dkonieczek Apr 3, 2024
6974a0e
refactor: remove css properties from MetaStore, cleanup
dkonieczek Apr 4, 2024
1316a97
chore: merge develop and rebuild package-lock
dkonieczek Apr 4, 2024
6ffb28b
feat: add BadgePill and BadgeRectangle components
dkonieczek Apr 17, 2024
26fdb4b
chore: add package-lock
dkonieczek Apr 17, 2024
3ba9fd9
fix(preact-components-badges): some small bugfixes to new badge compo…
chrisFrazier77 Apr 29, 2024
bb1842b
fix BadgeOverlay and BadgeCallout stories
dkonieczek Apr 30, 2024
c06ec17
fix: update badges to latest meta changes
dkonieczek May 6, 2024
f3accd2
refactor(badges): polish on badges to support stacked badges and grou…
korgon May 9, 2024
daf2e3c
Merge branch 'develop' into badges
korgon May 9, 2024
0541342
test: fixing tests after branch merger
korgon May 9, 2024
ec34389
refactor(preact-components/badges): adding 'renderEmpty' prop and fur…
korgon May 9, 2024
a53d718
refactor(store-mobx/storage): changing typing around StorageType enum…
korgon May 9, 2024
2689f9b
refactor(preact-demo): refactoring siteId switching and adding abilit…
korgon May 9, 2024
fc72bbc
fix(preact-components/overlaybadge): restoring lost positioning of th…
korgon May 9, 2024
8841830
test(store-mobx/storage): fixing test from previous enum refactor
korgon May 9, 2024
30b4f5a
fix: add right css property to OverlayBadge
dkonieczek May 13, 2024
9cc4a72
fix: remove getTag helper
dkonieczek May 13, 2024
9db11fb
fix: only lift badges if an object
dkonieczek May 13, 2024
c977223
fix: pr feedback
dkonieczek May 14, 2024
0daf2d8
fix: remove opacity from BadgePill and BadgeRectangle color default
dkonieczek May 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,6 @@ export default {
},
control: { type: 'text' },
},
overflow: {
description: 'Allow badge image to overflow grid cell',
table: {
type: {
summary: 'boolean',
},
defaultValue: { summary: true },
},
control: { type: 'boolean' },
},
...componentArgs,
dkonieczek marked this conversation as resolved.
Show resolved Hide resolved
},
};
Expand All @@ -72,5 +62,4 @@ export const Default = (args: BadgeImageProps) => <BadgeImage {...args} />;
Default.args = {
url: '//cdn.searchspring.net/ajax_search/img/star-badge-new-blue.png',
label: 'placeholder badge image',
constrain: true,
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,6 @@ describe('BadgeImage Component', () => {
expect(BadgeImg).toHaveAttribute('alt', BADGE_LABEL);
});

dkonieczek marked this conversation as resolved.
Show resolved Hide resolved
it('renders BadgeImage with overflow prop true', () => {
const rendered = render(<BadgeImage url={BADGE_URL} label={BADGE_LABEL} overflow={true} />);
const BadgeImg = rendered.container.querySelector('.ss__badge-image')!;
expect(BadgeImg).toBeInTheDocument();

const styles = getComputedStyle(BadgeImg);

expect(styles.maxHeight).toBe('100%');
expect(styles.maxWidth).toBe('100%');
});

it('renders BadgeImage with overflow prop false', () => {
const rendered = render(<BadgeImage url={BADGE_URL} label={BADGE_LABEL} overflow={false} />);
const BadgeImg = rendered.container.querySelector('.ss__badge-image')!;
expect(BadgeImg).toBeInTheDocument();

const styles = getComputedStyle(BadgeImg);

expect(styles.maxHeight).toBe('');
expect(styles.maxWidth).toBe('unset');
});

describe('BadgeImage theming works', () => {
dkonieczek marked this conversation as resolved.
Show resolved Hide resolved
it('is themeable with ThemeProvider', () => {
const globalTheme = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import { Theme, useTheme, CacheProvider } from '../../../providers';
import { ComponentProps, StylingCSS } from '../../../types';

const CSS = {
BadgeImage: (props: BadgeImageProps) => {
BadgeImage: () => {
return css({
maxHeight: props.overflow ? '100%' : undefined,
maxWidth: props.overflow ? '100%' : 'unset',
maxHeight: '100%',
maxWidth: '100%',
});
},
};
Expand All @@ -22,7 +22,6 @@ export const BadgeImage = observer((properties: BadgeImageProps): JSX.Element =>

const props: BadgeImageProps = {
// default props
overflow: false,
// global theme
...globalTheme?.components?.badgeImage,
// props
Expand All @@ -34,7 +33,7 @@ export const BadgeImage = observer((properties: BadgeImageProps): JSX.Element =>
const styling: { css?: StylingCSS } = {};

if (!disableStyles) {
styling.css = [CSS.BadgeImage(props), style];
styling.css = [CSS.BadgeImage(), style];
} else if (style) {
styling.css = [style];
}
Expand All @@ -51,5 +50,4 @@ export const BadgeImage = observer((properties: BadgeImageProps): JSX.Element =>
export interface BadgeImageProps extends ComponentProps {
url: string;
label?: string;
overflow?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,3 @@ The `label` prop specifies the badge image `alt` attribute.
```jsx
<BadgeImage label={'example badge'} src={'/images/example.png'} />
dkonieczek marked this conversation as resolved.
Show resolved Hide resolved
```

### overflow

The `overflow` prop is disabled by default. It allows a large sized badge image to overflow its grid cell position and therefore overlap badges in other overlay position.

```jsx
<BadgeImage overflow={false} src={'/images/example.png'} />
```
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default {
),
],
argTypes: {
label: {
value: {
description: 'Badge text contents',
type: { required: true },
table: {
Expand Down Expand Up @@ -64,24 +64,13 @@ export default {
},
control: { type: 'color' },
},
overflow: {
description: 'Allow badge sizing to overflow grid cell',
table: {
type: {
summary: 'boolean',
},
defaultValue: { summary: true },
},
control: { type: 'boolean' },
},
...componentArgs,
},
};

export const Default = (args: BadgeTextProps) => <BadgeText {...args} />;
Default.args = {
label: '30% Off',
value: '30% Off',
color: '#0000FF',
colorText: '#FFFFFF',
overflow: true,
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { ThemeProvider } from '../../../providers';

import { BadgeText } from './BadgeText';

const BADGE_LABEL = 'example badge';
const BADGE_VALUE = 'example badge';

describe('BadgeText Component', () => {
it('renders BadgeText with label text', () => {
const rendered = render(<BadgeText label={BADGE_LABEL} />);
it('renders BadgeText with value text', () => {
const rendered = render(<BadgeText value={BADGE_VALUE} />);
const BadgeTextEl = rendered.container.querySelector('.ss__badge-text')!;
expect(BadgeTextEl).toBeInTheDocument();
expect(BadgeTextEl).toHaveTextContent(BADGE_LABEL);
expect(BadgeTextEl).toHaveTextContent(BADGE_VALUE);

const styles = getComputedStyle(BadgeTextEl);

Expand All @@ -24,7 +24,7 @@ describe('BadgeText Component', () => {
const color = 'rgb(255, 0, 0)';
const colorText = 'rgb(14, 14, 14)';

const rendered = render(<BadgeText label={BADGE_LABEL} color={color} colorText={colorText} />);
const rendered = render(<BadgeText value={BADGE_VALUE} color={color} colorText={colorText} />);
const BadgeTextEl = rendered.container.querySelector('.ss__badge-text')!;
expect(BadgeTextEl).toBeInTheDocument();

Expand All @@ -34,32 +34,6 @@ describe('BadgeText Component', () => {
expect(styles.color).toBe(colorText);
});

dkonieczek marked this conversation as resolved.
Show resolved Hide resolved
it('renders BadgeText with overflow prop true', () => {
const rendered = render(<BadgeText label={BADGE_LABEL} overflow={true} />);
const BadgeTextEl = rendered.container.querySelector('.ss__badge-text')!;
expect(BadgeTextEl).toBeInTheDocument();

const styles = getComputedStyle(BadgeTextEl);

expect(styles.textOverflow).toBe('ellipsis');
expect(styles.whiteSpace).toBe('nowrap');
expect(styles.overflow).toBe('hidden');
expect(styles.maxWidth).toBe('200%');
});

it('renders BadgeText with overflow prop false', () => {
const rendered = render(<BadgeText label={BADGE_LABEL} overflow={false} />);
const BadgeTextEl = rendered.container.querySelector('.ss__badge-text')!;
expect(BadgeTextEl).toBeInTheDocument();

const styles = getComputedStyle(BadgeTextEl);

expect(styles.textOverflow).toBe('');
expect(styles.whiteSpace).toBe('');
expect(styles.overflow).toBe('');
expect(styles.maxWidth).toBe('');
});

describe('BadgeText theming works', () => {
it('is themeable with ThemeProvider', () => {
const globalTheme = {
Expand All @@ -71,7 +45,7 @@ describe('BadgeText Component', () => {
};
const rendered = render(
<ThemeProvider theme={globalTheme}>
<BadgeText label={BADGE_LABEL} />
<BadgeText value={BADGE_VALUE} />
</ThemeProvider>
);
const element = rendered.container.querySelector('.ss__badge-text');
Expand All @@ -87,7 +61,7 @@ describe('BadgeText Component', () => {
},
},
};
const rendered = render(<BadgeText label={BADGE_LABEL} theme={propTheme} />);
const rendered = render(<BadgeText value={BADGE_VALUE} theme={propTheme} />);
const element = rendered.container.querySelector('.ss__badge-text');
expect(element).toBeInTheDocument();
expect(element).toHaveClass(propTheme.components.badgeText.className);
Expand All @@ -98,30 +72,30 @@ describe('BadgeText Component', () => {
components: {
badgeText: {
className: 'classy',
label: 'oooo',
value: 'oooo',
},
},
};
const propTheme = {
components: {
badgeText: {
className: 'classier',
label: 'ahhhh',
value: 'ahhhh',
},
},
};
const rendered = render(
<ThemeProvider theme={globalTheme}>
<BadgeText label={BADGE_LABEL} theme={propTheme} />
<BadgeText value={BADGE_VALUE} theme={propTheme} />
</ThemeProvider>
);

const element = rendered.container.querySelector('.ss__badge-text');
expect(element).toBeInTheDocument();
expect(element).toHaveClass(propTheme.components.badgeText.className);
expect(element).not.toHaveClass(globalTheme.components.badgeText.className);
expect(element).toHaveTextContent(propTheme.components.badgeText.label);
expect(element).not.toHaveTextContent(globalTheme.components.badgeText.label);
expect(element).toHaveTextContent(propTheme.components.badgeText.value);
expect(element).not.toHaveTextContent(globalTheme.components.badgeText.value);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,20 @@ import { Fragment, h } from 'preact';
import { jsx, css } from '@emotion/react';
import classnames from 'classnames';
import { observer } from 'mobx-react-lite';
import type { CSSObject } from '@emotion/serialize';
import { Theme, useTheme, CacheProvider } from '../../../providers';
import { ComponentProps, StylingCSS } from '../../../types';

const CSS = {
BadgeText: (props: BadgeTextProps) => {
const overflow: CSSObject = props.overflow
? {
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
overflow: 'hidden',
maxWidth: '200%',
}
: {};

return css({
display: 'inline-block',
padding: '0.2em 0.5em',
background: props.color,
color: props.colorText,
...overflow,
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
overflow: 'hidden',
maxWidth: '100%',
});
},
};
Expand All @@ -34,7 +27,6 @@ export const BadgeText = observer((properties: BadgeTextProps): JSX.Element => {

const props: BadgeTextProps = {
// default props
overflow: true,
color: 'rgba(255, 255, 255, 0.5)',
colorText: '#000000',
// global theme
dkonieczek marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -43,7 +35,7 @@ export const BadgeText = observer((properties: BadgeTextProps): JSX.Element => {
...properties,
...properties.theme?.components?.badgeText,
};
const { label, disableStyles, className, style } = props;
const { value, disableStyles, className, style } = props;

const styling: { css?: StylingCSS } = {};

Expand All @@ -53,10 +45,10 @@ export const BadgeText = observer((properties: BadgeTextProps): JSX.Element => {
styling.css = [style];
}

return label ? (
return value ? (
<CacheProvider>
<div {...styling} className={classnames('ss__badge-text', className)}>
{label}
{value}
</div>
</CacheProvider>
) : (
Expand All @@ -65,8 +57,7 @@ export const BadgeText = observer((properties: BadgeTextProps): JSX.Element => {
});

export interface BadgeTextProps extends ComponentProps {
label: string;
value: string;
color?: string;
colorText?: string;
overflow?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,24 @@ Renders a text badge. It is expected to be used with `CalloutBadge` and `Overlay

## Usage

### label
The required `label` prop specifies the badge text contents.
### value
The required `value` prop specifies the badge text contents.

```jsx
<BadgeText label={'30% Off'} />
<BadgeText value={'30% Off'} />
```

### color
The `color` prop specifies the badge background color.

```jsx
<BadgeText color={'rgba(255, 255, 255, 0.5)'} label={'30% Off'} />
<BadgeText color={'rgba(255, 255, 255, 0.5)'} value={'30% Off'} />
```

### colorText
The `colorText` prop specifies the badge text color.

```jsx
<BadgeText colorText={'#000000'} label={'30% Off'} />
<BadgeText colorText={'#000000'} value={'30% Off'} />
```
dkonieczek marked this conversation as resolved.
Show resolved Hide resolved

### overflow

The `overflow` prop is enabled by default. It allows a badge with long text content to overflow its grid cell position and therefore overlap badges in other overlay position.

```jsx
<BadgeText overflow={true} label={'30% Off'} />
```
Loading