Skip to content

Commit

Permalink
Add toggle element to @meetfranz/forms
Browse files Browse the repository at this point in the history
  • Loading branch information
adlk committed Jan 11, 2019
1 parent a4d5cf0 commit 0ac2806
Show file tree
Hide file tree
Showing 21 changed files with 281 additions and 38 deletions.
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
"@types/react": "^16.7.18",
"@types/react-dom": "16.0.11",
"@types/react-jss": "^8.6.0",
"@types/uuid": "3.4.4",
"babel-eslint": "10.0.1",
"babel-loader": "^8.0.4",
"cross-env": "^5.0.5",
Expand Down Expand Up @@ -132,6 +133,7 @@
"install": "0.12.2",
"kebab-case": "1.0.0",
"lerna": "^3.8.0",
"mobx-react-devtools": "6.0.3",
"mocha": "5.2.0",
"node-sass": "4.11.0",
"npm": "6.5.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/forms/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as Input } from './input';
export { Input } from './input';
export { Toggle } from './toggle';
10 changes: 4 additions & 6 deletions packages/forms/src/input/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ import scorePasswordFunc from './scorePassword';

import styles from './styles';

interface IProps extends IFormField, React.InputHTMLAttributes<HTMLInputElement>, IWithStyle {
label: string;
interface IProps extends React.InputHTMLAttributes<HTMLInputElement>, IFormField, IWithStyle {
focus?: boolean;
prefix?: string;
suffix?: string;
scorePassword?: boolean;
showPasswordToggle?: boolean;
error?: string;
}

interface IState {
Expand All @@ -32,15 +30,15 @@ interface IState {
}

@observer
class Input extends Component<IProps, IState> {
class InputComponent extends Component<IProps, IState> {
public static defaultProps = {
classes: {},
focus: false,
onChange: () => {},
scorePassword: false,
showLabel: true,
showPasswordToggle: false,
type: 'text',
disabled: false,
};

state = {
Expand Down Expand Up @@ -172,4 +170,4 @@ class Input extends Component<IProps, IState> {
}
}

export default injectSheet(styles)(Input);
export const Input = injectSheet(styles)(InputComponent);
2 changes: 1 addition & 1 deletion packages/forms/src/input/styles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Theme } from '@meetfranz/theme';
import CSS from 'csstype';
import { Theme } from '../../../theme/lib';

const prefixStyles = (theme: Theme) => ({
background: theme.inputPrefixBackground,
Expand Down
117 changes: 117 additions & 0 deletions packages/forms/src/toggle/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { Theme } from '@meetfranz/theme';
import classnames from 'classnames';
import CSS from 'csstype';
import { observer } from 'mobx-react';
import React, { Component, createRef } from 'react';
import injectStyle from 'react-jss';

import { IFormField, IWithStyle, Omit } from '../typings/generic';

import Error from '../error';
import Label from '../label';
import Wrapper from '../wrapper';

interface IProps extends React.InputHTMLAttributes<HTMLInputElement>, IFormField, IWithStyle {
// field: any;
}

const styles = (theme: Theme) => ({
toggle: {
background: theme.toggleBackground,
borderRadius: theme.borderRadius,
height: theme.toggleHeight,
position: 'relative' as CSS.PositionProperty,
width: theme.toggleWidth,
},
button: {
background: theme.toggleButton,
borderRadius: '100%',
boxShadow: '0 1px 4px rgba(0, 0, 0, .3)',
width: theme.toggleHeight - 2,
height: theme.toggleHeight - 2,
left: 1,
top: 1,
position: 'absolute' as CSS.PositionProperty,
transition: 'all .5s',
},
buttonActive: {
background: theme.toggleButtonActive,
left: (theme.toggleWidth - theme.toggleHeight) + 1,
},
input: {
visibility: 'hidden' as any,
},
disabled: {
opacity: 0.5,
},
toggleLabel: {
display: 'flex',

'& > span': {
order: 1,
marginLeft: 15,
marginTop: 2,
},
},
});

@observer
class ToggleComponent extends Component<IProps> {
public static defaultProps = {
onChange: () => {},
showLabel: true,
disabled: false,
error: '',
};

render() {
const {
classes,
disabled,
error,
id,
label,
showLabel,
checked,
value,
onChange,
} = this.props;

console.log('props', this.props);

return (
<Wrapper>
<Label
title={label}
showLabel={showLabel}
htmlFor={id}
className={classes.toggleLabel}
>
<div className={classnames({
[`${classes.toggle}`]: true,
[`${classes.disabled}`]: disabled,
})}>
<div className={classnames({
[`${classes.button}`]: true,
[`${classes.buttonActive}`]: checked,
})} />
<input
className={classes.input}
id={id || name}
type="checkbox"
checked={checked}
value={value}
onChange={onChange}
disabled={disabled}
/>
</div>
</Label>
{error && (
<Error message={error} />
)}
</Wrapper>
);
}
}

export const Toggle = injectStyle(styles)(ToggleComponent);
7 changes: 5 additions & 2 deletions packages/forms/src/typings/generic.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Classes } from 'jss';

export interface IFormField {
title?: string;
showLabel?: boolean;
isError?: boolean;
label?: string;
error?: string;
}

export interface IWithStyle {
classes: Classes;
}

export type Merge<M, N> = Omit<M, Extract<keyof M, keyof N>> & N;
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
2 changes: 2 additions & 0 deletions packages/forms/src/wrapper/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { observer } from 'mobx-react';
import React, { Component } from 'react';
import injectStyle from 'react-jss';
import { IWithStyle } from '../typings/generic';
Expand All @@ -8,6 +9,7 @@ interface IProps extends IWithStyle {
children: React.ReactNode;
}

@observer
class Wrapper extends Component<IProps> {
render() {
const {
Expand Down
4 changes: 4 additions & 0 deletions packages/theme/src/themes/dark/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ export const inputDisabledOpacity = 0.5;
export const inputScorePasswordBackground = legacyStyles.darkThemeGrayDark;
export const inputModifierColor = color(legacyStyles.darkThemeGrayLighter).lighten(0.3).hex();
export const inputPlaceholderColor = color(legacyStyles.darkThemeGrayLighter).darken(0.1).hex();

// Toggle
export const toggleBackground = legacyStyles.darkThemeGray;
export const toggleButton = legacyStyles.darkThemeGrayLighter;
7 changes: 7 additions & 0 deletions packages/theme/src/themes/default/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ export const inputPrefixColor = legacyStyles.themeGrayLight;
export const inputPrefixBackground = legacyStyles.themeGrayLighter;
export const inputDisabledOpacity = 0.5;
export const inputScorePasswordBackground = legacyStyles.themeGrayLighter;

// Toggle
export const toggleBackground = legacyStyles.themeGrayLighter;
export const toggleButton = legacyStyles.themeGrayLight;
export const toggleButtonActive = brandPrimary;
export const toggleWidth = 40;
export const toggleHeight = 14;
2 changes: 1 addition & 1 deletion src/styles/toggle.scss
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ $toggle-button-size: 22px;

&.is-active .franz-form__toggle-button {
background: $theme-brand-primary;
left: $toggle-width - $toggle-size - 3;;
left: $toggle-width - $toggle-size - 3;
}

input { display: none; }
Expand Down
8 changes: 7 additions & 1 deletion tsconfig.settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"compilerOptions": {
"baseUrl": ".",
"target": "esnext",
"module": "commonjs",
"lib": [
Expand All @@ -13,6 +14,11 @@
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"composite": true,
"esModuleInterop": true
"esModuleInterop": true,
"typeRoots": ["packages/typings/types", "node_modules/@types"],
"paths": {
"@types/*": ["packages/typings/types/*.d.ts"],
"*": ["packages/typings/types/*.d.ts"]
}
}
}
7 changes: 0 additions & 7 deletions tsconfig.storybook.json

This file was deleted.

1 change: 0 additions & 1 deletion typings/react-html-attributes.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion typings/storybook__addons.d.ts

This file was deleted.

Loading

0 comments on commit 0ac2806

Please sign in to comment.