migrate boolean widget

This commit is contained in:
Shawn Erquhart 2018-07-24 09:18:36 -04:00
parent 0e25b76bb7
commit 6be3c7a839
9 changed files with 91 additions and 63 deletions

View File

@ -24,7 +24,7 @@ export default class Preview extends React.Component {
return ( return (
<PreviewContainer> <PreviewContainer>
{fields.filter(isVisible).map(field => ( {fields.filter(isVisible).map(field => (
<div>{widgetFor(field.get('name'))}</div> <div key={field.get('name')}>{widgetFor(field.get('name'))}</div>
))} ))}
</PreviewContainer> </PreviewContainer>
); );

View File

@ -1,9 +0,0 @@
.nc-booleanControl-switch {
& .nc-toggle-background {
background-color: var(--textFieldBorderColor);
}
& .nc-toggle-active .nc-toggle-background {
background-color: var(--colorActive);
}
}

View File

@ -23,7 +23,6 @@ import ObjectControl from './Object/ObjectControl';
import ObjectPreview from './Object/ObjectPreview'; import ObjectPreview from './Object/ObjectPreview';
import RelationControl from './Relation/RelationControl'; import RelationControl from './Relation/RelationControl';
import RelationPreview from './Relation/RelationPreview'; import RelationPreview from './Relation/RelationPreview';
import BooleanControl from './Boolean/BooleanControl';
registerWidget('text', TextControl, TextPreview); registerWidget('text', TextControl, TextPreview);
registerWidget('number', NumberControl, NumberPreview); registerWidget('number', NumberControl, NumberPreview);
@ -36,5 +35,4 @@ registerWidget('datetime', DateTimeControl, DateTimePreview);
registerWidget('select', SelectControl, SelectPreview); registerWidget('select', SelectControl, SelectPreview);
registerWidget('object', ObjectControl, ObjectPreview); registerWidget('object', ObjectControl, ObjectPreview);
registerWidget('relation', RelationControl, RelationPreview); registerWidget('relation', RelationControl, RelationPreview);
registerWidget('boolean', BooleanControl);
registerWidget('unknown', UnknownControl, UnknownPreview); registerWidget('unknown', UnknownControl, UnknownPreview);

View File

@ -1,67 +1,67 @@
import React from 'react' import React from 'react'
import styled, { css, cx } from 'react-emotion'; import styled, { css } from 'react-emotion';
import ReactToggled from 'react-toggled'; import ReactToggled from 'react-toggled';
import { colors, colorsRaw, shadows, transitions } from './styles'; import { colors, colorsRaw, shadows, transitions } from './styles';
const styles = { const ToggleContainer = styled.span`
switch: css` display: inline-flex;
display: inline-flex; align-items: center;
align-items: center; justify-content: center;
justify-content: center; position: relative;
position: relative; width: 40px;
width: 40px; height: 20px;
height: 20px; cursor: pointer;
cursor: pointer; `
`,
switchHandle: css` const ToggleHandle = styled.span`
${shadows.dropDeep}; ${shadows.dropDeep};
position: absolute; position: absolute;
left: 0; left: 0;
top: 0; top: 0;
width: 20px; width: 20px;
height: 20px; height: 20px;
border-radius: 50%; border-radius: 50%;
background-color: ${colorsRaw.white}; background-color: ${colorsRaw.white};
transition: transform ${transitions.main}; transition: transform ${transitions.main};
`,
switchHandleActive: css` ${props => props.isActive && css`
transform: translateX(20px); transform: translateX(20px);
`, `}
switchBackground: css` `
width: 34px;
height: 14px; const ToggleBackground = styled.span`
border-radius: 10px; width: 34px;
background-color: ${colors.active}; height: 14px;
`, border-radius: 10px;
}; background-color: ${colors.active};
`
const Toggle = ({ const Toggle = ({
active, active,
onChange, onChange,
className, renderBackground,
classNameBackground,
classNameSwitch,
onFocus, onFocus,
onBlur onBlur,
}) => Container = ToggleContainer,
Background = ToggleBackground,
Handle = ToggleHandle,
}) => (
<ReactToggled on={active} onToggle={onChange}> <ReactToggled on={active} onToggle={onChange}>
{({on, getElementTogglerProps}) => ( {({on, getElementTogglerProps}) => (
<span <Container
className={cx(styles.switch, className)}
role="switch" role="switch"
aria-checked={on.toString()} aria-checked={on.toString()}
onFocus={onFocus} onFocus={onFocus}
onBlur={onBlur} onBlur={onBlur}
{...getElementTogglerProps()} {...getElementTogglerProps()}
> >
<span className={cx(styles.switchBackground, classNameBackground)}/> <Background isActive={on}/>
<span className={cx( <Handle isActive={on}/>
styles.switchHandle, </Container>
classNameSwitch,
{ [styles.switchHandleActive]: on },
)}/>
</span>
)} )}
</ReactToggled>; </ReactToggled>
);
export default styled(Toggle)`` const StyledToggle = styled(Toggle)``;
export { StyledToggle as default, ToggleContainer, ToggleBackground, ToggleHandle };

View File

@ -2,7 +2,7 @@ export Dropdown, { DropdownItem, DropdownButton, StyledDropdownButton } from './
export Icon from './Icon'; export Icon from './Icon';
export ListItemTopBar from './ListItemTopBar'; export ListItemTopBar from './ListItemTopBar';
export Loader from './Loader'; export Loader from './Loader';
export Toggle from './Toggle'; export Toggle, { ToggleContainer, ToggleBackground, ToggleHandle } from './Toggle';
export AuthenticationPage from './AuthenticationPage'; export AuthenticationPage from './AuthenticationPage';
export WidgetPreviewContainer from './WidgetPreviewContainer'; export WidgetPreviewContainer from './WidgetPreviewContainer';
export { export {

View File

@ -0,0 +1,29 @@
{
"name": "netlify-cms-widget-boolean",
"description": "Widget for editing boolean values in Netlify CMS.",
"version": "2.0.0-alpha.0",
"main": "dist/netlify-cms-widget-boolean.js",
"license": "MIT",
"keywords": [
"netlify",
"netlify-cms",
"widget",
"boolean"
],
"sideEffects": false,
"scripts": {
"watch": "webpack -w",
"build": "webpack"
},
"devDependencies": {
"webpack": "^4.16.1",
"webpack-cli": "^3.1.0"
},
"peerDependencies": {
"lodash": "^4.17.10",
"netlify-cms-ui-default": "^2.0.0-alpha.0",
"prop-types": "^15.5.10",
"react": "^16.4.1",
"react-immutable-proptypes": "^2.1.0"
}
}

View File

@ -1,8 +1,13 @@
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import React from 'react'; import React from 'react';
import ImmutablePropTypes from "react-immutable-proptypes"; import ImmutablePropTypes from 'react-immutable-proptypes';
import styled from 'react-emotion';
import { isBoolean } from 'lodash'; import { isBoolean } from 'lodash';
import { Toggle } from 'netlify-cms-ui-default'; import { Toggle, ToggleBackground, colors } from 'netlify-cms-ui-default';
const BooleanBackground = styled(ToggleBackground)`
background-color: ${props => props.isActive ? colors.active : colors.textFieldBorder};
`
export default class BooleanControl extends React.Component { export default class BooleanControl extends React.Component {
render() { render() {
@ -16,13 +21,14 @@ export default class BooleanControl extends React.Component {
setInactiveStyle setInactiveStyle
} = this.props; } = this.props;
return ( return (
<div className={`${classNameWrapper} nc-booleanControl-switch`}> <div>
<Toggle <Toggle
id={forID} id={forID}
active={isBoolean(value) ? value : field.get('defaultValue', false)} active={isBoolean(value) ? value : field.get('defaultValue', false)}
onChange={onChange} onChange={onChange}
onFocus={setActiveStyle} onFocus={setActiveStyle}
onBlur={setInactiveStyle} onBlur={setInactiveStyle}
Background={BooleanBackground}
/> />
</div> </div>
); );

View File

@ -0,0 +1 @@
export BooleanControl from './BooleanControl';

View File

@ -0,0 +1,3 @@
const { getConfig } = require('../../scripts/webpack.js');
module.exports = getConfig();