Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: autofocus input from dialog no effect #43

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions assets/autofocus.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
.rc-dialog {
position: relative;
width: auto;
margin: 10px;

&-wrap {
position: fixed;
overflow: auto;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 1050;
-webkit-overflow-scrolling: touch;
outline: 0;
}

&-title {
margin: 0;
font-size: 14px;
line-height: 21px;
font-weight: bold;
}

&-content {
position: relative;
background-color: #ffffff;
border: none;
border-radius: 6px 6px;
background-clip: padding-box;
}

&-close {
cursor: pointer;
border: 0;
background: transparent;
font-size: 21px;
position: absolute;
right: 20px;
top: 12px;
font-weight: 700;
line-height: 1;
color: #000;
text-shadow: 0 1px 0 #fff;
filter: alpha(opacity=20);
opacity: .2;
text-decoration: none;

&-x:after {
content: '×'
}

&:hover {
opacity: 1;
filter: alpha(opacity=100);
text-decoration: none;
}
}
Comment on lines +33 to +58
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

优化关闭按钮的实现

关闭按钮的当前实现存在以下问题:

  1. 缺少键盘访问支持
  2. 使用伪元素添加 '×' 符号不利于国际化
  3. 缺少 ARIA 标签

建议进行如下修改:

&-close {
  cursor: pointer;
  border: 0;
  background: transparent;
  font-size: 21px;
  position: absolute;
  right: 20px;
  top: 12px;
  font-weight: 700;
  line-height: 1;
  color: #000;
  text-shadow: 0 1px 0 #fff;
  filter: alpha(opacity=20);
  opacity: .2;
  text-decoration: none;
+ outline: none;
+ aria-label: "关闭";

- &-x:after {
-   content: '×'
- }

+ &:focus {
+   opacity: 1;
+   box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.1);
+ }

  &:hover {
    opacity: 1;
    filter: alpha(opacity=100);
    text-decoration: none;
  }
}

Committable suggestion skipped: line range outside the PR's diff.


&-header {
padding: 13px 20px 14px 20px;
border-radius: 5px 5px 0 0;
background: #fff;
color: #666;
border-bottom: 1px solid #e9e9e9;
}

&-body {
padding: 20px;
}

&-footer {
border-top: 1px solid #e9e9e9;
padding: 10px 20px;
text-align: right;
border-radius: 0 0 5px 5px;
}

.effect() {
animation-duration: 0.3s;
animation-fill-mode: both;
}

&-zoom-enter, &-zoom-appear {
opacity: 0;
.effect();
animation-timing-function: cubic-bezier(0.08, 0.82, 0.17, 1);
animation-play-state: paused;
}

&-zoom-leave {
.effect();
animation-timing-function: cubic-bezier(0.6, 0.04, 0.98, 0.34);
animation-play-state: paused;
}

&-zoom-enter&-zoom-enter-active, &-zoom-appear&-zoom-appear-active {
animation-name: rcDialogZoomIn;
animation-play-state: running;
}

&-zoom-leave&-zoom-leave-active {
animation-name: rcDialogZoomOut;
animation-play-state: running;
}

@keyframes rcDialogZoomIn {
0% {
opacity: 0;
transform: scale(0, 0);
}
100% {
opacity: 1;
transform: scale(1, 1);
}
}
@keyframes rcDialogZoomOut {
0% {

transform: scale(1, 1);
}
100% {
opacity: 0;
transform: scale(0, 0);
}
}
}

@media (min-width: 768px) {
.rc-dialog {
width: 600px;
margin: 30px auto;
}
}
1 change: 1 addition & 0 deletions assets/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@
}
}
}
@import './autofocus.less';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not import modal styles, it is for debug only.

8 changes: 8 additions & 0 deletions docs/demo/autofocus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: AutoFocus
nav:
title: Demo
path: /demo
---

<code src="../examples/autofocus.tsx"></code>
31 changes: 31 additions & 0 deletions docs/examples/autofocus.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Dialog from 'rc-dialog';
import Input from 'rc-input';
import type { FC } from 'react';
import React from 'react';
import '../../assets/index.less';

const Demo: FC = () => {
const [vis, setVis] = React.useState(false);
return (
<div>
normal:
<Input placeholder="input" autoFocus />
<br />
<br />
dialog:
<button onClick={() => setVis((pre) => !pre)}>
toggle autoFocus input{' '}
</button>
<Dialog
visible={vis}
onClose={() => setVis(false)}
style={{ width: 600 }}
title={<div>第二个弹框</div>}
>
<Input placeholder="input" autoFocus />
</Dialog>
</div>
);
};

export default Demo;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"np": "^7.0.0",
"prettier": "^2.0.5",
"pretty-quick": "^3.0.0",
"rc-dialog": "^9.1.0",
"rc-test": "^7.0.15",
"react": "^18.0.0",
"react-dom": "^18.0.0",
Expand Down
6 changes: 6 additions & 0 deletions src/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@
}
setFocused((prev) => (prev && disabled ? false : prev));
}, [disabled]);

useEffect(() => {
if (rest.autoFocus) {
focus();

Check warning on line 105 in src/Input.tsx

View check run for this annotation

Codecov / codecov/patch

src/Input.tsx#L105

Added line #L105 was not covered by tests
}
}, [rest.autoFocus]);

const triggerChange = (
e:
Expand Down