@gm-pc/react 1.17.0-beta.0 → 1.17.0-beta.3

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": "@gm-pc/react",
3
- "version": "1.17.0-beta.0",
3
+ "version": "1.17.0-beta.3",
4
4
  "description": "观麦前端基础组件库",
5
5
  "author": "liyatang <liyatang@qq.com>",
6
6
  "homepage": "https://github.com/gmfe/gm-pc#readme",
@@ -24,7 +24,7 @@
24
24
  "dependencies": {
25
25
  "@gm-common/hooks": "^2.10.0",
26
26
  "@gm-common/tool": "^2.10.0",
27
- "@gm-pc/locales": "^1.17.0-beta.0",
27
+ "@gm-pc/locales": "^1.17.0-beta.3",
28
28
  "big.js": "^6.0.1",
29
29
  "classnames": "^2.2.5",
30
30
  "lodash": "^4.17.19",
@@ -48,5 +48,5 @@
48
48
  "react-router-dom": "^5.2.0",
49
49
  "react-window": "^1.8.5"
50
50
  },
51
- "gitHead": "4ded82c728d9153ce0f837ad39777ac461a57d1f"
51
+ "gitHead": "c6262effbcbb9685808340368095de89903bcfde"
52
52
  }
@@ -0,0 +1,195 @@
1
+ import React, {
2
+ useRef,
3
+ useState,
4
+ useMemo,
5
+ useEffect,
6
+ InputHTMLAttributes,
7
+ CSSProperties,
8
+ ReactNode,
9
+ forwardRef,
10
+ useImperativeHandle,
11
+ } from 'react'
12
+ import { Input } from '../input'
13
+ import { Popover } from '../popover'
14
+ import { List } from '../list'
15
+
16
+ import _ from 'lodash'
17
+ import classNames from 'classnames'
18
+ import { ListDataItem } from '../../types'
19
+
20
+ export interface AutoCompleteOption {
21
+ value: string
22
+ }
23
+
24
+ type InputProps = InputHTMLAttributes<HTMLInputElement>
25
+
26
+ export interface AutoCompleteProps extends Omit<InputProps, 'value' | 'onChange'> {
27
+ /** input value */
28
+ value?: string
29
+ /** 填充文本列表 */
30
+ options?: AutoCompleteOption[]
31
+ /** 自定义 options 外层容器类名 */
32
+ optionsWrapClassName?: string
33
+ /** 自定义 options 外层容器行内样式 */
34
+ optionsWrapStyle?: CSSProperties
35
+ /** 自定义 option 渲染 */
36
+ renderOption?: (value: AutoCompleteOption, index: number) => ReactNode
37
+ /** 在 options 前追加内容 */
38
+ addonOptionsBefore?: () => ReactNode
39
+ /** 在 options 后追加内容 */
40
+ addonOptionsAfter?: () => ReactNode
41
+ /** input onChange 触发、点击 option 时触发、键盘选择 option 并回车时触发 */
42
+ onChange?: (value: string) => void
43
+ }
44
+
45
+ export interface AutoCompleteRef {
46
+ /** input DOM 节点 */
47
+ input?: HTMLInputElement | null
48
+ /** 控制弹出层显示 */
49
+ triggerPopover: (value: boolean) => void
50
+ }
51
+
52
+ const preventDefault = (e: React.MouseEvent<HTMLElement, MouseEvent>) => {
53
+ e.preventDefault()
54
+ }
55
+
56
+ const LIST_ITEM_PROPS: React.HTMLAttributes<HTMLDivElement> = {
57
+ onPointerDown: preventDefault,
58
+ onMouseDown: preventDefault,
59
+ onClick: preventDefault,
60
+ }
61
+
62
+ const listItemProps = () => LIST_ITEM_PROPS
63
+
64
+ const AutoComplete = forwardRef<AutoCompleteRef, AutoCompleteProps>((props, ref) => {
65
+ const {
66
+ value,
67
+ options: externalOptions,
68
+ optionsWrapClassName,
69
+ optionsWrapStyle,
70
+ onChange,
71
+ onKeyDown,
72
+ onBlur,
73
+ renderOption,
74
+ addonOptionsAfter,
75
+ addonOptionsBefore,
76
+ ...rest
77
+ } = props
78
+
79
+ const [willActiveIndex, setWillActiveIndex] = useState(0)
80
+ const popoverNode = useRef<Popover | null>(null)
81
+ const popoverVisible = useRef(false)
82
+ const inputNode = useRef<HTMLInputElement | null>(null)
83
+
84
+ const triggerPopover = (value: boolean) => {
85
+ popoverNode.current && popoverNode.current.apiDoSetActive(value)
86
+ }
87
+
88
+ const options = useMemo(() => {
89
+ return _.map(externalOptions, (item) => ({ text: item.value, value: item.value }))
90
+ }, [externalOptions])
91
+
92
+ useEffect(() => {
93
+ if (_.isNil(value) || options.length <= 0) return
94
+ setWillActiveIndex(() => {
95
+ return options.findIndex((item) => item.value === value)
96
+ })
97
+ }, [value, options])
98
+
99
+ const handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
100
+ if (!popoverVisible.current) {
101
+ onKeyDown && onKeyDown(event)
102
+ return
103
+ }
104
+ if (event.key === 'Enter') {
105
+ onChange && onChange(options[willActiveIndex].value)
106
+ triggerPopover(false)
107
+ }
108
+ if (event.key === 'Escape') {
109
+ triggerPopover(false)
110
+ }
111
+ if (event.key !== 'ArrowUp' && event.key !== 'ArrowDown') {
112
+ onKeyDown && onKeyDown(event)
113
+ return
114
+ }
115
+ let index = willActiveIndex
116
+ if (event.key === 'ArrowUp') {
117
+ index--
118
+ } else if (event.key === 'ArrowDown') {
119
+ index++
120
+ }
121
+ if (index < 0) {
122
+ index = options.length - 1
123
+ } else if (index > options.length - 1) {
124
+ index = 0
125
+ }
126
+ setWillActiveIndex(index)
127
+ }
128
+
129
+ const renderItem = useMemo(() => {
130
+ if (renderOption) {
131
+ return (value: ListDataItem<string>, index: number) => {
132
+ return renderOption({ value: value.value }, index)
133
+ }
134
+ }
135
+ return undefined
136
+ }, [renderOption])
137
+
138
+ useImperativeHandle(ref, () => {
139
+ return {
140
+ input: inputNode.current,
141
+ triggerPopover,
142
+ }
143
+ })
144
+
145
+ return (
146
+ <Popover
147
+ ref={popoverNode}
148
+ type='click'
149
+ disabled={options.length === 0}
150
+ onVisibleChange={(visible) => {
151
+ popoverVisible.current = visible
152
+ }}
153
+ popup={
154
+ <>
155
+ {addonOptionsBefore && <div {...LIST_ITEM_PROPS}>{addonOptionsBefore()}</div>}
156
+ <List
157
+ data={options}
158
+ selected={value}
159
+ onSelect={(val) => {
160
+ onChange && onChange(val as string)
161
+ triggerPopover(false)
162
+ inputNode.current && inputNode.current.focus()
163
+ }}
164
+ willActiveIndex={willActiveIndex}
165
+ className={classNames('gm-border-0', optionsWrapClassName)}
166
+ renderItem={renderItem}
167
+ style={{ maxHeight: 250, ...optionsWrapStyle }}
168
+ getItemProps={listItemProps}
169
+ />
170
+ {addonOptionsAfter && <div {...LIST_ITEM_PROPS}>{addonOptionsAfter()}</div>}
171
+ </>
172
+ }
173
+ >
174
+ <Input
175
+ {...rest}
176
+ ref={inputNode}
177
+ value={value}
178
+ onChange={(e) => {
179
+ const val = e.target.value
180
+ if (val.length === 0) {
181
+ triggerPopover(true)
182
+ }
183
+ onChange && onChange(val)
184
+ }}
185
+ onBlur={(e) => {
186
+ triggerPopover(false)
187
+ onBlur && onBlur(e)
188
+ }}
189
+ onKeyDown={handleKeyDown}
190
+ />
191
+ </Popover>
192
+ )
193
+ })
194
+
195
+ export default AutoComplete
@@ -0,0 +1,6 @@
1
+ export { default as AutoComplete } from './auto_complete'
2
+ export type {
3
+ AutoCompleteProps,
4
+ AutoCompleteOption,
5
+ AutoCompleteRef,
6
+ } from './auto_complete'
@@ -0,0 +1,37 @@
1
+ import React, { useState } from 'react'
2
+ import AutoComplete, { AutoCompleteOption } from './auto_complete'
3
+
4
+ const tips: AutoCompleteOption[] = [
5
+ { value: '自动填充文本' },
6
+ { value: '自动填充文本2' },
7
+ { value: '自动填充文本3' },
8
+ ]
9
+
10
+ export const ComAutoComplete = () => {
11
+ const [value, setValue] = useState('')
12
+
13
+ return (
14
+ <div style={{ width: 300 }}>
15
+ <AutoComplete value={value} options={tips} onChange={setValue} />
16
+ <br />
17
+ <br />
18
+ <br />
19
+ <AutoComplete
20
+ value={value}
21
+ options={tips}
22
+ onChange={setValue}
23
+ addonOptionsBefore={() => {
24
+ return (
25
+ <span style={{ display: 'block', padding: '4px 10px', color: '#888' }}>
26
+ 最近几条输入记录:
27
+ </span>
28
+ )
29
+ }}
30
+ />
31
+ </div>
32
+ )
33
+ }
34
+
35
+ export default {
36
+ title: '表单/AutoComplete',
37
+ }
@@ -105,6 +105,7 @@ class Popover extends Component<PopoverProps, PopoverState> {
105
105
  if (this._isUnmounted) {
106
106
  return
107
107
  }
108
+ const { onVisibleChange } = this.props
108
109
  if (active) {
109
110
  // eslint-disable-next-line react/no-find-dom-node
110
111
  const dom = findDOMNode(this) as HTMLElement
@@ -116,6 +117,7 @@ class Popover extends Component<PopoverProps, PopoverState> {
116
117
  width: dom.offsetWidth,
117
118
  }
118
119
  }
120
+ onVisibleChange && onVisibleChange(active)
119
121
  this.setState({ active })
120
122
  }
121
123
 
@@ -19,6 +19,7 @@ interface PopoverProps {
19
19
  predictingHeight?: number
20
20
  className?: string
21
21
  style?: CSSProperties
22
+ onVisibleChange?: (value: boolean) => void
22
23
  }
23
24
 
24
25
  export type { Popup, PopoverTrigger, PopoverProps }
package/src/index.ts CHANGED
@@ -2,6 +2,7 @@ export * from './types'
2
2
  export { default as EVENT_TYPE } from './event_type'
3
3
  export { default as Validator } from './validator'
4
4
  export * from './component/affix'
5
+ export * from './component/auto_complete'
5
6
  export * from './component/box'
6
7
  export * from './component/button'
7
8
  export * from './component/calendar'