-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathActivatorTextInput.tsx
More file actions
112 lines (103 loc) · 2.96 KB
/
ActivatorTextInput.tsx
File metadata and controls
112 lines (103 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import React, { ChangeEvent, useEffect, useRef, useState } from 'react';
import { Icon } from '../../../Icon';
import { Option } from '../../../Menu';
import { TextInputProps } from '../../../TextInput';
import {
SearchableSelectInput,
SearchableSelectState,
} from '../SearchableSelectInput/SearchableSelectInput';
import { StyledTextInput } from './ActivatorStyle';
export interface ActivatorTextInputProps
extends Omit<TextInputProps, 'onChange' | 'onSelect'> {
allowMultiple?: boolean;
disabled?: boolean;
hasError?: boolean;
onChange?(value: string): void;
onSelect?({ value }: { value: string }): void;
selectedValues?: string[];
width?: string;
inputValue?: string;
updateInputValue?: (newValue: string) => void;
searchableSelectState?: SearchableSelectState;
updateSearchableSelectState?: (newState: SearchableSelectState) => void;
options?: Option[];
updateMenuOptions?: (newState: Option[]) => void;
}
export const ActivatorTextInput = ({
allowMultiple,
disabled = false,
hasError = false,
onChange,
onFocus,
onSelect,
prefix = <Icon name="ri-search" />,
selectedValues,
width,
inputValue,
updateInputValue,
options,
updateMenuOptions,
...props
}: ActivatorTextInputProps) => {
const activatorRef = useRef(null);
const [hasSelectedValues, setHasSelectedValues] = useState(false);
const filterOptions = (str: string) => {
if (str === '') {
updateMenuOptions(options);
return options;
}
const filterRegex = new RegExp(str, 'i');
const filterOptions = options.filter((option: Option) =>
(option.label as string).match(filterRegex)
);
return filterOptions;
};
const handleChange = ({ value }: { value: string }) => {
if (onChange) {
onChange(value);
const filteredOptions = filterOptions(value);
updateMenuOptions(filteredOptions);
return;
}
};
useEffect(() => {
setHasSelectedValues(selectedValues.filter(v => v != '').length > 0);
}, [selectedValues]);
return (
<>
{allowMultiple ? (
<StyledTextInput
{...props}
className="select-input"
ref={activatorRef}
prefix={prefix}
onChange={(value: ChangeEvent<HTMLInputElement>) =>
handleChange({ value })
}
onFocus={onFocus}
error={hasError}
disabled={disabled}
width={width}
/>
) : (
<SearchableSelectInput
{...props}
ref={activatorRef}
prefix={prefix}
filterOptions={filterOptions}
onFocus={onFocus}
onInputChange={onChange}
onSelect={onSelect}
error={hasError}
disabled={disabled}
selectedValue={hasSelectedValues ? selectedValues[0] : null}
width={width}
inputValue={inputValue}
updateInputValue={updateInputValue}
options={options}
updateMenuOptions={updateMenuOptions}
/>
)}
</>
);
};