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: Adding start adornment to Chips #1058

Merged
merged 2 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 23 additions & 0 deletions src/components/Chip.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,26 @@ export function WithIcon() {
</div>
);
}

export function WithCustomLabel() {
return (
<div css={Css.wPx(200).df.gap1.$}>
<Chip
text={
<span css={Css.sm.df.gap1.$}>
<span css={Css.smBd.$}>K</span>
Kitchen
</span>
}
/>
<Chip
text={
<span css={Css.sm.df.gap1.$}>
<span css={Css.smBd.$}>B</span>
Bedroom
</span>
}
/>
</div>
);
}
2 changes: 1 addition & 1 deletion src/components/Chip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const ChipTypes: Record<ChipType, ChipType> = {
};

export interface ChipProps<X> {
text: string;
text: ReactNode;
title?: ReactNode;
xss?: X;
type?: ChipType;
Expand Down
8 changes: 8 additions & 0 deletions src/inputs/ToggleChipGroup.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ export function ToggleChipGroups() {
values={selectedValues}
onChange={setSelectedValues}
/>
<p css={Css.mt2.mb2.$}>Start Adornment:</p>
<ToggleChipGroup
label="Select Markets"
labelStyle="left"
options={options.map((o) => ({ ...o, startAdornment: <span css={Css.smBd.$}>{o.label.slice(0, 2)}</span> }))}
values={selectedValues}
onChange={setSelectedValues}
/>
</>
);
}
10 changes: 8 additions & 2 deletions src/inputs/ToggleChipGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type ToggleChipXss = Xss<"color" | "backgroundColor">;
type ToggleChipItemProps = {
label: string;
value: string;
startAdornment?: ReactNode;
/**
* Whether the interactive element is disabled.
*
Expand Down Expand Up @@ -55,6 +56,7 @@ export function ToggleChipGroup(props: ToggleChipGroupProps) {
selected={state.value.includes(o.value)}
label={o.label}
disabled={o.disabled}
startAdornment={o.startAdornment}
xss={xss}
{...tid[o.value]}
/>
Expand All @@ -75,11 +77,12 @@ interface ToggleChipProps {
* If a ReactNode, it's treated as a "disabled reason" that's shown in a tooltip.
*/
disabled?: boolean | ReactNode;
startAdornment?: ReactNode;
xss?: ToggleChipXss;
}

function ToggleChip(props: ToggleChipProps) {
const { label, value, groupState, selected: isSelected, disabled = false, xss, ...others } = props;
const { label, value, groupState, selected: isSelected, disabled = false, startAdornment, xss, ...others } = props;
const isDisabled = !!disabled;
const ref = useRef(null);
const { inputProps } = useCheckboxGroupItem({ value, "aria-label": label, isDisabled }, groupState, ref);
Expand Down Expand Up @@ -109,7 +112,10 @@ function ToggleChip(props: ToggleChipProps) {
<VisuallyHidden>
<input {...inputProps} {...focusProps} />
</VisuallyHidden>
{label}
<div css={Css.df.gap1.$}>
{startAdornment}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JonnCharpentier same push, I think label: ReactNode is a simpler/more extensible change to ToggleChip than a new startAdornment that isn't that complicated / value-adding...

(I.e. we do have things like TextField.startAdornment, but it's more intricate/complicated to place the adornment in "the right spot" of the input field, so it makes more sense for startAdornment to be a pattern there, vs. here where we could just as well send in some raw HTML.)

Copy link
Contributor Author

@JonnCharpentier JonnCharpentier Aug 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephenh we need options.label to be a string, because we use it for aria-label.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see 🤔 ... that makes sense

{label}
</div>
</label>
),
});
Expand Down
Loading