1 | # rc-util
|
2 |
|
3 | Common Utils For React Component.
|
4 |
|
5 | [![NPM version][npm-image]][npm-url]
|
6 | [![npm download][download-image]][download-url]
|
7 | [![build status][github-actions-image]][github-actions-url]
|
8 | [![Codecov][codecov-image]][codecov-url]
|
9 | [![bundle size][bundlephobia-image]][bundlephobia-url]
|
10 | [![dumi][dumi-image]][dumi-url]
|
11 |
|
12 | [npm-image]: http://img.shields.io/npm/v/rc-util.svg?style=flat-square
|
13 | [npm-url]: http://npmjs.org/package/rc-util
|
14 | [travis-image]: https://img.shields.io/travis/react-component/util/master?style=flat-square
|
15 | [travis-url]: https://travis-ci.com/react-component/util
|
16 | [github-actions-image]: https://github.com/react-component/util/workflows/CI/badge.svg
|
17 | [github-actions-url]: https://github.com/react-component/util/actions
|
18 | [codecov-image]: https://img.shields.io/codecov/c/github/react-component/util/master.svg?style=flat-square
|
19 | [codecov-url]: https://app.codecov.io/gh/react-component/util
|
20 | [david-url]: https://david-dm.org/react-component/util
|
21 | [david-image]: https://david-dm.org/react-component/util/status.svg?style=flat-square
|
22 | [david-dev-url]: https://david-dm.org/react-component/util?type=dev
|
23 | [david-dev-image]: https://david-dm.org/react-component/util/dev-status.svg?style=flat-square
|
24 | [download-image]: https://img.shields.io/npm/dm/rc-util.svg?style=flat-square
|
25 | [download-url]: https://npmjs.org/package/rc-util
|
26 | [bundlephobia-url]: https://bundlephobia.com/package/rc-util
|
27 | [bundlephobia-image]: https://badgen.net/bundlephobia/minzip/rc-util
|
28 | [dumi-url]: https://github.com/umijs/dumi
|
29 | [dumi-image]: https://img.shields.io/badge/docs%20by-dumi-blue?style=flat-square
|
30 |
|
31 | ## Install
|
32 |
|
33 | [![rc-util](https://nodei.co/npm/rc-util.png)](https://npmjs.org/package/rc-util)
|
34 |
|
35 | ## API
|
36 |
|
37 | ### createChainedFunction
|
38 |
|
39 | > (...functions): Function
|
40 |
|
41 | Create a function which will call all the functions with it's arguments from left to right.
|
42 |
|
43 | ```jsx|pure
|
44 | import createChainedFunction from 'rc-util/lib/createChainedFunction';
|
45 | ```
|
46 |
|
47 | ### deprecated
|
48 |
|
49 | > (prop: string, instead: string, component: string): void
|
50 |
|
51 | Log an error message to warn developers that `prop` is deprecated.
|
52 |
|
53 | ```jsx|pure
|
54 | import deprecated from 'rc-util/lib/deprecated';
|
55 | ```
|
56 |
|
57 | ### getContainerRenderMixin
|
58 |
|
59 | > (config: Object): Object
|
60 |
|
61 | To generate a mixin which will render specific component into specific container automatically.
|
62 |
|
63 | ```jsx|pure
|
64 | import getContainerRenderMixin from 'rc-util/lib/getContainerRenderMixin';
|
65 | ```
|
66 |
|
67 | Fields in `config` and their meanings.
|
68 |
|
69 | | Field | Type | Description | Default |
|
70 | | ------------- | ---------------------------- | -------------------------------------------------------------------------- | ------- |
|
71 | | autoMount | boolean | Whether to render component into container automatically | true |
|
72 | | autoDestroy | boolean | Whether to remove container automatically while the component is unmounted | true |
|
73 | | isVisible | (instance): boolean | A function to get current visibility of the component | - |
|
74 | | isForceRender | (instance): boolean | A function to determine whether to render popup even it's not visible | - |
|
75 | | getComponent | (instance, extra): ReactNode | A function to get the component which will be rendered into container | - |
|
76 | | getContainer | (instance): HTMLElement | A function to get the container | |
|
77 |
|
78 | ### Portal
|
79 |
|
80 | Render children to the specific container;
|
81 |
|
82 | ```jsx|pure
|
83 | import Portal from 'rc-util/lib/Portal';
|
84 | ```
|
85 |
|
86 | Props:
|
87 |
|
88 | | Prop | Type | Description | Default |
|
89 | | ------------ | --------------- | ------------------------------- | ------- |
|
90 | | children | ReactChildren | Content render to the container | - |
|
91 | | getContainer | (): HTMLElement | A function to get the container | - |
|
92 |
|
93 | ### getScrollBarSize
|
94 |
|
95 | > (fresh?: boolean): number
|
96 |
|
97 | Get the width of scrollbar.
|
98 |
|
99 | ```jsx|pure
|
100 | import getScrollBarSize from 'rc-util/lib/getScrollBarSize';
|
101 | ```
|
102 |
|
103 | ### guid
|
104 |
|
105 | > (): string
|
106 |
|
107 | To generate a global unique id across current application.
|
108 |
|
109 | ```jsx|pure
|
110 | import guid from 'rc-util/lib/guid';
|
111 | ```
|
112 |
|
113 | ### pickAttrs
|
114 |
|
115 | > (props: Object): Object
|
116 |
|
117 | Pick valid HTML attributes and events from props.
|
118 |
|
119 | ```jsx|pure
|
120 | import pickAttrs from 'rc-util/lib/pickAttrs';
|
121 | ```
|
122 |
|
123 | ### warn
|
124 |
|
125 | > (msg: string): void
|
126 |
|
127 | A shallow wrapper of `console.warn`.
|
128 |
|
129 | ```jsx|pure
|
130 | import warn from 'rc-util/lib/warn';
|
131 | ```
|
132 |
|
133 | ### warning
|
134 |
|
135 | > (valid: boolean, msg: string): void
|
136 |
|
137 | A shallow wrapper of [warning](https://github.com/BerkeleyTrue/warning), but only warning once for the same message.
|
138 |
|
139 | ```jsx|pure
|
140 | import warning, { noteOnce } from 'rc-util/lib/warning';
|
141 |
|
142 | warning(false, '[antd Component] test hello world');
|
143 |
|
144 | // Low level note
|
145 | noteOnce(false, '[antd Component] test hello world');
|
146 | ```
|
147 |
|
148 | ### Children
|
149 |
|
150 | A collection of functions to operate React elements' children.
|
151 |
|
152 | #### Children/mapSelf
|
153 |
|
154 | > (children): children
|
155 |
|
156 | Return a shallow copy of children.
|
157 |
|
158 | ```jsx|pure
|
159 | import mapSelf from 'rc-util/lib/Children/mapSelf';
|
160 | ```
|
161 |
|
162 | #### Children/toArray
|
163 |
|
164 | > (children: ReactNode[]): ReactNode[]
|
165 |
|
166 | Convert children into an array.
|
167 |
|
168 | ```jsx|pure
|
169 | import toArray from 'rc-util/lib/Children/toArray';
|
170 | ```
|
171 |
|
172 | ### Dom
|
173 |
|
174 | A collection of functions to operate DOM elements.
|
175 |
|
176 | #### Dom/addEventlistener
|
177 |
|
178 | > (target: ReactNode, eventType: string, listener: Function): { remove: Function }
|
179 |
|
180 | A shallow wrapper of [add-dom-event-listener](https://github.com/yiminghe/add-dom-event-listener).
|
181 |
|
182 | ```jsx|pure
|
183 | import addEventlistener from 'rc-util/lib/Dom/addEventlistener';
|
184 | ```
|
185 |
|
186 | #### Dom/canUseDom
|
187 |
|
188 | > (): boolean
|
189 |
|
190 | Check if DOM is available.
|
191 |
|
192 | ```jsx|pure
|
193 | import canUseDom from 'rc-util/lib/Dom/canUseDom';
|
194 | ```
|
195 |
|
196 | #### Dom/class
|
197 |
|
198 | A collection of functions to operate DOM nodes' class name.
|
199 |
|
200 | - `hasClass(node: HTMLElement, className: string): boolean`
|
201 | - `addClass(node: HTMLElement, className: string): void`
|
202 | - `removeClass(node: HTMLElement, className: string): void`
|
203 |
|
204 | ```jsx|pure
|
205 | import cssClass from 'rc-util/lib/Dom/class;
|
206 | ```
|
207 |
|
208 | #### Dom/contains
|
209 |
|
210 | > (root: HTMLElement, node: HTMLElement): boolean
|
211 |
|
212 | Check if node is equal to root or in the subtree of root.
|
213 |
|
214 | ```jsx|pure
|
215 | import contains from 'rc-util/lib/Dom/contains';
|
216 | ```
|
217 |
|
218 | #### Dom/css
|
219 |
|
220 | A collection of functions to get or set css styles.
|
221 |
|
222 | - `get(node: HTMLElement, name?: string): any`
|
223 | - `set(node: HTMLElement, name?: string, value: any) | set(node, object)`
|
224 | - `getOuterWidth(el: HTMLElement): number`
|
225 | - `getOuterHeight(el: HTMLElement): number`
|
226 | - `getDocSize(): { width: number, height: number }`
|
227 | - `getClientSize(): { width: number, height: number }`
|
228 | - `getScroll(): { scrollLeft: number, scrollTop: number }`
|
229 | - `getOffset(node: HTMLElement): { left: number, top: number }`
|
230 |
|
231 | ```jsx|pure
|
232 | import css from 'rc-util/lib/Dom/css';
|
233 | ```
|
234 |
|
235 | #### Dom/focus
|
236 |
|
237 | A collection of functions to operate focus status of DOM node.
|
238 |
|
239 | - `saveLastFocusNode(): void`
|
240 | - `clearLastFocusNode(): void`
|
241 | - `backLastFocusNode(): void`
|
242 | - `getFocusNodeList(node: HTMLElement): HTMLElement[]` get a list of focusable nodes from the subtree of node.
|
243 | - `limitTabRange(node: HTMLElement, e: Event): void`
|
244 |
|
245 | ```jsx|pure
|
246 | import focus from 'rc-util/lib/Dom/focus';
|
247 | ```
|
248 |
|
249 | #### Dom/support
|
250 |
|
251 | > { animation: boolean | Object, transition: boolean | Object }
|
252 |
|
253 | A flag to tell whether current environment supports `animationend` or `transitionend`.
|
254 |
|
255 | ```jsx|pure
|
256 | import support from 'rc-util/lib/Dom/support';
|
257 | ```
|
258 |
|
259 | ### KeyCode
|
260 |
|
261 | > Enum
|
262 |
|
263 | Enum of KeyCode, please check the [definition](https://github.com/react-component/util/blob/master/src/KeyCode.ts) of it.
|
264 |
|
265 | ```jsx|pure
|
266 | import KeyCode from 'rc-util/lib/KeyCode';
|
267 | ```
|
268 |
|
269 | #### KeyCode.isTextModifyingKeyEvent
|
270 |
|
271 | > (e: Event): boolean
|
272 |
|
273 | Whether text and modified key is entered at the same time.
|
274 |
|
275 | #### KeyCode.isCharacterKey
|
276 |
|
277 | > (keyCode: KeyCode): boolean
|
278 |
|
279 | Whether character is entered.
|
280 |
|
281 | ### ScrollLocker
|
282 |
|
283 | > ScrollLocker<{lock: (options: {container: HTMLElement}) => void, unLock: () => void}>
|
284 |
|
285 | improve shake when page scroll bar hidden.
|
286 |
|
287 | `ScrollLocker` change body style, and add a class `ant-scrolling-effect` when called, so if you page look abnormal, please check this;
|
288 |
|
289 | ```js
|
290 | import ScrollLocker from 'rc-util/lib/Dom/scrollLocker';
|
291 |
|
292 | const scrollLocker = new ScrollLocker();
|
293 |
|
294 | // lock
|
295 | scrollLocker.lock()
|
296 |
|
297 | // unLock
|
298 | scrollLocker.unLock()
|
299 | ```
|
300 |
|
301 | ## License
|
302 |
|
303 | [MIT](/LICENSE)
|