From 92697f7990b341aa94c57e73f10df81f43011e2a Mon Sep 17 00:00:00 2001
From: Chao Liang
Date: Fri, 25 Jan 2019 15:29:03 +1100
Subject: [PATCH] feat: rewrite checkbox component add partial select state
---
.eslintrc | 3 +-
.sass-lint.yml | 4 +-
config/common.js | 2 +-
config/dev-cold.js | 11 --
config/dev.js | 11 --
docs/examples/CheckboxExample.jsx | 135 +++++++++++++-----
docs/examples/CheckboxGroupExample.jsx | 47 +++---
docs/examples/styles.scss | 11 ++
src/components/adslot-ui/Checkbox/index.jsx | 86 +++++------
.../adslot-ui/Checkbox/index.spec.jsx | 32 +++--
src/components/adslot-ui/Checkbox/styles.scss | 85 ++++++++++-
.../adslot-ui/CheckboxGroup/index.jsx | 71 ++++-----
.../adslot-ui/CheckboxGroup/index.spec.jsx | 31 ++--
src/components/prop-types/inputPropTypes.js | 19 ++-
.../icons/checkbox/checked-disabled.svg | 1 +
src/styles/icons/checkbox/checked.svg | 1 +
src/styles/icons/checkbox/empty-disabled.svg | 1 +
src/styles/icons/checkbox/empty.svg | 1 +
src/styles/icons/checkbox/hover.svg | 1 +
.../icons/checkbox/partial-disabled.svg | 1 +
src/styles/icons/checkbox/partial-hover.svg | 1 +
src/styles/icons/checkbox/partial.svg | 1 +
22 files changed, 365 insertions(+), 191 deletions(-)
create mode 100644 src/styles/icons/checkbox/checked-disabled.svg
create mode 100644 src/styles/icons/checkbox/checked.svg
create mode 100644 src/styles/icons/checkbox/empty-disabled.svg
create mode 100644 src/styles/icons/checkbox/empty.svg
create mode 100644 src/styles/icons/checkbox/hover.svg
create mode 100644 src/styles/icons/checkbox/partial-disabled.svg
create mode 100644 src/styles/icons/checkbox/partial-hover.svg
create mode 100644 src/styles/icons/checkbox/partial.svg
diff --git a/.eslintrc b/.eslintrc
index 88f4c439b..11266cb3d 100644
--- a/.eslintrc
+++ b/.eslintrc
@@ -91,6 +91,7 @@
"functions": "never"
}
],
- "object-curly-newline": ["error", { "multiline": true, "consistent": true }]
+ "object-curly-newline": ["error", { "multiline": true, "consistent": true }],
+ "no-console": ["error", { "allow": ["warn", "error"] }]
}
}
diff --git a/.sass-lint.yml b/.sass-lint.yml
index bee25cdf2..40ba1bf83 100644
--- a/.sass-lint.yml
+++ b/.sass-lint.yml
@@ -40,8 +40,8 @@ rules:
# Nesting
force-attribute-nesting: 2
- force-element-nesting: 2
- force-pseudo-nesting: 2
+ force-element-nesting: 0
+ force-pseudo-nesting: 0
# Name Formats
# TODO: When inline linting is supported set as error (https://github.com/sasstools/sass-lint/pull/402) and ignore
diff --git a/config/common.js b/config/common.js
index 74ea8caab..a88973a86 100644
--- a/config/common.js
+++ b/config/common.js
@@ -30,7 +30,7 @@ module.exports = {
// "url" loader works like "file" loader except that it embeds assets smaller than specified limit in bytes
// as data URLs to avoid requests.
{
- test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
+ test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/,
loader: 'url-loader',
options: {
limit: 8192,
diff --git a/config/dev-cold.js b/config/dev-cold.js
index 903d51aad..8fdf59e67 100644
--- a/config/dev-cold.js
+++ b/config/dev-cold.js
@@ -18,17 +18,6 @@ module.exports = merge(commonConfig, {
filename: 'app.js',
publicPath,
},
- module: {
- rules: [
- {
- test: /\.(jpe?g|png|gif|svg)$/i,
- loaders: [
- 'file-loader?hash=sha512&digest=hex&name=[hash].[ext]',
- 'image-webpack-loader?bypassOnDebug&optipng.optimizationLevel=7&gifsicle.interlaced=false',
- ],
- },
- ],
- },
devServer: {
hot: false, // disable HMR on the server
contentBase: resolve(__dirname, '../src'), // match the output path
diff --git a/config/dev.js b/config/dev.js
index d567db154..741b6ef69 100644
--- a/config/dev.js
+++ b/config/dev.js
@@ -26,17 +26,6 @@ module.exports = merge(commonConfig, {
path: resolve(__dirname, '../dist/assets'),
publicPath, // necessary for HMR to know where to load the hot update chunks
},
- module: {
- rules: [
- {
- test: /\.(jpe?g|png|gif|svg)$/i,
- loaders: [
- 'file-loader?hash=sha512&digest=hex&name=[hash].[ext]',
- 'image-webpack-loader?bypassOnDebug&optipng.optimizationLevel=7&gifsicle.interlaced=false',
- ],
- },
- ],
- },
devServer: {
hot: true, // enable HMR on the server
contentBase: resolve(__dirname, '../docs'),
diff --git a/docs/examples/CheckboxExample.jsx b/docs/examples/CheckboxExample.jsx
index 7a8bb4a95..e362a00d9 100644
--- a/docs/examples/CheckboxExample.jsx
+++ b/docs/examples/CheckboxExample.jsx
@@ -3,47 +3,110 @@ import React from 'react';
import Example from '../components/Example';
import { Checkbox } from '../../src';
-class CheckboxExample extends React.PureComponent {
- constructor(props) {
- super(props);
+const handleChange = (...arg) => console.log('Checkbox changed with arguments', ...arg);
- this.state = {
- isChecked: false,
- };
- this.handleChange = this.handleChange.bind(this);
- }
-
- handleChange() {
- this.setState(prevState => ({
- isChecked: !prevState.isChecked,
- }));
- }
-
- render() {
- return (
-
- );
- }
-}
+const CheckboxExample = () => (
+ <>
+ Not selected
+
+
+ Checked: false
Disabled
+
+ }
+ checked={false}
+ disabled
+ inline
+ className="fix-size"
+ onChange={handleChange}
+ />
+ Selected
+
+
+ Checked: true
Disabled
+
+ }
+ checked
+ disabled
+ inline
+ className="fix-size"
+ onChange={handleChange}
+ />
+ Partial Selected
+
+
+ Checked: 'partial'
Disabled
+
+ }
+ checked="partial"
+ disabled
+ inline
+ className="fix-size"
+ onChange={handleChange}
+ />
+ Checkboxes without labels
+
+
+
+
+
+
+ >
+);
const exampleProps = {
componentName: 'Checkbox',
notes: '',
- exampleCodeSnippet: ``,
+ exampleCodeSnippet: `
+ Not selected
+
+
+ Checked: false
Disabled
+
+ }
+ checked={false}
+ disabled
+ inline
+ />
+ Selected
+
+
+ Checked: true
Disabled
+
+ }
+ checked
+ disabled
+ inline
+ />
+ Partial Selected
+
+
+ Checked: 'partial'
Disabled
+
+ }
+ checked="partial"
+ disabled
+ inline
+ />
+ Checkboxes without labels
+
+
+
+
+
+
+ `,
propTypeSectionArray: [
{
label: '',
diff --git a/docs/examples/CheckboxGroupExample.jsx b/docs/examples/CheckboxGroupExample.jsx
index 6f37089ab..c9328cd49 100644
--- a/docs/examples/CheckboxGroupExample.jsx
+++ b/docs/examples/CheckboxGroupExample.jsx
@@ -4,22 +4,25 @@ import Example from '../components/Example';
import { CheckboxGroup, Checkbox } from '../../src';
class CheckboxGroupExample extends React.PureComponent {
- constructor(props) {
- super(props);
+ state = {
+ movies: ['terminator', 'predator'],
+ };
- this.state = {
- movies: ['terminator', 'predator'],
- };
- this.handleGroupChange = this.handleGroupChange.bind(this);
- }
-
- handleGroupChange(movies) {
+ handleGroupChange = movies => {
this.setState({ movies });
- }
+ };
+
+ displayValue = () => {
+ return _.isEmpty(this.state.movies) ? Empty : [{this.state.movies.join(' ')}];
+ };
render() {
return (
-
+ <>
+
+ Selected Values: {this.displayValue()}
+
+
@@ -27,12 +30,12 @@ class CheckboxGroupExample extends React.PureComponent {
Inline CheckboxGroup
-
+
-
+ >
);
}
}
@@ -40,11 +43,19 @@ class CheckboxGroupExample extends React.PureComponent {
const exampleProps = {
componentName: 'CheckboxGroup',
notes: 'Contains individual checkboxes. The state of child checkboxes is held in an array.',
- exampleCodeSnippet: `
-
-
-
-`,
+ exampleCodeSnippet: `
+
+
+
+
+
+
+
+
+
+
+
+ `,
propTypeSectionArray: [
{
label: '',
diff --git a/docs/examples/styles.scss b/docs/examples/styles.scss
index a38f85d5a..72e0cf642 100644
--- a/docs/examples/styles.scss
+++ b/docs/examples/styles.scss
@@ -71,6 +71,17 @@
}
}
}
+
+ &.checkbox-example {
+ h4 {
+ margin-top: 20px;
+ font-weight: bold;
+ }
+
+ .fix-size {
+ width: 200px;
+ }
+ }
}
.full-width {
diff --git a/src/components/adslot-ui/Checkbox/index.jsx b/src/components/adslot-ui/Checkbox/index.jsx
index 2a9a56e9f..ccf42da10 100644
--- a/src/components/adslot-ui/Checkbox/index.jsx
+++ b/src/components/adslot-ui/Checkbox/index.jsx
@@ -2,60 +2,60 @@ import _ from 'lodash';
import React from 'react';
import classnames from 'classnames';
import { expandDts } from '../../../lib/utils';
-import { checkboxPropTypes } from '../../prop-types/inputPropTypes';
+import { checkboxPropTypes, checkboxCheckStates } from '../../prop-types/inputPropTypes';
import './styles.scss';
-class Checkbox extends React.Component {
- constructor(props) {
- super(props);
-
- this.onChangeDefault = this.onChangeDefault.bind(this);
- }
-
- onChangeDefault(event) {
- this.props.onChange(event, this.props.name);
- }
-
- render() {
- const { name, value, label, dts, disabled, checked, id, className, inline } = this.props;
-
- const componentClassName = classnames([
- 'checkbox-component',
- { 'checkbox-component-inline': inline },
- { disabled },
- ]);
- const iconClassName = classnames(['selection-component-icon', 'icheckbox', { checked }, { disabled }]);
+const getNextState = checked => {
+ if (checked === 'partial') return false;
+ return !checked;
+};
- return (
-
-