Skip to content

Commit

Permalink
feat: add support use dots to provide dotsClasse (ant-design#21708)
Browse files Browse the repository at this point in the history
* add support use dots to provide dotsClasse

* update docs
  • Loading branch information
yoyo837 authored Mar 3, 2020
1 parent a226c22 commit 68f8851
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
14 changes: 14 additions & 0 deletions components/carousel/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,18 @@ describe('Carousel', () => {
).toBeTruthy();
});
});

describe('dots precise control by plain object', () => {
it('use dots to provide dotsClasse', () => {
const wrapper = mount(
<Carousel dots={{ dotsClass: 'customDots' }}>
<div>1</div>
<div>2</div>
<div>3</div>
</Carousel>,
);
wrapper.update();
expect(wrapper.find('.slick-dots').hasClass('customDots')).toBeTruthy();
});
});
});
2 changes: 1 addition & 1 deletion components/carousel/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ A carousel component. Scales with its container.
| autoplay | Whether to scroll automatically | boolean | `false` | |
| beforeChange | Callback function called before the current index changes | function(from, to) | - | |
| dotPosition | The position of the dots, which can be one of `top` `bottom` `left` `right` | string | bottom | |
| dots | Whether to show the dots at the bottom of the gallery | boolean | `true` | |
| dots | Whether to show the dots at the bottom of the gallery, `string` for `dotsClass` | boolean \| { dotsClass?:string } | `true` | |
| easing | Transition interpolation function name | string | `linear` | |
| effect | Transition effect | `scrollx` \| `fade` | `scrollx` | |

Expand Down
18 changes: 15 additions & 3 deletions components/carousel/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import isPlainObject from 'lodash/isPlainObject';
import debounce from 'lodash/debounce';
import { Settings } from '@ant-design/react-slick';
import classNames from 'classnames';
Expand All @@ -14,13 +15,18 @@ export type CarouselEffect = 'scrollx' | 'fade';
export type DotPosition = 'top' | 'bottom' | 'left' | 'right';

// Carousel
export interface CarouselProps extends Settings {
export interface CarouselProps extends Omit<Settings, 'dots' | 'dotsClass'> {
effect?: CarouselEffect;
style?: React.CSSProperties;
prefixCls?: string;
slickGoTo?: number;
dotPosition?: DotPosition;
children?: React.ReactNode;
dots?:
| boolean
| {
dotsClass?: string;
};
}

export default class Carousel extends React.Component<CarouselProps, {}> {
Expand Down Expand Up @@ -106,7 +112,13 @@ export default class Carousel extends React.Component<CarouselProps, {}> {
const dotsClass = 'slick-dots';
const dotPosition = this.getDotPosition();
props.vertical = dotPosition === 'left' || dotPosition === 'right';
props.dotsClass = `${dotsClass} ${dotsClass}-${dotPosition || 'bottom'}`;

const enableDots = props.dots === true || isPlainObject(props.dots);
const dsClass = classNames(
dotsClass,
`${dotsClass}-${dotPosition || 'bottom'}`,
typeof props.dots === 'boolean' ? false : props.dots?.dotsClass,
);

const className = classNames(prefixCls, {
[`${prefixCls}-rtl`]: direction === 'rtl',
Expand All @@ -115,7 +127,7 @@ export default class Carousel extends React.Component<CarouselProps, {}> {

return (
<div className={className}>
<SlickCarousel ref={this.saveSlick} {...props} />
<SlickCarousel ref={this.saveSlick} {...props} dots={enableDots} dotsClass={dsClass} />
</div>
);
};
Expand Down
2 changes: 1 addition & 1 deletion components/carousel/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ subtitle: 走马灯
| autoplay | 是否自动切换 | boolean | false | | |
| beforeChange | 切换面板的回调 | function(from, to) || | |
| dotPosition | 面板指示点位置,可选 `top` `bottom` `left` `right` | string | bottom | |
| dots | 是否显示面板指示点 | boolean | true | | |
| dots | 是否显示面板指示点,如果为 `string` 则指定为 `dotsClass` | boolean \| { dotsClass?:string } | true | | |
| easing | 动画效果 | string | linear | | |
| effect | 动画效果函数,可取 scrollx, fade | string | scrollx | | |

Expand Down

0 comments on commit 68f8851

Please sign in to comment.