Skip to content

Commit

Permalink
Merge pull request #542 from LifeSG/toggle-fit-content-width
Browse files Browse the repository at this point in the history
[BOOKINGSG-5999][JH] add optional useContentWidth for Toggle & Filter.Checkbox
  • Loading branch information
qroll authored Aug 21, 2024
2 parents 0df8336 + 9387a34 commit 83488bb
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/filter/addons/filter-item-checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const FilterItemCheckbox = <T,>({
onSelect,
labelExtractor,
valueExtractor,
useToggleContentWidth,
...filterItemProps
}: FilterItemCheckboxProps<T>) => {
// =============================================================================
Expand Down Expand Up @@ -180,6 +181,7 @@ export const FilterItemCheckbox = <T,>({
(minimisedHeight && index <= lastVisibleElementIndex)
}
onChange={handleItemClick(option)}
useContentWidth={useToggleContentWidth}
>
{optionLabel}
</StyledToggle>
Expand Down
2 changes: 2 additions & 0 deletions src/filter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,6 @@ export interface FilterItemCheckboxProps<T>
labelExtractor?: ((item: T) => React.ReactNode) | undefined;
/** Function to derive value from an item. If not set, checks `item.value`. */
valueExtractor?: ((item: T) => string) | undefined;
/** Changes min-width of toggle in mobile view to fit content */
useToggleContentWidth?: boolean | undefined;
}
10 changes: 10 additions & 0 deletions src/toggle/toggle.styles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface StyleProps {
interface ContainerStyleProps extends StyleProps {
$styleType?: ToggleStyleType;
$error?: boolean;
$useContentWidth?: boolean;
}

interface IndicatorLabelContainerStyleProps {
Expand Down Expand Up @@ -61,6 +62,15 @@ export const Container = styled.div<ContainerStyleProps>`
}
}}
// Container min width to fit content
${(props) => {
if (props.$useContentWidth) {
return css`
min-width: unset;
`;
}
}}
// Background, Hover and Border style
${(props) => {
switch (props.$styleType) {
Expand Down
2 changes: 2 additions & 0 deletions src/toggle/toggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const Toggle = ({
onRemove,
"data-testid": testId,
onChange,
useContentWidth,
}: ToggleProps) => {
// =============================================================================
// CONST, STATE, REF
Expand Down Expand Up @@ -324,6 +325,7 @@ export const Toggle = ({
$styleType={styleType}
$error={error}
$indicator={indicator}
$useContentWidth={useContentWidth}
id={id}
data-testid={testId}
>
Expand Down
2 changes: 2 additions & 0 deletions src/toggle/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export interface ToggleProps
/** Specifies if the remove button should be displayed */
removable?: boolean | undefined;
onRemove?: (() => void) | undefined;
/** Changes min-width to fit content */
useContentWidth?: boolean | undefined;
}

export interface ToggleCompositeSectionProps {
Expand Down
6 changes: 6 additions & 0 deletions stories/filter/addon-props-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ const FILTER_CHECKBOX_DATA: ApiTableSectionProps[] = [
description: "Called when selection changes",
propTypes: ["(options: T[]) => void"],
},
{
name: "useToggleContentWidth",
description:
"Changes the minimum width of the checkbox toggle to fit its content (on mobile)",
propTypes: ["boolean"],
},
{
name: "labelExtractor",
description: (
Expand Down
6 changes: 6 additions & 0 deletions stories/toggle/props-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ const DATA: ApiTableSectionProps[] = [
),
propTypes: ["CompositeSectionProps"],
},
{
name: "useContentWidth",
description:
"Changes the minimum width of the container to fit its content",
propTypes: ["boolean"],
},
],
},

Expand Down

0 comments on commit 83488bb

Please sign in to comment.