Skip to content

Commit

Permalink
Merge pull request #365 from guanpu/checkbox/rtl
Browse files Browse the repository at this point in the history
[Checkbox]: RTL feature
  • Loading branch information
youluna authored Feb 21, 2019
2 parents 912bc65 + 313f1ac commit dcf3eda
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 16 deletions.
17 changes: 8 additions & 9 deletions docs/checkbox/demo/basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ Basic components rendered using `Checkbox`.
````jsx
import { Checkbox } from '@alifd/next';

ReactDOM.render((
ReactDOM.render(
<div>
<h6>Different status without label:</h6>
Unchecked:<Checkbox />&nbsp;
DefaultChecked:<Checkbox defaultChecked />&nbsp;
Indeterminate:<Checkbox defaultIndeterminate />&nbsp;
Disabled:<Checkbox disabled />&nbsp;
Checked-Disabled:<Checkbox disabled checked />&nbsp;
Checked:<Checkbox checked />&nbsp;
<Checkbox >Unchecked</Checkbox>
<Checkbox defaultChecked >DefaultChecked</Checkbox>
<Checkbox defaultIndeterminate >Indeterminate</Checkbox>
<Checkbox disabled >Disabled</Checkbox>;
<Checkbox disabled checked >Checked-Disabled</Checkbox>
<Checkbox checked >Checked</Checkbox>
<h6>Using with label</h6>
<Checkbox>Banana</Checkbox>&nbsp;
<Checkbox id="apple" /><label htmlFor="apple" className="next-checkbox-label">Apple</label>&nbsp;
Expand All @@ -32,6 +32,5 @@ ReactDOM.render((
<span className="next-checkbox-label">Pear</span>
</label>&nbsp;
<Checkbox label="Banana" />
</div>
), mountNode);
</div>, mountNode);
````
20 changes: 13 additions & 7 deletions src/checkbox/checkbox-group.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {Component} from 'react';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import ConfigProvider from '../config-provider';
Expand Down Expand Up @@ -108,7 +108,7 @@ class CheckboxGroup extends Component {

componentWillReceiveProps(nextProps) {
if ('value' in nextProps) {
let {value} = nextProps;
let { value } = nextProps;
if (!Array.isArray(value)) {
if (value === null || value === undefined) {
value = [];
Expand All @@ -123,7 +123,7 @@ class CheckboxGroup extends Component {
}

onChange(currentValue, e) {
const {value} = this.state;
const { value } = this.state;
const index = value.indexOf(currentValue);
const valTemp = [...value];

Expand All @@ -134,19 +134,24 @@ class CheckboxGroup extends Component {
}

if (!('value' in this.props)) {
this.setState({value: valTemp});
this.setState({ value: valTemp });
}
this.props.onChange(valTemp, e);
}

render() {
const { className, style, prefix, disabled, itemDirection } = this.props;
const { className, style, prefix, disabled, itemDirection, rtl } = this.props;
const others = pickOthers(CheckboxGroup.propTypes, this.props);

// 如果内嵌标签跟dataSource同时存在,以内嵌标签为主
let children;
if (this.props.children) {
children = this.props.children;
children = React.Children.map(this.props.children, (child) => {
if (!React.isValidElement(child)) {
return child;
}
return React.cloneElement(child, child.props.rtl === undefined ? { rtl } : null);
});
} else {
children = this.props.dataSource.map((item, index) => {
let option = item;
Expand All @@ -163,6 +168,7 @@ class CheckboxGroup extends Component {
<Checkbox key={index}
value={option.value}
checked={checked}
rtl={rtl}
disabled={disabled || option.disabled}
label={option.label}
/>
Expand All @@ -177,7 +183,7 @@ class CheckboxGroup extends Component {
disabled
});

return <span {...others} className={cls} style={style}>{children}</span>;
return <span dir={rtl ? 'rtl' : undefined} {...others} className={cls} style={style}>{children}</span>;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/checkbox/checkbox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ class Checkbox extends UIState {
{...othersData}
className={cls}
style={style}
dir={rtl ? 'rtl' : undefined}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
>
Expand Down
2 changes: 2 additions & 0 deletions src/checkbox/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

@import '../core/index-noreset';
@import 'scss/variable';
@import './rtl.scss';

/* stylelint-disable max-nesting-depth */

Expand Down Expand Up @@ -153,6 +154,7 @@
#{$checkbox-prefix}-wrapper {
display: block;
margin-left: 0;
margin-right: 0;
margin-bottom: 8px;
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/checkbox/rtl.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#{$checkbox-prefix}-wrapper[dir="rtl"] {
margin-right: 8px;
margin-left: 0;
&:first-child {
margin-right: 0;
}

> #{$checkbox-prefix}-label {
margin-right: $checkbox-margin-left;
margin-left: 0;
}
}
9 changes: 9 additions & 0 deletions test/checkbox/group-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ describe('Checkbox.Group', () => {
const wrapper = mount(<CheckboxGroup value={['pear']} dataSource={list} />);
assert(wrapper.find('.next-checkbox-group').children().length === 3);
});

it('should support null child', ()=>{
const wrapper = mount(<CheckboxGroup>
<Checkbox>1</Checkbox>
<Checkbox>2</Checkbox>
{null}
</CheckboxGroup>);
assert(wrapper.find('.next-checkbox-group').children().length === 2);
});
});

describe('[render] uncontrol', () => {
Expand Down

0 comments on commit dcf3eda

Please sign in to comment.