Skip to content

Commit 4bf0b72

Browse files
committed
feat(Switch): add autoWidth, close #2993
1 parent e983006 commit 4bf0b72

File tree

11 files changed

+28
-11
lines changed

11 files changed

+28
-11
lines changed

docs/switch/demo/accessibility.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function onChange(checked) {
2525
}
2626

2727
ReactDOM.render(<div>
28-
<Switch checkedChildren="on" onChange={onChange} unCheckedChildren="off" aria-label="accessible switch"/>
28+
<Switch autoWidth checkedChildren="on" onChange={onChange} unCheckedChildren="off" aria-label="accessible switch"/>
2929

3030
</div>,
3131
mountNode);
@@ -35,4 +35,4 @@ mountNode);
3535
.large-width {
3636
width: 100px;
3737
}
38-
````
38+
````

docs/switch/demo/basic.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function onChange(checked) {
2424
}
2525

2626
ReactDOM.render(<div>
27-
<Switch defaultChecked={false} onChange={onChange} />
27+
<Switch autoWidth defaultChecked={false} onChange={onChange} />
2828
</div>,
2929
mountNode);
3030
````

docs/switch/demo/checked-children.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ function onChange(checked) {
2424
}
2525

2626
ReactDOM.render(<div>
27-
<Switch checkedChildren="on" unCheckedChildren="off" onChange={onChange} />
27+
<Switch autoWidth checkedChildren="on" unCheckedChildren="off" onChange={onChange} />
2828
<br />
29-
<Switch checkedChildren="已启用" unCheckedChildren="已关闭" onChange={onChange}/>
29+
<Switch autoWidth checkedChildren="已启用" unCheckedChildren="已关闭" onChange={onChange}/>
3030
<br />
31-
<Switch checkedChildren={<Icon type="select" size="xs"/>} unCheckedChildren={<Icon type="close" size="xs"/>} onChange={onChange} />
31+
<Switch autoWidth checkedChildren={<Icon type="select" size="xs"/>} unCheckedChildren={<Icon type="close" size="xs"/>} onChange={onChange} />
3232
</div>,
3333
mountNode);
3434
````

docs/switch/demo/disabled.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const Demo = () => {
2727

2828
return (
2929
<div>
30-
<Switch disabled={disabled} defaultChecked />
30+
<Switch autoWidth disabled={disabled} defaultChecked />
3131
<br />
3232
<Button type="primary" onClick={toggle}>
3333
Toggle disabled

docs/switch/demo/loading.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Switch with loading
1919
import { Switch } from '@alifd/next';
2020

2121
ReactDOM.render(<div>
22-
<Switch loading defaultChecked={false} /> <Switch loading defaultChecked />
22+
<Switch autoWidth loading defaultChecked={false} /> <Switch loading defaultChecked />
2323
</div>,
2424
mountNode);
2525
````

docs/switch/demo/size.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import { Switch } from '@alifd/next';
2020

2121
ReactDOM.render(
2222
<div>
23-
<Switch />
23+
<Switch autoWidth/>
2424
<br/>
25-
<Switch size="small"/>
25+
<Switch autoWidth size="small"/>
2626
</div>,
2727
mountNode
2828
);

docs/switch/index.md

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
对于 checkChildren 和 unCheckedChildren 的自定义要考虑文字大小,因为 switch 的宽度有限,默认一个汉字大小。如果设置成多个字或者英文要注意宽度控制。
1919

20+
## FAQ
21+
1.23 版本增加了 `autoWidth` API,我们推荐用户默认开启,同时在2.0里也会默认设置为true。开启后,原 `<Switch style={{display: 'block'}}>` 写法的用户可能会出现样式异常。
2022
## API
2123

2224
### Switch

src/switch/index.jsx

+8
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ class Switch extends React.Component {
7878
* @param {number} value 评分值
7979
*/
8080
renderPreview: PropTypes.func,
81+
/**
82+
* 开启后宽度根据内容自适应
83+
* @version 1.23
84+
*/
85+
autoWidth: PropTypes.bool,
8186
/**
8287
* 国际化配置
8388
*/
@@ -91,6 +96,7 @@ class Switch extends React.Component {
9196
isPreview: false,
9297
loading: false,
9398
readOnly: false,
99+
autoWidth: false,
94100
onChange: () => {},
95101
locale: zhCN.Switch,
96102
};
@@ -143,6 +149,7 @@ class Switch extends React.Component {
143149
readOnly,
144150
size,
145151
loading,
152+
autoWidth,
146153
checkedChildren,
147154
unCheckedChildren,
148155
rtl,
@@ -165,6 +172,7 @@ class Switch extends React.Component {
165172
[`${prefix}switch-loading`]: loading,
166173
[`${prefix}switch-${status}`]: true,
167174
[`${prefix}switch-${_size}`]: true,
175+
[`${prefix}switch-auto-width`]: autoWidth,
168176
[className]: className,
169177
});
170178
let attrs;

src/switch/main.scss

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
cursor: pointer;
1313
vertical-align: middle;
1414
user-select: none;
15+
overflow: hidden;
1516

1617
&-btn {
1718
transition: all $motion-duration-immediately $motion-linear;

src/switch/scss/mixin.scss

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@
1313
position: relative;
1414
display: inline-block;
1515
border: $border-width-container solid transparent;
16-
min-width: $width;
16+
width: $width;
1717
height: calc(#{$trigger-size} + #{$border-width-container} * 2);
1818
border-radius: $container-radius;
1919

20+
&.#{$css-prefix}switch-auto-width {
21+
min-width: $width;
22+
width: auto;
23+
overflow: initial;
24+
}
2025
&:after {
2126
content: '';
2227
}

types/switch/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export interface SwitchProps extends HTMLAttributesWeak, CommonProps {
4040
* 开关当前的值(针对受控组件)
4141
*/
4242
checked?: boolean;
43+
autoWidth?: boolean;
4344

4445
/**
4546
* 开关默认值 (针对非受控组件)

0 commit comments

Comments
 (0)