Skip to content

Commit

Permalink
feat: 增加 Radio.Button 组件
Browse files Browse the repository at this point in the history
  • Loading branch information
lijinke666 committed Dec 3, 2018
1 parent 253ed68 commit 0bc2ce5
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 44 deletions.
5 changes: 4 additions & 1 deletion components/checkbox/CheckboxButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import React, { PureComponent } from "react";
import Checkbox from "./Checkbox";

export default class CheckboxButton extends PureComponent {
static defaultProps = {
isButton: true
};
render() {
return <Checkbox {...this.props} isButton />;
return <Checkbox {...this.props} />;
}
}
52 changes: 32 additions & 20 deletions components/checkbox/styles.less
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
@import "../styles/vars.less";

@prefixCls : cuke-checkbox;
@buttonPrefixCls: cuke-checkbox-button;
.@{prefixCls} {
Expand All @@ -10,14 +9,31 @@
line-height: 1;
vertical-align: sub;
white-space: nowrap;

&-group {

.@{prefixCls}-wrapper {
margin-right: @cuke-checkbox-group-offset-right;
}
.@{buttonPrefixCls}-wrapper {
border: 1px solid @border-color;
padding: @cuke-checkbox-button-padding;
border-radius: 0 !important;
margin: 0;
&:first-child {
border-top-left-radius: @border-radius !important;
border-bottom-left-radius: @border-radius !important;
}
&:last-child {
border-bottom-right-radius: @border-radius !important;
border-top-right-radius: @border-radius !important;
}
& + .@{buttonPrefixCls}-wrapper {
border-left: 1px solid transparent;
}
&.@{prefixCls}-disabled:not(:first-child) {
border-left: 1px solid transparent !important;
}
}
}

&-input {
box-sizing: border-box;
padding: 0;
Expand All @@ -29,40 +45,34 @@
opacity: 0;
z-index: 1;
}

&.@{prefixCls}-disabled {
cursor: not-allowed;
}

&-disabled {
.@{prefixCls}-inner {
border-color: @border-color !important;
background-color: @disabled-color !important;
border-color: @border-color !important;
background-color: @disabled-color !important;
&::after {
border-color: @disabled-color;
}
}
.@{prefixCls}-input {
cursor: not-allowed;
}

& + span {
& +span {
cursor: not-allowed;
color: @disabled-font-color;
}
}

&-checked {
.@{prefixCls}-inner {
border-color: @primary-color;
background-color: @primary-color;
&::after {
transform: rotate(45deg) scale(1);
//勾√ 晚一点出现
transform: rotate(45deg) scale(1); //勾√ 晚一点出现
transition: all .2s @animate-type .1s;
}
}

&.@{prefixCls}-disabled {
.@{prefixCls}-inner {
&::after {
Expand All @@ -71,7 +81,6 @@
}
}
}

&-inner {
display: inline-block;
width: @cuke-checkbox-width;
Expand All @@ -83,7 +92,6 @@
left: 0;
top: 0;
border-radius: @border-radius - 2px;

&::after {
//画一个矩形 旋转45° 形成一个 √
transform: rotate(45deg) scale(0);
Expand All @@ -100,7 +108,6 @@
transition: all .1s cubic-bezier(.71, -0.46, .88, .6);
}
}

&-indeterminate {
.@{prefixCls}-inner {
border-color: @primary-color;
Expand All @@ -124,27 +131,32 @@
cursor: pointer;
user-select: none;
transition: all @default-transition;

&.@{prefixCls}-disabled {
cursor: not-allowed;
}

&.@{buttonPrefixCls}-wrapper {
border: 1px solid @border-color;
padding: @cuke-checkbox-button-padding;
border-radius: @border-radius;

margin: 0;
.@{prefixCls} {
display: none;
& +.@{prefixCls} {
margin: 0;
}
}
&.@{prefixCls}-checked {
background-color: @primary-color;
border-color: @primary-color;
color: #fff;
& +.@{prefixCls}-checked {
border-left: 1px solid fade(@border-color, 70%);
}
}
&.@{prefixCls}-disabled {
background-color: @disabled-color;
color: @disabled-font-color;
border-color: @border-color;
}
}
}
13 changes: 10 additions & 3 deletions components/radio/Radio.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,25 @@ export default class Radio extends PureComponent {
className,
children,
disabled,
isButton,
onChange, //eslint-disable-line
...attr
} = this.props;

const { checked } = this.state;

return (
<label className={cls(`${prefixCls}-wrapper`)} {...attr}>
<label
className={cls(`${prefixCls}-wrapper`, {
[`${prefixCls}-button-wrapper`]: isButton,
[`${prefixCls}-checked`]: checked,
[`${prefixCls}-disabled`]: disabled
})}
{...attr}
>
<span
className={cls(prefixCls, className, {
[`${prefixCls}-checked`]: checked,
[`${prefixCls}-disabled`]: disabled
[`${prefixCls}-button`]: isButton
})}
>
<input
Expand Down
11 changes: 11 additions & 0 deletions components/radio/RadioButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React, { PureComponent } from "react";
import Radio from "./Radio";

export default class RadioButton extends PureComponent {
static defaultProps = {
isButton: true
};
render() {
return <Radio {...this.props} />;
}
}
83 changes: 75 additions & 8 deletions components/radio/__tests__/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ exports[`<Radio/> should render Radio 1`] = `
</span>
</label>
<label
class="cuke-radio-wrapper"
class="cuke-radio-wrapper cuke-radio-disabled"
>
<span
class="cuke-radio cuke-radio-disabled"
class="cuke-radio"
>
<input
class="cuke-radio-input"
Expand All @@ -41,10 +41,77 @@ exports[`<Radio/> should render Radio 1`] = `
</label>
<label
checked=""
class="cuke-radio-wrapper"
class="cuke-radio-wrapper cuke-radio-checked cuke-radio-disabled"
>
<span
class="cuke-radio cuke-radio-checked cuke-radio-disabled"
class="cuke-radio"
>
<input
checked=""
class="cuke-radio-input"
disabled=""
type="radio"
/>
<span
class="cuke-radio-inner"
/>
</span>
<span />
</label>
</div>
`;

exports[`<Radio/> should render Radio.Button 1`] = `
<div
class="cuke-radio-group"
>
<label
checked=""
class="cuke-radio-wrapper cuke-radio-button-wrapper cuke-radio-checked"
>
<span
class="cuke-radio cuke-radio-button"
>
<input
checked=""
class="cuke-radio-input"
type="radio"
/>
<span
class="cuke-radio-inner"
/>
</span>
<span>
黄瓜 ui
</span>
</label>
<label
checked=""
class="cuke-radio-wrapper cuke-radio-button-wrapper cuke-radio-checked cuke-radio-disabled"
>
<span
class="cuke-radio cuke-radio-button"
>
<input
checked=""
class="cuke-radio-input"
disabled=""
type="radio"
/>
<span
class="cuke-radio-inner"
/>
</span>
<span>
黄瓜 ui
</span>
</label>
<label
checked=""
class="cuke-radio-wrapper cuke-radio-button-wrapper cuke-radio-checked cuke-radio-disabled"
>
<span
class="cuke-radio cuke-radio-button"
>
<input
checked=""
Expand All @@ -69,10 +136,10 @@ exports[`<Radio/> should render RadioGroup 1`] = `
>
<label
checked=""
class="cuke-radio-wrapper"
class="cuke-radio-wrapper cuke-radio-checked"
>
<span
class="cuke-radio cuke-radio-checked"
class="cuke-radio"
>
<input
checked=""
Expand Down Expand Up @@ -146,10 +213,10 @@ exports[`<Radio/> should render RadioGroup 1`] = `
</span>
</label>
<label
class="cuke-radio-wrapper"
class="cuke-radio-wrapper cuke-radio-disabled"
>
<span
class="cuke-radio cuke-radio-disabled"
class="cuke-radio"
>
<input
class="cuke-radio-input"
Expand Down
11 changes: 10 additions & 1 deletion components/radio/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ describe("<Radio/>", () => {
);
expect(toJson(wrapper)).toMatchSnapshot();
});

it("should render Radio.Button", () => {
const wrapper = render(
<Radio.Group>
<Radio.Button>黄瓜 ui</Radio.Button>
<Radio.Button disabled>黄瓜 ui</Radio.Button>
<Radio.Button checked disabled />
</Radio.Group>
);
expect(toJson(wrapper)).toMatchSnapshot();
});
it("should render RadioGroup", () => {
const wrapper = render(
<div>
Expand Down
2 changes: 2 additions & 0 deletions components/radio/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Radio from "./Radio";
import RadioGroup from "./RadioGroup";
import RadioButton from "./RadioButton";

Radio.Group = RadioGroup;
Radio.Button = RadioButton;

export default Radio;
Loading

0 comments on commit 0bc2ce5

Please sign in to comment.