|
8 | 8 | import PropTypes from 'prop-types'; |
9 | 9 | import React from 'react'; |
10 | 10 | import cx from 'classnames'; |
11 | | -import { PrefixContext } from '../../internal/usePrefix'; |
| 11 | +import { usePrefix } from '../../internal/usePrefix'; |
12 | 12 |
|
13 | | -type ToggleSkeletonProps = { |
14 | | - 'aria-label': string; |
15 | | - |
16 | | - /** |
17 | | - * Specify an optional className to add to the form item wrapper. |
18 | | - */ |
| 13 | +interface ToggleSkeletonProps { |
| 14 | + 'aria-label'?: string; |
19 | 15 | className?: string; |
20 | | - |
21 | | - /** |
22 | | - * Provide an id that unique represents the underlying `<input>` |
23 | | - */ |
24 | | - id?: string; |
25 | | - |
26 | | - /** |
27 | | - * Provide the text that will be read by a screen reader when visiting this |
28 | | - * control |
29 | | - * `aria-label` is always required but will be null if `labelText` is also |
30 | | - * provided |
31 | | - */ |
32 | | - labelText?: string; |
33 | | - |
34 | | - /** |
35 | | - * Specify the size of the Toggle. Currently only supports 'sm' or 'md' (default) |
36 | | - */ |
37 | | - size?: 'sm' | 'md'; |
38 | | -} & React.HTMLAttributes<HTMLDivElement>; |
39 | | - |
40 | | -class ToggleSkeleton extends React.Component<ToggleSkeletonProps> { |
41 | | - static propTypes = { |
42 | | - ['aria-label']: PropTypes.string.isRequired, |
43 | | - |
44 | | - /** |
45 | | - * Specify an optional className to add to the form item wrapper. |
46 | | - */ |
47 | | - className: PropTypes.string, |
48 | | - /** |
49 | | - * Provide an id that unique represents the underlying `<input>` |
50 | | - */ |
51 | | - id: PropTypes.string, |
52 | | - |
53 | | - /** |
54 | | - * Provide the text that will be read by a screen reader when visiting this |
55 | | - * control |
56 | | - * `aria-label` is always required but will be undefined if `labelText` is also |
57 | | - * provided |
58 | | - */ |
59 | | - labelText: PropTypes.string, |
60 | | - |
61 | | - /** |
62 | | - * Specify the size of the Toggle. Currently only supports 'sm' or 'md' (default) |
63 | | - */ |
64 | | - size: PropTypes.oneOf(['sm', 'md']), |
65 | | - }; |
66 | | - |
67 | | - static defaultProps: Partial<ToggleSkeletonProps> = { |
68 | | - ['aria-label']: 'Toggle is loading', |
69 | | - size: 'md', |
70 | | - }; |
71 | | - |
72 | | - render() { |
73 | | - const { id, labelText, className, size, ...rest } = this.props; |
74 | | - |
75 | | - return ( |
76 | | - <PrefixContext.Consumer> |
77 | | - {(prefix) => { |
78 | | - const toggleInputClassNames = cx( |
79 | | - `${prefix}--toggle ${prefix}--skeleton`, |
80 | | - { |
81 | | - [`${prefix}--toggle-input--small`]: size === 'sm', |
82 | | - } |
83 | | - ); |
84 | | - |
85 | | - return ( |
86 | | - <div className={cx(`${prefix}--form-item`, className)} {...rest}> |
87 | | - <input |
88 | | - type="checkbox" |
89 | | - id={id} |
90 | | - className={toggleInputClassNames} |
91 | | - /> |
92 | | - |
93 | | - <label |
94 | | - className={`${prefix}--toggle-input__label`} |
95 | | - htmlFor={id} |
96 | | - aria-label={labelText ? undefined : this.props['aria-label']}> |
97 | | - {labelText ? <div>{labelText}</div> : null} |
98 | | - <span className={`${prefix}--toggle__switch`}> |
99 | | - <span className={`${prefix}--toggle__text--left`} /> |
100 | | - <span className={`${prefix}--toggle__appearance`} /> |
101 | | - <span className={`${prefix}--toggle__text--right`} /> |
102 | | - </span> |
103 | | - </label> |
104 | | - </div> |
105 | | - ); |
106 | | - }} |
107 | | - </PrefixContext.Consumer> |
108 | | - ); |
109 | | - } |
110 | 16 | } |
111 | 17 |
|
| 18 | +const ToggleSkeleton: React.FC<ToggleSkeletonProps> = ({ |
| 19 | + 'aria-label': ariaLabel = 'Toggle is loading', |
| 20 | + className, |
| 21 | + ...rest |
| 22 | +}) => { |
| 23 | + const prefix = usePrefix(); |
| 24 | + |
| 25 | + const skeletonClassNames = cx( |
| 26 | + `${prefix}--toggle ${prefix}--toggle--skeleton`, |
| 27 | + className |
| 28 | + ); |
| 29 | + |
| 30 | + return ( |
| 31 | + <div className={skeletonClassNames} {...rest} aria-label={ariaLabel}> |
| 32 | + <div className={`${prefix}--toggle__skeleton-circle`} /> |
| 33 | + <div className={`${prefix}--toggle__skeleton-rectangle`} /> |
| 34 | + </div> |
| 35 | + ); |
| 36 | +}; |
| 37 | + |
| 38 | +ToggleSkeleton.propTypes = { |
| 39 | + 'aria-label': PropTypes.string, |
| 40 | + className: PropTypes.string, |
| 41 | +}; |
| 42 | + |
112 | 43 | export default ToggleSkeleton; |
113 | 44 | export { ToggleSkeleton }; |
0 commit comments