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

Add internal text to switch component #3327

Merged
merged 15 commits into from
Feb 6, 2019
43 changes: 24 additions & 19 deletions packages/core/src/components/forms/_controls.scss
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,12 @@ $control-indicator-spacing: $pt-grid-size !default;
$switch-indicator-margin: 2px !default;
$switch-indicator-size: calc(1em - #{$switch-indicator-margin * 2});

$switch-indicator-text-font-size: 0.6em; //@KEVINNG MAKE THIS RIGHT
$switch-indicator-text-line-height: 1.7em; //@KEVINNG MAKE THIS RIGHT
$switch-indicator-text-outside-margin: 0.75em; //@KEVINNG MAKE THIS RIGHT
$switch-indicator-text-inside-margin: 1.75em; //@KEVINNG MAKE THIS RIGHT
$switch-indicator-child-height: 1em;
$switch-indicator-child-outside-margin: 0.5em;
$switch-indicator-child-inside-margin: 1.2em;

$switch-indicator-text-font-size: 0.5em;
$switch-indicator-text-line-height: 2em; //to match height of parent
giladgray marked this conversation as resolved.
Show resolved Hide resolved

$switch-background-color: rgba($gray4, 0.5) !default;
$switch-background-color-hover: rgba($gray2, 0.5) !default;
Expand Down Expand Up @@ -374,7 +376,7 @@ $control-indicator-spacing: $pt-grid-size !default;

input:checked ~ .#{$ns}-control-indicator::before {
right: 0;
left: auto;
left: calc(100% - 1em);
}

&.#{$ns}-large {
Expand Down Expand Up @@ -408,34 +410,37 @@ $control-indicator-spacing: $pt-grid-size !default;
}
}

.#{$ns}-control-indicator-child {
.#{$ns}-switch-inner-text {
text-align: center;
line-height: $switch-indicator-text-line-height;
giladgray marked this conversation as resolved.
Show resolved Hide resolved
font-size: $switch-indicator-text-font-size;
}

&.#{$ns}-switch-active-text {
.#{$ns}-control-indicator-child {
&:first-child {
visibility: hidden;
margin-right: $switch-indicator-text-inside-margin;
margin-left: $switch-indicator-text-outside-margin;
line-height: 0;
margin-right: $switch-indicator-child-inside-margin;
margin-left: $switch-indicator-child-outside-margin;
height: 0;
giladgray marked this conversation as resolved.
Show resolved Hide resolved
}

&.#{$ns}-switch-inactive-text {
&:last-child {
visibility: visible;
margin-right: $switch-indicator-text-outside-margin;
margin-left: $switch-indicator-text-inside-margin;
line-height: $switch-indicator-text-line-height;
margin-right: $switch-indicator-child-outside-margin;
margin-left: $switch-indicator-child-inside-margin;
height: $switch-indicator-child-height;
}
}

input:checked ~ .#{$ns}-control-indicator {
.#{$ns}-switch-active-text {
input:checked ~ .#{$ns}-control-indicator .#{$ns}-control-indicator-child {
&:first-child {
visibility: visible;
line-height: $switch-indicator-text-line-height;
height: $switch-indicator-child-height;
}

.#{$ns}-switch-inactive-text {
&:last-child {
visibility: hidden;
line-height: 0;
height: 0;
}
}
}
Expand Down
40 changes: 19 additions & 21 deletions packages/core/src/components/forms/controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export interface IControlProps extends IProps, HTMLInputProps {
interface IControlInternalProps extends IControlProps {
type: "checkbox" | "radio";
typeClassName: string;
indicatorChildren?: React.ReactNode[];
indicatorChildren?: React.ReactNode;
}

/**
Expand Down Expand Up @@ -133,39 +133,37 @@ const Control: React.SFC<IControlInternalProps> = ({

export interface ISwitchProps extends IControlProps {
/**
* String to display within the switch control component when the switch is active ("on").
* String to display within the switch control component when the switch is checked/on. Defaults to innerLabel.
giladgray marked this conversation as resolved.
Show resolved Hide resolved
*/
internalTextActive?: string;
innerLabelChecked?: string;

/**
* String to display within the switch control component when the switch is inactive ("off").
* String to display within the switch control component when the switch is unchecked/off. Defaults to "".
giladgray marked this conversation as resolved.
Show resolved Hide resolved
*/
internalTextInactive?: string;
innerLabel?: string;
}

export class Switch extends React.PureComponent<ISwitchProps> {
public static displayName = `${DISPLAYNAME_PREFIX}.Switch`;
private switchText = [
<div
key={`${Classes.SWITCH}-active-text`}
className={classNames(`${Classes.CONTROL_INDICATOR}-child`, `${Classes.SWITCH}-active-text`)}
>
{this.props.internalTextActive}
</div>,
<div
key={`${Classes.SWITCH}-inactive-text`}
className={classNames(`${Classes.CONTROL_INDICATOR}-child`, `${Classes.SWITCH}-inactive-text`)}
>
{this.props.internalTextInactive}
</div>,
];
private childClassName = `${Classes.CONTROL_INDICATOR}-child`;
private textClassName = `${Classes.SWITCH}-inner-text`;
giladgray marked this conversation as resolved.
Show resolved Hide resolved

public render() {
const { innerLabelChecked, innerLabel, ...controlProps } = this.props;
const switchLabels = [
giladgray marked this conversation as resolved.
Show resolved Hide resolved
<div key={`${Classes.SWITCH}-checked-text`} className={this.childClassName}>
<div className={this.textClassName}> {innerLabelChecked ? innerLabelChecked : innerLabel} </div>
giladgray marked this conversation as resolved.
Show resolved Hide resolved
</div>,
<div key={`${Classes.SWITCH}-unchecked-text`} className={this.childClassName}>
giladgray marked this conversation as resolved.
Show resolved Hide resolved
<div className={this.textClassName}> {innerLabel} </div>
giladgray marked this conversation as resolved.
Show resolved Hide resolved
</div>,
];
return (
<Control
{...this.props}
{...controlProps}
type="checkbox"
typeClassName={Classes.SWITCH}
indicatorChildren={this.switchText}
indicatorChildren={switchLabels}
/>
);
}
Expand Down
13 changes: 7 additions & 6 deletions packages/core/test/controls/controlsTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ describe("Controls:", () => {

controlsTests(Switch, "checkbox", Classes.SWITCH, () => {
describe("internal text", () => {
const switchWithText = mount(
<Switch internalTextActive="Active Text" internalTextInactive="Inactive Text" />,
);
const switchWithText = mount(<Switch innerLabelChecked="Checked text" innerLabel="Unchecked text" />);
it("renders internal text", () => {
giladgray marked this conversation as resolved.
Show resolved Hide resolved
assert.equal(switchWithText.find(`.${Classes.SWITCH}-active-text`).text(), "Active Text");
const innerTextNodes = switchWithText.find(`.${Classes.SWITCH}-inner-text`);
assert.lengthOf(innerTextNodes, 2);
const uncheckedText = innerTextNodes.last().text();
assert.equal(uncheckedText.trim(), "Unchecked text");
});
});
});
Expand All @@ -60,7 +61,7 @@ describe("Controls:", () => {
it("supports JSX children", () => {
const control = mountControl({}, <span className="jsx-child" key="jsx" />, "Label Text");
assert.lengthOf(control.find(".jsx-child"), 1);
assert.equal(control.text(), "Label Text");
assert.equal(control.text().trim(), "Label Text");
});

it("supports JSX labelElement", () => {
Expand All @@ -69,7 +70,7 @@ describe("Controls:", () => {

const control = mountControl({ labelElement: <strong>boom</strong> });
assert.lengthOf(control.find("strong"), 1);
assert.equal(control.text(), "boom");
assert.equal(control.text().trim(), "boom");
});

if (moreTests != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export class SwitchExample extends CheckboxExample {
<Switch
{...this.state}
labelElement={"Containing Text"}
internalTextActive="activeText"
internalTextInactive="inactiveText"
innerLabelChecked="checked"
innerLabel="unchecked"
giladgray marked this conversation as resolved.
Show resolved Hide resolved
/>
</div>
<small style={{ width: "100%", textAlign: "center" }}>
Expand Down