Skip to content

Commit

Permalink
chore(switch): wIP: fix up playwright tests and minor styling issues
Browse files Browse the repository at this point in the history
  • Loading branch information
damienrobson-sage committed Oct 21, 2024
1 parent 8c81497 commit c8f4397
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 26 deletions.
12 changes: 10 additions & 2 deletions src/__internal__/checkable-input/checkable-input.style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import styled, { css } from "styled-components";

import FieldHelpStyle from "../field-help/field-help.style";
import { FieldLineStyle } from "../form-field/form-field.style";
import HiddenCheckableInputStyle from "./hidden-checkable-input.style";
import LabelStyle, { StyledLabelContainer } from "../label/label.style";
import StyledHelp from "../../components/help/help.style";
import StyledValidationIcon from "../validations/validation-icon.style";
import StyledHelp from "../../components/help/help.style";

import HiddenCheckableInputStyle from "./hidden-checkable-input.style";

const StyledCheckableInput = styled.div`
display: inline-block;
Expand All @@ -30,6 +31,12 @@ const StyledCheckableInputWrapper = styled.div<StyledCheckableInputWrapperProps>
labelInline,
reverse,
}) => css`
/* ${inputWidth !== undefined &&
inputWidth !== 0 &&
css` */
width: 100% !important;
/* `} */
${FieldLineStyle} {
display: flex;
}
Expand Down Expand Up @@ -105,6 +112,7 @@ const StyledCheckableInputWrapper = styled.div<StyledCheckableInputWrapperProps>
css`
${StyledCheckableInput} {
width: ${inputWidth}% !important;
min-width: 67px;
}
`}
Expand Down
2 changes: 1 addition & 1 deletion src/components/switch/__internal__/switch-slider.style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ const StyledSwitchSlider = styled.div`
error &&
!disabled &&
css`
border-color: var(--colorsSemanticNegative450);
border-color: var(--colorsSemanticNegative500);
`
}
Expand Down
64 changes: 41 additions & 23 deletions src/components/switch/switch.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,23 @@ export const Switch = React.forwardRef(
);
}

const errorMargin = labelInline && (error || warning) ? 3 : 1;
const defaultMargin = labelInline ? 1 : 0;
const errorMargin = error || warning ? 3 : defaultMargin;
const direction = labelInline ? "row" : "column";
const reverseDirection = labelInline ? "row-reverse" : "column";

let requestedInputWidth = rest.inputWidth;
if (
requestedInputWidth &&
requestedInputWidth <= 1 &&
requestedInputWidth >= 0
)
requestedInputWidth *= 100;
const enforcedInputWidth =
requestedInputWidth && typeof requestedInputWidth === "number"
? `${requestedInputWidth}%`
: rest.inputWidth;

return (
<>
<StyledSwitch {...switchStylePropsForNewValidation}>
Expand All @@ -264,34 +277,38 @@ export const Switch = React.forwardRef(
flexWrap="wrap"
alignItems="flex-start"
flexDirection={!reverse ? reverseDirection : direction}
width={labelInline ? "100%" : "auto"}
>
<Box id="flow-control">
<Label
isDarkBackground={isDarkBackground}
labelId={labelId.current}
disabled={disabled}
isRequired={required}
optional={isOptional}
<Label
isDarkBackground={isDarkBackground}
labelId={labelId.current}
disabled={disabled}
isRequired={required}
optional={isOptional}
>
<Box
data-role="hint-text-wrapper"
mb={labelHelp ? 0 : 1}
mr={reverse ? 0 : 1}
ml={reverse ? 0 : 1}
>
<Box
data-role="hint-text-wrapper"
mb={labelHelp ? 0 : 1}
mr={reverse ? 0 : 1}
ml={reverse ? 0 : 1}
>
{label}
</Box>
</Label>
{labelHelp && (
<StyledHintText data-role="hint-text" id={inputHintId.current}>
{labelHelp}
</StyledHintText>
)}
</Box>
{label}
{labelHelp && (
<StyledHintText
data-role="hint-text"
id={inputHintId.current}
>
{labelHelp}
</StyledHintText>
)}
</Box>
</Label>
<Box
ml={reverse ? errorMargin : rest.ml}
mr={!reverse ? errorMargin : rest.mr}
position="relative"
id="input-wrapper"
width={enforcedInputWidth}
>
<ValidationMessage
error={error}
Expand All @@ -309,6 +326,7 @@ export const Switch = React.forwardRef(
ariaLabelledBy={`${label && labelId.current}`}
ariaDescribedBy={ariaDescribedBy}
{...inputPropsForNewValidation}
// inputWidth={undefined}
fieldHelp={labelInline ? undefined : rest.fieldHelp}
>
<SwitchSlider {...switchSliderPropsForNewValidation} />
Expand Down
7 changes: 7 additions & 0 deletions src/components/switch/switch.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ The folowing examples use the new validation pattern that is available by settin
name="single switch - inline validation with options"
/>

#### With optional props and custom widths

<Canvas
of={SwitchStories.NewValidationInlineDefaultWithOptionsAndCustomWidths}
name="single switch - inline validation with options and custom widths"
/>

#### With error

<Canvas
Expand Down
32 changes: 32 additions & 0 deletions src/components/switch/switch.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,38 @@ export const NewValidationInlineDefaultWithOptions: Story = () => {
NewValidationInlineDefaultWithOptions.storyName =
"New Validation - Inline with options";

export const NewValidationInlineDefaultWithOptionsAndCustomWidths: Story = () => {
return (
<Box m={2}>
<CarbonProvider validationRedesignOptIn>
<Switch
label="90% Width"
labelInline
labelHelp="Hint text"
fieldHelp="Field help"
inputWidth={90}
/>
<Switch
label="70% Width"
labelInline
labelHelp="Hint text"
fieldHelp="Field help"
inputWidth={70}
/>
<Switch
label="20% Width"
labelInline
labelHelp="Hint text"
fieldHelp="Field help"
inputWidth={20}
/>
</CarbonProvider>
</Box>
);
};
NewValidationInlineDefaultWithOptionsAndCustomWidths.storyName =
"New Validation - Inline with options and custom widths";

export const NewValidationInlineError: Story = () => {
return (
<Box m={2}>
Expand Down

0 comments on commit c8f4397

Please sign in to comment.