Skip to content

Commit

Permalink
Switch component and fix i18n in client (#1108)
Browse files Browse the repository at this point in the history
* [#780] Switch Component

* Update Switch in Moments/Strategies form

* Remove react-intl and use i18n-js

* Add i18n to TODOs in client

* Make Codeclimate happy

* Remove build-i18n from test build

* Quick fixes

* CSS updates

* Letter spacing
  • Loading branch information
julianguyen authored Oct 7, 2018
1 parent 01ec097 commit ed1a17d
Show file tree
Hide file tree
Showing 41 changed files with 370 additions and 269 deletions.
95 changes: 0 additions & 95 deletions app/assets/stylesheets/application/drafts.scss

This file was deleted.

4 changes: 2 additions & 2 deletions app/helpers/moments_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def moment_form_inputs
moments_viewers_input,
{
id: 'moment_comment',
type: 'checkbox',
type: 'switch',
name: 'moment[comment]',
label: t('comment.allow_comments'),
value: true,
Expand All @@ -104,7 +104,7 @@ def moment_form_inputs
},
{
id: 'moment_publishing',
type: 'checkbox',
type: 'switch',
label: t('moments.form.draft_question'),
dark: true,
name: 'publishing',
Expand Down
22 changes: 11 additions & 11 deletions app/helpers/strategies_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def strategy_form_inputs
strategy_viewers_input,
{
id: 'strategy_comment',
type: 'checkbox',
type: 'switch',
name: 'strategy[comment]',
label: t('comment.allow_comments'),
value: true,
Expand All @@ -65,6 +65,16 @@ def strategy_form_inputs
info: t('comment.hint'),
dark: true
},
{
id: 'strategy_publishing',
type: 'switch',
label: t('strategies.form.draft_question'),
dark: true,
name: 'publishing',
value: '0',
uncheckedValue: '1',
checked: !@strategy.published?
},
{
id: 'strategy_perform_strategy_reminder',
type: 'checkbox',
Expand All @@ -76,16 +86,6 @@ def strategy_form_inputs
checked: @strategy&.perform_strategy_reminder&.active,
dark: true
},
{
id: 'strategy_publishing',
type: 'checkbox',
label: t('strategies.form.draft_question'),
dark: true,
name: 'publishing',
value: '0',
uncheckedValue: '1',
checked: !@strategy.published?
},
{
id: 'strategy_perform_strategy_reminder_attributes_id',
name: 'strategy[perform_strategy_reminder_attributes][id]',
Expand Down
11 changes: 0 additions & 11 deletions app/views/shared/_draft_slider.html.erb

This file was deleted.

1 change: 0 additions & 1 deletion client/.storybook/addons.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@

import '@storybook/addon-actions/register';
import '@storybook/addon-backgrounds/register';
import 'storybook-addon-intl/register';
import '@storybook/addon-links/register';
12 changes: 2 additions & 10 deletions client/.storybook/config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/* eslint-disable import/no-extraneous-dependencies, import/no-unresolved, import/extensions */
import backgrounds from '@storybook/addon-backgrounds';
import { setDefaults, withInfo } from '@storybook/addon-info';
import { setIntlConfig, withIntl } from 'storybook-addon-intl';
import { addDecorator, configure } from '@storybook/react';

import { loadLocales } from 'libs/i18n/I18nSetup';
import { availableLocalesAsCodeArray, defaultLocale, getMessages } from 'libs/i18n/I18nUtils';
import { setup } from 'libs/i18n/setup';

import './stories.scss';

Expand All @@ -28,13 +26,7 @@ const withInfoConfig = {

const globalDecorator = (storyFn, context) => withInfo(withInfoConfig)(storyFn)(context);
addDecorator(globalDecorator);
loadLocales();
setIntlConfig({
locales: availableLocalesAsCodeArray,
defaultLocale,
getMessages,
});
addDecorator(withIntl);
setup();
addDecorator(backgrounds([
{ name: 'mulberry-wood', value: '#6D0839' },
{ name: 'dark-gray', value: '#3F3F3F' }, // 25% gray
Expand Down
1 change: 1 addition & 0 deletions client/app/components/Form/__tests__/Form.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const component = (noFormTag: boolean) => (
InputMocks.inputSelectProps,
Object.assign({}, InputMocks.inputCheckboxGroupProps, { required: true }),
InputMocks.inputTagProps,
InputMocks.inputSwitchProps,
InputMocks.inputSubmitProps,
]}
/>
Expand Down
4 changes: 3 additions & 1 deletion client/app/components/Header/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ReactHtmlParser from 'react-html-parser';
import { Logo } from '../Logo';
import { HeaderProfile } from './HeaderProfile';
import css from './Header.scss';
import { I18n } from '../../libs/i18n';

export type Link = {
name: string,
Expand Down Expand Up @@ -81,6 +82,7 @@ export class Header extends React.Component<Props, State> {

displayDesktop = () => {
const { home } = this.props;
const { mobileNavOpen } = this.state;
return (
<div className={css.headerDesktop}>
<div className={css.headerDesktopHome}>
Expand All @@ -94,7 +96,7 @@ export class Header extends React.Component<Props, State> {
onKeyDown={this.toggle}
role="button"
tabIndex="0"
aria-label="Expand menu" // TODO: intl in React not working in Rails
aria-label={mobileNavOpen ? I18n.t('close') : I18n.t('expand_menu')}
>
{this.displayToggle()}
</div>
Expand Down
88 changes: 88 additions & 0 deletions client/app/components/Input/InputSwitch.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// @flow
import React from 'react';
import { Utils } from '../../utils';
import { Input } from './index';
import css from './InputSwitch.scss';
import { I18n } from '../../libs/i18n';

export type Props = {
id: string,
name: string,
label: string,
value?: any,
checked?: boolean,
uncheckedValue?: any,
};

export type State = {
checked: boolean,
key?: string,
};

export class InputSwitch extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = { checked: !!props.checked };
}

toggleChecked = () => {
this.setState((prevState: State) => ({
checked: !prevState.checked,
key: Utils.randomString(),
}));
};

onKeyPress = (e: SyntheticKeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Enter') {
this.toggleChecked();
}
};

displaySwitchHidden = () => {
const {
id, name, label, value, uncheckedValue,
} = this.props;
const { checked, key } = this.state;
return (
<div className={css.switchHidden}>
<Input
id={id}
key={key}
type="checkbox"
name={name}
label={label}
value={value}
uncheckedValue={uncheckedValue}
checked={checked}
/>
</div>
);
};

render() {
const { id } = this.props;
const { checked } = this.state;
return (
<div className={css.switch}>
<div
className={`${css.switchWrapper} ${
checked ? css.switchOn : css.switchOff
}`}
>
<div
id={`${id}_switch`}
className={`switchToggle ${css.switchToggle}`}
onClick={this.toggleChecked}
onKeyPress={this.onKeyPress}
role="switch"
aria-checked={checked}
tabIndex={0}
>
{checked ? I18n.t('true') : I18n.t('false')}
</div>
</div>
{this.displaySwitchHidden()}
</div>
);
}
}
39 changes: 39 additions & 0 deletions client/app/components/Input/InputSwitch.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@import "../../styles/_global.scss";

.switch {
@include setFontSize($size-16);
display: inline-block;
min-width: 120px;
letter-spacing: 0.095em;

&Wrapper {
display: flex;
flex-direction: row;
border-radius: $size-4;
padding: $size-4;
}

&On {
@include linearTransition(0.5s);
background: $key-lime;
justify-content: flex-end;
}

&Off {
@include linearTransition(0.5s);
background: $mulberry-70;
}

&Toggle {
background: $white-70;
padding: $size-8 $size-16;
border-radius: $size-4;
color: $mulberry-80;
cursor: pointer;
text-transform: uppercase;
}

&Hidden {
display: none;
}
}
Loading

0 comments on commit ed1a17d

Please sign in to comment.