Skip to content

Commit

Permalink
fix: Deploy app select vxnet (#314)
Browse files Browse the repository at this point in the history
* Fix Checkbox component no value prop
  • Loading branch information
whDongRui authored and sunnywx committed Aug 29, 2018
1 parent bad206c commit b92504d
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 41 deletions.
19 changes: 10 additions & 9 deletions src/components/Base/Checkbox/checkbox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default class Checkbox extends Component {
static propTypes = {
className: PropTypes.string,
name: PropTypes.string,
value: PropTypes.string,
disabled: PropTypes.bool,
checked: PropTypes.bool,
onChange: PropTypes.func
Expand All @@ -34,8 +35,6 @@ export default class Checkbox extends Component {
if (disabled) {
return;
}
const isChecked = this.state.isChecked;
this.setState({ isChecked: !isChecked });
onChange && onChange(e);
};

Expand All @@ -46,12 +45,9 @@ export default class Checkbox extends Component {
}

render() {
const { style, className, disabled, children } = this.props;
const { style, className, disabled, children, value } = this.props;
const isChecked = this.state.isChecked;
const color = {
primary: '#fff',
secondary: '#fff'
};

return (
<label
className={classNames(styles.checkbox, className, {
Expand All @@ -60,8 +56,13 @@ export default class Checkbox extends Component {
disabled={disabled}
style={style}
>
{isChecked && <Icon name="check" color={color} />}
<input type="checkbox" checked={isChecked} onChange={this.handleToggleCheck} />
{isChecked && <Icon name="check" />}
<input
type="checkbox"
value={value}
checked={isChecked}
onChange={this.handleToggleCheck}
/>
{children}
</label>
);
Expand Down
43 changes: 20 additions & 23 deletions src/components/Base/Checkbox/group.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,47 @@ export default class CheckboxGroup extends Component {
children: PropTypes.oneOfType([PropTypes.node, PropTypes.arrayOf(PropTypes.node)]),
values: PropTypes.array,
name: PropTypes.string,
onChange: PropTypes.func,
onChange: PropTypes.func
};

static defaultProps = {
children: [],
values: [],
name: '',
name: ''
};

constructor(props) {
super(props);

this.values = [...props.values];
}

handleChange = (e) => {
handleChange = e => {
e.stopPropagation();

const { name, onChange } = this.props;
const targetValue = e.target.value;
let values = this.props.values;

if (e.target.checked) {
this.values.push(targetValue);
values.push(targetValue);
} else {
remove(this.values, v => v === targetValue);
remove(values, v => v === targetValue);
}
onChange && onChange(this.values, name);
}
onChange && onChange(values, name);
};

render() {
const { className, name, children } = this.props;

const childNodes = React.Children.map(children, (child) => React.cloneElement(child, {
...child.props,
key: `check-${child.props.value}`,
name: child.props.name || name,
checked: this.values.indexOf(child.props.value) !== -1,
onChange: this.handleChange,
}));

return (
<div className={classNames(styles.group, className)}>
{childNodes}
</div>
const { className, name, children, values } = this.props;

const childNodes = React.Children.map(children, child =>
React.cloneElement(child, {
...child.props,
key: `check-${child.props.value}`,
name: child.props.name || name,
checked: values.indexOf(child.props.value) !== -1,
onChange: this.handleChange
})
);

return <div className={classNames(styles.group, className)}>{childNodes}</div>;
}
}
4 changes: 4 additions & 0 deletions src/components/Base/Checkbox/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
.icon {
display: block;
z-index: 3;
svg {
--primary-color: #{$N0};
--secondary-color: #{$N0};
}
}
}
&:hover{
Expand Down
9 changes: 6 additions & 3 deletions src/components/Base/Select/select.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ export default class Select extends React.Component {
const { isOpen } = this.state;
const { disabled } = this.props;

if (disabled) {
return;
}
if (isOpen) {
document.removeEventListener('click', this.handleOutsideClick.bind(this));
} else if (!disabled) {
} else {
document.addEventListener('click', this.handleOutsideClick.bind(this));
}

Expand Down Expand Up @@ -82,11 +85,11 @@ export default class Select extends React.Component {

renderControl() {
const { isOpen } = this.state;
const { disabled } = this.props;
const { value, disabled } = this.props;

return (
<div className={styles.control} onClick={this.handleControlClick}>
<div className={styles.controlLabel}>{this.currentLabel}</div>
<div className={styles.controlLabel}>{value}</div>
<Icon name={isOpen && !disabled ? 'caret-up' : 'caret-down'} type="dark" />
</div>
);
Expand Down
14 changes: 12 additions & 2 deletions src/pages/Admin/Apps/Deploy/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,12 @@ export default class AppDeploy extends Component {
</div>
<div className={styles.cellModule}>
<label className={classnames(styles.name, styles.selectName)}>Version</label>
<Select className={styles.select} value={versionId} onChange={changeVersion}>
<Select
className={styles.select}
value={versionId}
onChange={changeVersion}
disabled={versions.length === 0}
>
{versions.map(({ version_id, name }) => (
<Select.Option key={version_id} value={version_id} title={name}>
{name}
Expand Down Expand Up @@ -167,7 +172,12 @@ export default class AppDeploy extends Component {
</div>
<div className={styles.cellModule}>
<label className={classnames(styles.name, styles.selectName)}>VxNet to Join</label>
<Select className={styles.select} value={subnetId} onChange={changeSubnet}>
<Select
className={styles.select}
value={subnetId}
onChange={changeSubnet}
disabled={subnets.length === 0}
>
{subnets.map(({ subnet_id, name }) => (
<Select.Option key={subnet_id} value={subnet_id}>
{subnet_id}
Expand Down
2 changes: 2 additions & 0 deletions src/stores/app/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ export default class AppDeployStore extends Store {
let arrSubnets = this.subnets.toJSON();
if (arrSubnets[0]) {
this.subnetId = arrSubnets[0].subnet_id;
} else {
this.subnetId = '';
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/stores/repo/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export default class RepoCreateStore extends Store {
const data = getFormData(e.target);

if (_.isEmpty(providers)) {
return this.showMsg('please select at least one provider');
return this.showMsg('Please select at least one Runtime Provider');
}

for (let i = 0; i < this.selectors.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion test/components/Base/Checkbox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('Base/Checkbox', () => {
wrapper.find('input').simulate('change');

const isChecked = wrapper.state().isChecked;
expect(isChecked).not.toBeTruthy();
expect(isChecked).toBeTruthy();
expect(mockChange).toHaveBeenCalled();
expect(mockChange).toHaveBeenCalledTimes(1);
expect(mockChange.mock.calls[0][0].target.checked).toEqual(isChecked);
Expand Down
5 changes: 3 additions & 2 deletions test/components/Base/__snapshots__/Checkbox.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ exports[`Base/Checkbox group render 1`] = `
>
<svg
class="qicon qicon-check qicon-light"
style="--primary-color:#fff;--secondary-color:#fff"
>
<use
href="#check"
Expand All @@ -35,6 +34,7 @@ exports[`Base/Checkbox group render 1`] = `
<input
checked=""
type="checkbox"
value="1"
/>
option1
</label>
Expand All @@ -43,6 +43,7 @@ exports[`Base/Checkbox group render 1`] = `
>
<input
type="checkbox"
value="2"
/>
option2
</label>
Expand All @@ -55,7 +56,6 @@ exports[`Base/Checkbox group render 1`] = `
>
<svg
class="qicon qicon-check qicon-light"
style="--primary-color:#fff;--secondary-color:#fff"
>
<use
href="#check"
Expand All @@ -65,6 +65,7 @@ exports[`Base/Checkbox group render 1`] = `
<input
checked=""
type="checkbox"
value="3"
/>
option3
</label>
Expand Down

0 comments on commit b92504d

Please sign in to comment.