@yorkjs/hive-ui 2.0.3 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yorkjs/hive-ui",
3
- "version": "2.0.3",
3
+ "version": "2.0.4",
4
4
  "description": "Hive UI - Taro React component library",
5
5
  "main": "src/index.ts",
6
6
  "module": "src/index.ts",
@@ -0,0 +1,24 @@
1
+ @import '../../styles/mixin.styl'
2
+
3
+ .switch-component
4
+ &.is-readonly
5
+ opacity 0.8
6
+ cursor default
7
+ &.is-disabled
8
+ cursor not-allowed
9
+
10
+ :global
11
+ .weui-switch, .wx-switch-input
12
+ border-color var(--switch-inactive-color) !important
13
+ background-color var(--switch-inactive-color) !important
14
+
15
+ &:before
16
+ background-color var(--switch-inactive-color) !important
17
+ border-color var(--switch-inactive-color) !important
18
+
19
+ &:checked, &.wx-switch-input-checked
20
+ border-color var(--switch-active-color) !important
21
+ background-color var(--switch-active-color) !important
22
+ &:before
23
+ background-color var(--switch-active-color) !important
24
+ border-color var(--switch-active-color) !important
@@ -0,0 +1,80 @@
1
+ import React, { useMemo } from 'react'
2
+ import { BaseEventOrig, Switch as TaroSwitch } from '@tarojs/components'
3
+ import { formatClassNames } from '../../util/function'
4
+
5
+ import { useTheme } from '../../config'
6
+
7
+ import styles from './index.module.styl'
8
+
9
+ export interface SwitchProps {
10
+ /** 是否选中 */
11
+ checked?: boolean
12
+ /** 改变事件 */
13
+ onChange?: (checked: boolean) => void
14
+ /** 是否禁用 */
15
+ disabled?: boolean
16
+ /** 只读状态 */
17
+ readOnly?: boolean
18
+ /** 自定义类名 */
19
+ className?: string
20
+ /** 选中时的背景颜色 (不传则使用主题色) */
21
+ color?: string
22
+ }
23
+
24
+ const Switch: React.FC<SwitchProps> = (props) => {
25
+ const {
26
+ checked = false,
27
+ onChange,
28
+ disabled = false,
29
+ readOnly = false,
30
+ className,
31
+ color,
32
+ } = props
33
+
34
+ const { isDark, themeSelect } = useTheme()
35
+
36
+ const colors = useMemo(() => {
37
+ let activeTrack = color || 'var(--primary)'
38
+ let inactiveTrack = themeSelect('#CCCCCC', 'var(--containerInner)')
39
+
40
+ if (disabled) {
41
+ activeTrack = '#ffc097'
42
+ inactiveTrack = '#E6E6E6'
43
+ }
44
+
45
+ return {
46
+ activeTrack,
47
+ inactiveTrack,
48
+ }
49
+ }, [color, disabled, themeSelect])
50
+
51
+ const handleChange = (e: BaseEventOrig<{ value: boolean }>) => {
52
+ if (disabled || readOnly) return
53
+ onChange?.(e.detail.value)
54
+ }
55
+
56
+ const dynamicStyle = {
57
+ '--switch-active-color': colors.activeTrack,
58
+ '--switch-inactive-color': colors.inactiveTrack,
59
+ } as React.CSSProperties
60
+
61
+ return (
62
+ <TaroSwitch
63
+ checked={checked}
64
+ disabled={disabled || readOnly}
65
+ color={colors.activeTrack}
66
+ onChange={handleChange}
67
+ style={dynamicStyle}
68
+ className={formatClassNames(
69
+ styles['switch-component'],
70
+ {
71
+ [styles['is-readonly']]: readOnly,
72
+ [styles['is-disabled']]: disabled,
73
+ },
74
+ className
75
+ )}
76
+ />
77
+ )
78
+ }
79
+
80
+ export default React.memo(Switch)
package/src/index.ts CHANGED
@@ -10,6 +10,7 @@ export { default as DisplayCard } from './components/DisplayCard'
10
10
  export { default as Card } from './components/Card'
11
11
  export { default as RemoteImage } from './components/RemoteImage'
12
12
  export { default as Alert } from './components/Alert'
13
+ export { default as Switch } from './components/Switch'
13
14
 
14
15
  export { default as FlowItem } from './item/FlowItem'
15
16
  export { default as ProductItem, setProductItemConfig } from './item/ProductItem'