forked from nyaruka/floweditor
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from weni-ai/feature/unnnic-switch-migration
Migrating unnnic-switch component
- Loading branch information
Showing
10 changed files
with
191 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@import '@weni/unnnic-system/dist/scss/unnnic.scss'; | ||
|
||
.description { | ||
margin-top: $unnnic-spacing-stack-nano; | ||
margin-left: 2.5rem; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { composeComponentTestUtils, getSpecWrapper } from 'testUtils'; | ||
|
||
import SwitchElement, { SwitchElementProps, descSpecId } from './SwitchElement'; | ||
|
||
const baseProps: SwitchElementProps = { | ||
name: 'Switch', | ||
checked: false | ||
}; | ||
|
||
const { setup } = composeComponentTestUtils(SwitchElement, baseProps); | ||
|
||
describe(SwitchElement.name, () => { | ||
it('should render a switch element with description', () => { | ||
const { wrapper } = setup(true, { | ||
$merge: { | ||
title: 'Switch', | ||
description: 'All Destinations', | ||
labelClassName: 'label', | ||
switchClassName: 'switch', | ||
onChange: jest.fn() | ||
} | ||
}); | ||
|
||
expect(getSpecWrapper(wrapper, descSpecId).hasClass('description')).toBeTruthy(); | ||
expect(wrapper).toMatchSnapshot('unchecked'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { FormElementProps } from 'components/form/FormElement'; | ||
import * as React from 'react'; | ||
import { isRealValue, renderIf } from 'utils'; | ||
import { applyVueInReact } from 'vuereact-combined'; | ||
|
||
// @ts-ignore | ||
import { unnnicSwitch } from '@weni/unnnic-system'; | ||
|
||
import styles from './SwitchElement.module.scss'; | ||
|
||
export enum SwitchSizes { | ||
medium = 'medium', | ||
small = 'small' | ||
} | ||
|
||
export interface SwitchElementProps extends FormElementProps { | ||
checked: boolean; | ||
title?: string; | ||
description?: string; | ||
labelClassName?: string; | ||
switchClassName?: string; | ||
size?: SwitchSizes; | ||
onChange?(checked: boolean): void; | ||
} | ||
|
||
interface SwitchState { | ||
checked: boolean; | ||
} | ||
|
||
export const boxIco = 'fe-square'; | ||
export const checkedBoxIco = 'fe-check-square'; | ||
|
||
export const switchSpecId = 'switch'; | ||
export const titleSpecId = 'title'; | ||
export const descSpecId = 'description'; | ||
|
||
const UnnnicSwitch = applyVueInReact(unnnicSwitch); | ||
|
||
export default class SwitchElement extends React.Component<SwitchElementProps, SwitchState> { | ||
constructor(props: any) { | ||
super(props); | ||
|
||
this.state = { | ||
checked: this.props.checked | ||
}; | ||
|
||
this.handleChange = this.handleChange.bind(this); | ||
} | ||
|
||
private handleChange(checked: boolean): void { | ||
console.log('checked', checked); | ||
|
||
this.setState({ checked }, () => { | ||
if (this.props.onChange) { | ||
this.props.onChange(this.state.checked); | ||
} | ||
}); | ||
} | ||
|
||
/* istanbul ignore next */ | ||
public validate(): boolean { | ||
return true; | ||
} | ||
|
||
public render(): JSX.Element { | ||
// const switchIcon = this.state.checked ? checkedBoxIco : boxIco; | ||
return ( | ||
<> | ||
<UnnnicSwitch | ||
data-spec={switchSpecId} | ||
value={this.state.checked} | ||
textRight={this.props.title} | ||
on={{ input: this.handleChange }} | ||
size={this.props.size} | ||
/> | ||
|
||
{renderIf(isRealValue(this.props.description))( | ||
<div | ||
data-spec={descSpecId} | ||
className={`u font secondary body-sm color-neutral-cloudy ${styles.description}`} | ||
> | ||
{this.props.description} | ||
</div> | ||
)} | ||
</> | ||
); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/components/form/switch/__snapshots__/SwitchElement.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`SwitchElement should render a switch element with description: unchecked 1`] = ` | ||
<Fragment> | ||
<ForwardRef | ||
data-spec="switch" | ||
on={ | ||
Object { | ||
"input": [Function], | ||
} | ||
} | ||
textRight="Switch" | ||
value={false} | ||
/> | ||
<div | ||
className="u font secondary body-sm color-neutral-cloudy description" | ||
data-spec="description" | ||
> | ||
All Destinations | ||
</div> | ||
</Fragment> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters