Skip to content

Commit

Permalink
Merge pull request #6 from weni-ai/feature/unnnic-switch-migration
Browse files Browse the repository at this point in the history
Migrating unnnic-switch component
  • Loading branch information
cristiantela authored May 12, 2023
2 parents bc80751 + fbe7e5d commit f9ba6b9
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 28 deletions.
5 changes: 3 additions & 2 deletions src/components/flow/actions/sendmsg/SendMsgForm.module.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
@import 'variables.module.scss';
@import '@weni/unnnic-system/dist/scss/unnnic.scss';

.checkbox {
margin-top: 20px;
.title {
margin-bottom: $unnnic-spacing-stack-sm;
}

.quick_reply_summary {
Expand Down
33 changes: 21 additions & 12 deletions src/components/flow/actions/sendmsg/SendMsgForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { ActionFormProps } from 'components/flow/props';
import AssetSelector from 'components/form/assetselector/AssetSelector';
import { hasUseableTranslation } from 'components/form/assetselector/helpers';
import CheckboxElement from 'components/form/checkbox/CheckboxElement';
import SwitchElement, { SwitchSizes } from 'components/form/switch/SwitchElement';
import MultiChoiceInput from 'components/form/multichoice/MultiChoice';
import SelectElement, { SelectOption } from 'components/form/select/SelectElement';
import TextInputElement from 'components/form/textinput/TextInputElement';
Expand Down Expand Up @@ -359,17 +359,26 @@ export default class SendMsgForm extends React.Component<ActionFormProps, SendMs
const advanced: Tab = {
name: i18n.t('forms.advanced', 'Advanced'),
body: (
<CheckboxElement
name={i18n.t('forms.all_destinations', 'All Destinations')}
title={i18n.t('forms.all_destinations', 'All Destinations')}
labelClassName={styles.checkbox}
checked={this.state.sendAll}
description={i18n.t(
'forms.all_destinations_description',
"Send a message to all destinations known for this contact. If you aren't sure what this means, leave it unchecked."
)}
onChange={this.handleSendAllUpdate}
/>
<>
<div className={`u font secondary body-md color-neutral-cloudy ${styles.title}`}>
{i18n.t(
'forms.all_destinations_title',
'Send a message to all destinations known for this contact.'
)}
</div>

<SwitchElement
name={i18n.t('forms.all_destinations', 'Send a message to all destinations')}
title={i18n.t('forms.all_destinations', 'Send a message to all destinations')}
checked={this.state.sendAll}
description={i18n.t(
'forms.all_destinations_description',
"If you aren't sure what this means, leave it unchecked."
)}
onChange={this.handleSendAllUpdate}
size={SwitchSizes.small}
/>
</>
),
checked: this.state.sendAll
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,21 @@ exports[`SendMsgForm render should render 1`] = `
"name": "Attachments",
},
Object {
"body": <CheckboxElement
checked={false}
description="Send a message to all destinations known for this contact. If you aren't sure what this means, leave it unchecked."
labelClassName="checkbox"
name="All Destinations"
onChange={[Function]}
title="All Destinations"
/>,
"body": <React.Fragment>
<div
className="u font secondary body-md color-neutral-cloudy title"
>
Send a message to all destinations known for this contact.
</div>
<SwitchElement
checked={false}
description="If you aren't sure what this means, leave it unchecked."
name="Send a message to all destinations"
onChange={[Function]}
size="small"
title="Send a message to all destinations"
/>
</React.Fragment>,
"checked": false,
"name": "Advanced",
},
Expand Down
6 changes: 6 additions & 0 deletions src/components/form/switch/SwitchElement.module.scss
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;
}
27 changes: 27 additions & 0 deletions src/components/form/switch/SwitchElement.test.ts
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');
});
});
88 changes: 88 additions & 0 deletions src/components/form/switch/SwitchElement.tsx
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>
)}
</>
);
}
}
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>
`;
5 changes: 3 additions & 2 deletions src/config/i18n/defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,9 @@
"add_quick_reply": "Add a new Quick Reply and press enter.",
"add_urn_summary": "Add a new URN to reach the contact such as a phone number.",
"advanced": "Advanced",
"all_destinations": "All Destinations",
"all_destinations_description": "Send a message to all destinations known for this contact. If you aren't sure what this means, leave it unchecked.",
"all_destinations": "Send a message to all destinations",
"all_destinations_title": "Send a message to all destinations known for this contact.",
"all_destinations_description": "If you aren't sure what this means, leave it unchecked.",
"and": "and",
"are_required": "are required",
"arguments": "arguments",
Expand Down
5 changes: 3 additions & 2 deletions src/config/i18n/es/resource.json
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,9 @@
"add_quick_reply": "Agregue una nueva respuesta rápida y presione enter.",
"add_urn_summary": "Agregue un nuevo URN para comunicarse con el contacto, como un número de teléfono.",
"advanced": "Avanzado",
"all_destinations": "Todos los Destinos",
"all_destinations_description": "Envíe un mensaje a todos los destinos conocidos para este contacto. Si no está seguro de lo que esto significa, déjelo sin marcar.",
"all_destinations": "Envíe un mensaje a todos los destinos",
"all_destinations_title": "Envíe un mensaje a todos los destinos conocidos para este contacto.",
"all_destinations_description": "Si no está seguro de lo que esto significa, déjelo sin marcar.",
"and": "y",
"are_required": "son requeridos",
"arguments": "argumentos",
Expand Down
5 changes: 3 additions & 2 deletions src/config/i18n/pt-br/resource.json
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,9 @@
"add_quick_reply": "Adicione uma nova Resposta Rápida e pressione enter.",
"add_urn_summary": "Adicione uma nova URN para encontrar o contato, como um número de telefone.",
"advanced": "Avançado",
"all_destinations": "Todos os Destinos",
"all_destinations_description": "Envie uma mensagem para todos os destinos conhecidos deste contato. Se você não tiver certeza do que isso significa, deixe-o desmarcado.",
"all_destinations": "Envie uma mensagem para todos os destinos",
"all_destinations_title": "Envie uma mensagem para todos os destinos conhecidos deste contato",
"all_destinations_description": "Se você não tiver certeza do que isso significa, deixe-o desmarcado.",
"and": "e",
"are_required": "são requeridos",
"arguments": "argumentos",
Expand Down

0 comments on commit f9ba6b9

Please sign in to comment.