Skip to content

Commit

Permalink
refactor: Change Button to div to support IE 11 (#288)
Browse files Browse the repository at this point in the history
* move button out of div

* support keyboard

* fix click active

* test coverage

* add coveerage
  • Loading branch information
zombieJ authored Jun 24, 2020
1 parent 5fa3d2d commit 6481b79
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 146 deletions.
31 changes: 7 additions & 24 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,35 +1,18 @@
language: node_js

sudo: false

notifications:
email:
- [email protected]
- [email protected]

node_js:
- 10
- 10

before_install:
- |
if ! git diff --name-only $TRAVIS_COMMIT_RANGE | grep -qvE '(\.md$)|(^(docs|examples))/'
then
echo "Only docs were updated, stopping build process."
exit
fi
phantomjs --version
script:
- |
- |
if [ "$TEST_TYPE" = test ]; then
npm test
npm test -- --coverage && \
bash <(curl -s https://codecov.io/bash)
else
npm run $TEST_TYPE
fi
env:
matrix:
- TEST_TYPE=lint
- TEST_TYPE=test
- TEST_TYPE=coverage
global:
- secure: S1VwbaPzLnSH/IUT/wlJulxAX5VHRIDmSt53h/ycHcZsszUpWcLCJRQAe0fTVB2dAx5MdBbSZ+o+tr3tRwVB5TRAYm0oTCsYAkOZaWOB28RuUQtdGt3wf9xxTG1UiPiaLLUW3waX9zAaf3yqKBcJGf1op0RD8dksxbCFw/7xVbU=
- secure: EBEDg8k///IlEsnx0AE8X3mbFl0QE5+xGKbG4AxXlGZda12uTIPUSMKJzdZQ2hVbZXduTzf1cQl9rcu9nGoSnkL/DWnIax9cvHi+1orx5+YPlxPHNWAwWByTnHosBn2MJhfy1s5paJfHC9cUzmmEL6x4fYthWxjsPUo+irEZH6E=
- TEST_TYPE=lint
- TEST_TYPE=test
- TEST_TYPE=compile
25 changes: 12 additions & 13 deletions assets/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -95,33 +95,32 @@
font-size: 20px;
background: rgba(255, 255, 255, 0.5);
margin: 0;
padding: 8px 16px;
display: flex;
outline: none;
cursor: pointer;
position: relative;
font-weight: lighter;
align-items: center;

&-with-remove {
padding-right: 32px;
&-btn,
&-remove {
border: 0;
background: transparent;
}

&-remove {
position: absolute;
right: 16px;
top: 50%;
transform: translateY(-50%);
&-btn {
font-weight: inherit;
line-height: 32px;
}

&-remove {
&:hover {
color: red;
}
}

&:focus {
background: rgba(0, 0, 255, 0.1);
}

&-active {
padding-left: 30px;
// padding-left: 30px;
font-weight: bolder;
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/mix.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ export default () => {
const [position, setPosition] = React.useState<any>('top');
const [gutter, setGutter] = React.useState(false);
const [fixHeight, setFixHeight] = React.useState(true);
const [rtl, setRTL] = React.useState(true);
const [rtl, setRTL] = React.useState(false);
const [editable, setEditable] = React.useState(true);
const [destroyInactiveTabPane, setDestroyInactiveTabPane] = React.useState(false);
const [destroy, setDestroy] = React.useState(false);
const [animated, setAnimated] = React.useState(true);
const [tabPanes, setTabPanes] = React.useState(getTabPanes(50));
const [tabPanes, setTabPanes] = React.useState(getTabPanes(10));

const editableConfig = editable
? {
Expand Down
62 changes: 40 additions & 22 deletions src/TabNavList/TabNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface TabNodeProps {
rtl: boolean;
closable?: boolean;
editable?: EditableConfig;
onClick?: React.MouseEventHandler<HTMLButtonElement>;
onClick?: (e: React.MouseEvent | React.KeyboardEvent) => void;
onResize?: (width: number, height: number, left: number, top: number) => void;
tabBarGutter?: number;
tabPosition: TabPosition;
Expand Down Expand Up @@ -39,7 +39,7 @@ function TabNode(
onRemove,
onFocus,
}: TabNodeProps,
ref: React.Ref<HTMLButtonElement>,
ref: React.Ref<HTMLDivElement>,
) {
const tabPrefix = `${prefixCls}-tab`;

Expand All @@ -54,6 +54,12 @@ function TabNode(

const removable = editable && closable !== false && !disabled;

function onInternalClick(e: React.MouseEvent | React.KeyboardEvent) {
if (disabled) return;

onClick(e);
}

function onRemoveTab(event: React.MouseEvent | React.KeyboardEvent) {
event.preventDefault();
event.stopPropagation();
Expand All @@ -64,45 +70,57 @@ function TabNode(
}

let node: React.ReactElement = (
<button
<div
key={key}
ref={ref}
type="button"
role="tab"
aria-selected={active}
id={id && `${id}-tab-${key}`}
aria-controls={id && `${id}-panel-${key}`}
tabIndex={0}
disabled={disabled}
className={classNames(tabPrefix, {
[`${tabPrefix}-with-remove`]: removable,
[`${tabPrefix}-active`]: active,
[`${tabPrefix}-disabled`]: disabled,
})}
onClick={onClick}
onFocus={onFocus}
style={nodeStyle}
onClick={onInternalClick}
>
{tab}
{/* Primary Tab Button */}
<div
role="tab"
aria-selected={active}
id={id && `${id}-tab-${key}`}
className={`${tabPrefix}-btn`}
aria-controls={id && `${id}-panel-${key}`}
aria-disabled={disabled}
tabIndex={disabled ? null : 0}
onClick={e => {
e.stopPropagation();
onInternalClick(e);
}}
onKeyDown={e => {
if ([KeyCode.SPACE, KeyCode.ENTER].includes(e.which)) {
e.preventDefault();
onInternalClick(e);
}
}}
onFocus={onFocus}
>
{tab}
</div>

{/* Remove Button */}
{removable && (
<span
role="button"
<button
type="button"
aria-label={removeAriaLabel || 'remove'}
tabIndex={0}
className={`${tabPrefix}-remove`}
onClick={e => {
e.stopPropagation();
onRemoveTab(e);
}}
onKeyDown={e => {
if ([KeyCode.SPACE, KeyCode.ENTER].includes(e.which)) {
onRemoveTab(e);
}
}}
>
{closeIcon || editable.removeIcon || '×'}
</span>
</button>
)}
</button>
</div>
);

if (renderWrapper) {
Expand Down
2 changes: 1 addition & 1 deletion src/TabNavList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function TabNavList(props: TabNavListProps, ref: React.Ref<HTMLDivElement>) {
const tabListRef = useRef<HTMLDivElement>();
const operationsRef = useRef<HTMLDivElement>();
const innerAddButtonRef = useRef<HTMLButtonElement>();
const [getBtnRef, removeBtnRef] = useRefs<HTMLButtonElement>();
const [getBtnRef, removeBtnRef] = useRefs<HTMLDivElement>();

const tabPositionTopOrBottom = tabPosition === 'top' || tabPosition === 'bottom';

Expand Down
84 changes: 48 additions & 36 deletions tests/__snapshots__/index.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,48 @@ exports[`Tabs.Basic Normal 1`] = `
class="rc-tabs-nav-list"
style="transform: translate(0px, 0px);"
>
<button
aria-controls="rc-tabs-test-panel-light"
aria-selected="false"
<div
class="rc-tabs-tab"
id="rc-tabs-test-tab-light"
role="tab"
tabindex="0"
type="button"
>
light
</button>
<button
aria-controls="rc-tabs-test-panel-bamboo"
aria-selected="true"
<div
aria-controls="rc-tabs-test-panel-light"
aria-selected="false"
class="rc-tabs-tab-btn"
id="rc-tabs-test-tab-light"
role="tab"
tabindex="0"
>
light
</div>
</div>
<div
class="rc-tabs-tab rc-tabs-tab-active"
id="rc-tabs-test-tab-bamboo"
role="tab"
tabindex="0"
type="button"
>
bamboo
</button>
<button
aria-controls="rc-tabs-test-panel-cute"
aria-selected="false"
<div
aria-controls="rc-tabs-test-panel-bamboo"
aria-selected="true"
class="rc-tabs-tab-btn"
id="rc-tabs-test-tab-bamboo"
role="tab"
tabindex="0"
>
bamboo
</div>
</div>
<div
class="rc-tabs-tab"
id="rc-tabs-test-tab-cute"
role="tab"
tabindex="0"
type="button"
>
cute
</button>
<div
aria-controls="rc-tabs-test-panel-cute"
aria-selected="false"
class="rc-tabs-tab-btn"
id="rc-tabs-test-tab-cute"
role="tab"
tabindex="0"
>
cute
</div>
</div>
<div
class="rc-tabs-ink-bar rc-tabs-ink-bar-animated"
/>
Expand Down Expand Up @@ -126,17 +135,20 @@ exports[`Tabs.Basic Skip invalidate children 1`] = `
class="rc-tabs-nav-list"
style="transform: translate(0px, 0px);"
>
<button
aria-controls="rc-tabs-test-panel-light"
aria-selected="true"
<div
class="rc-tabs-tab rc-tabs-tab-active"
id="rc-tabs-test-tab-light"
role="tab"
tabindex="0"
type="button"
>
light
</button>
<div
aria-controls="rc-tabs-test-panel-light"
aria-selected="true"
class="rc-tabs-tab-btn"
id="rc-tabs-test-tab-light"
role="tab"
tabindex="0"
>
light
</div>
</div>
<div
class="rc-tabs-ink-bar rc-tabs-ink-bar-animated"
/>
Expand Down
Loading

1 comment on commit 6481b79

@vercel
Copy link

@vercel vercel bot commented on 6481b79 Jun 24, 2020

Choose a reason for hiding this comment

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

Please sign in to comment.