@dvrd/dvr-controls 1.0.72 → 1.0.74
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
|
@@ -11,7 +11,7 @@ import AwesomeIcon from '../icon/awesomeIcon';
|
|
|
11
11
|
import delay from 'lodash.delay';
|
|
12
12
|
|
|
13
13
|
interface Props {
|
|
14
|
-
onChange: (selected: Array<string | number
|
|
14
|
+
onChange: (selected: Array<string | number>, submit: boolean) => MouseEventHandler;
|
|
15
15
|
onChangeSearch: ChangeFunction;
|
|
16
16
|
selected: Array<string | number>;
|
|
17
17
|
items: SelectItemShape[];
|
|
@@ -30,16 +30,28 @@ interface Props {
|
|
|
30
30
|
searchValue: string;
|
|
31
31
|
optionsContainerHeight: number | string;
|
|
32
32
|
placeholder?: string;
|
|
33
|
+
submitOnClose?: boolean;
|
|
33
34
|
}
|
|
34
35
|
|
|
35
36
|
const transitionDuration = 200; // ms
|
|
36
37
|
|
|
38
|
+
const useIsFirstRender = () => {
|
|
39
|
+
const isFirstRender = useRef(true);
|
|
40
|
+
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
isFirstRender.current = false;
|
|
43
|
+
}, []);
|
|
44
|
+
|
|
45
|
+
return isFirstRender.current;
|
|
46
|
+
};
|
|
47
|
+
|
|
37
48
|
export default function DvrdMultiSelect(props: Props) {
|
|
38
49
|
const {
|
|
39
50
|
error, disabled, selected, onChange, onChangeSearch, items, optionsContainerHeight, label, labelClassName,
|
|
40
51
|
selectOnly, valueClassName, searchValue, arrowClassName, itemClassName, itemsPosition, itemContainerClassName,
|
|
41
|
-
errorClassName, className, placeholder
|
|
52
|
+
errorClassName, className, placeholder, submitOnClose
|
|
42
53
|
} = props;
|
|
54
|
+
const isFirstRender = useIsFirstRender();
|
|
43
55
|
const [open, setOpen] = useState(false);
|
|
44
56
|
const [searchFocused, setSearchFocused] = useState(false);
|
|
45
57
|
const container = useRef<HTMLDivElement>(null);
|
|
@@ -60,8 +72,8 @@ export default function DvrdMultiSelect(props: Props) {
|
|
|
60
72
|
return function (evt: React.MouseEvent) {
|
|
61
73
|
stopPropagation(evt);
|
|
62
74
|
if (selected.includes(_value))
|
|
63
|
-
onChange(selected.filter((value: string | number) => value !== _value))(evt);
|
|
64
|
-
else onChange(selected.concat(_value))(evt);
|
|
75
|
+
onChange(selected.filter((value: string | number) => value !== _value), !submitOnClose)(evt);
|
|
76
|
+
else onChange(selected.concat(_value), !submitOnClose)(evt);
|
|
65
77
|
}
|
|
66
78
|
}
|
|
67
79
|
|
|
@@ -186,7 +198,10 @@ export default function DvrdMultiSelect(props: Props) {
|
|
|
186
198
|
|
|
187
199
|
useEffect(() => {
|
|
188
200
|
if (open) addClickListener();
|
|
189
|
-
else
|
|
201
|
+
else {
|
|
202
|
+
removeClickListener();
|
|
203
|
+
if (submitOnClose && !isFirstRender) onChange(selected, true);
|
|
204
|
+
}
|
|
190
205
|
return function () {
|
|
191
206
|
removeClickListener();
|
|
192
207
|
}
|
|
@@ -30,17 +30,14 @@ interface Props {
|
|
|
30
30
|
unControlled?: true;
|
|
31
31
|
multi?: true;
|
|
32
32
|
placeholder?: string;
|
|
33
|
+
submitOnClose?: boolean;
|
|
33
34
|
}
|
|
34
35
|
|
|
35
|
-
// interface State {
|
|
36
|
-
// searchValue: string;
|
|
37
|
-
// value: string | number | Array<string | number>;
|
|
38
|
-
// }
|
|
39
|
-
|
|
40
36
|
export default function DvrdSelectController(props: Props) {
|
|
41
37
|
const {
|
|
42
38
|
value, multi, unControlled, onChange, items, error, className, labelClassName, valueClassName,
|
|
43
|
-
itemContainerClassName, arrowClassName, errorClassName, itemClassName, label, disabled, placeholder
|
|
39
|
+
itemContainerClassName, arrowClassName, errorClassName, itemClassName, label, disabled, placeholder,
|
|
40
|
+
submitOnClose
|
|
44
41
|
} = props;
|
|
45
42
|
const [search, setSearch] = useState('');
|
|
46
43
|
const [internalValue, setInternalValue] = useState<ValueType>('');
|
|
@@ -48,12 +45,10 @@ export default function DvrdSelectController(props: Props) {
|
|
|
48
45
|
if (!search) return items;
|
|
49
46
|
return items.filter((item) => stringContains(item.label.toString(), search));
|
|
50
47
|
}, [items, search]);
|
|
51
|
-
const _value = useMemo(() => unControlled ? internalValue : value,
|
|
52
|
-
[unControlled, internalValue, value]);
|
|
53
48
|
|
|
54
|
-
function _onChange(value: ValueType) {
|
|
49
|
+
function _onChange(value: ValueType, submit: boolean = true) {
|
|
55
50
|
return function () {
|
|
56
|
-
onChange(value);
|
|
51
|
+
if (submit) onChange(value);
|
|
57
52
|
setInternalValue(value);
|
|
58
53
|
}
|
|
59
54
|
}
|
|
@@ -65,84 +60,30 @@ export default function DvrdSelectController(props: Props) {
|
|
|
65
60
|
useEffect(() => {
|
|
66
61
|
if (multi && !Array.isArray(value)) throw new TypeError('Value must be an array in multi mode');
|
|
67
62
|
else if (!multi && Array.isArray(value)) throw new TypeError('Value must be a string or number in single mode');
|
|
63
|
+
}, [value, multi]);
|
|
64
|
+
|
|
65
|
+
useEffect(() => {
|
|
68
66
|
if (!unControlled && value !== internalValue) setInternalValue(value);
|
|
69
|
-
}, [value,
|
|
67
|
+
}, [value, unControlled]);
|
|
70
68
|
|
|
71
69
|
const itemsPosition = props.itemsPosition ?? 'bottom';
|
|
72
70
|
const optionsContainerHeight = props.optionsContainerHeight ?? '15rem';
|
|
73
71
|
const selectOnly = props.selectOnly !== false;
|
|
74
72
|
if (multi) return (
|
|
75
73
|
<DvrdMultiSelect onChange={_onChange} onChangeSearch={onChangeSearch}
|
|
76
|
-
selected={
|
|
74
|
+
selected={internalValue as Array<string | number>} items={_items} itemsPosition={itemsPosition}
|
|
77
75
|
selectOnly={selectOnly} searchValue={search} optionsContainerHeight={optionsContainerHeight}
|
|
78
76
|
className={className} error={error} label={label} arrowClassName={arrowClassName}
|
|
79
|
-
errorClassName={errorClassName} itemClassName={itemClassName}
|
|
77
|
+
errorClassName={errorClassName} itemClassName={itemClassName} submitOnClose={submitOnClose}
|
|
80
78
|
itemContainerClassName={itemContainerClassName} valueClassName={valueClassName}
|
|
81
79
|
labelClassName={labelClassName} disabled={disabled} placeholder={placeholder}/>
|
|
82
80
|
);
|
|
83
81
|
return (
|
|
84
|
-
<DvrdSelect onChange={_onChange} onChangeSearch={onChangeSearch} value={
|
|
82
|
+
<DvrdSelect onChange={_onChange} onChangeSearch={onChangeSearch} value={internalValue as number | string}
|
|
85
83
|
items={_items} itemsPosition={itemsPosition} selectOnly={selectOnly} searchValue={search}
|
|
86
84
|
optionsContainerHeight={optionsContainerHeight} className={className} error={error} label={label}
|
|
87
85
|
arrowClassName={arrowClassName} errorClassName={errorClassName} itemClassName={itemClassName}
|
|
88
86
|
itemContainerClassName={itemContainerClassName} valueClassName={valueClassName}
|
|
89
87
|
labelClassName={labelClassName} disabled={disabled}/>
|
|
90
88
|
);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// class _DvrdSelectController extends PureComponent<Props, State> {
|
|
94
|
-
// private select: DvrdSelect;
|
|
95
|
-
//
|
|
96
|
-
// static defaultProps = {
|
|
97
|
-
// itemsPosition: 'bottom',
|
|
98
|
-
// selectOnly: true,
|
|
99
|
-
// optionsContainerHeight: '15rem',
|
|
100
|
-
// };
|
|
101
|
-
//
|
|
102
|
-
// state: State = {
|
|
103
|
-
// searchValue: '',
|
|
104
|
-
// value: this.props.value,
|
|
105
|
-
// };
|
|
106
|
-
//
|
|
107
|
-
// onChange = (value: string | number) => () => {
|
|
108
|
-
// this.props.onChange(value);
|
|
109
|
-
// this.setState({value});
|
|
110
|
-
// };
|
|
111
|
-
//
|
|
112
|
-
// onChangeSearch = (evt: React.ChangeEvent<HTMLInputElement>) => {
|
|
113
|
-
// const {value} = evt.target;
|
|
114
|
-
// this.setState({searchValue: value});
|
|
115
|
-
// };
|
|
116
|
-
//
|
|
117
|
-
// onOpen = (evt: React.MouseEvent) => {
|
|
118
|
-
// if (this.select) this.select.onClickElement(evt);
|
|
119
|
-
// };
|
|
120
|
-
//
|
|
121
|
-
// getItems = () => {
|
|
122
|
-
// const items = this.props.items.slice(), {searchValue} = this.state;
|
|
123
|
-
// if (!searchValue) return items;
|
|
124
|
-
// return items.filter((item) => stringContains(item.label.toString(), searchValue))
|
|
125
|
-
// }
|
|
126
|
-
//
|
|
127
|
-
// render = () => {
|
|
128
|
-
// const {
|
|
129
|
-
// arrowClassName, label, error, className, labelClassName, valueClassName, itemContainerClassName,
|
|
130
|
-
// itemClassName, errorClassName, disabled, itemsPosition, selectOnly, optionsContainerHeight, unControlled,
|
|
131
|
-
// multi
|
|
132
|
-
// } = this.props, {searchValue} = this.state;
|
|
133
|
-
// const value = !!unControlled ? this.state.value : this.props.value;
|
|
134
|
-
// if (!multi && Array.isArray(value)) throw new TypeError('Value cannot be an array in single mode');
|
|
135
|
-
// else if (multi && !Array.isArray(value)) throw new TypeError('Value must be an array in multi mode');
|
|
136
|
-
// return (
|
|
137
|
-
// <DvrdSelect onChange={this.onChange} value={value as string | number} items={this.getItems()}
|
|
138
|
-
// disabled={disabled}
|
|
139
|
-
// errorClassName={errorClassName} itemClassName={itemClassName}
|
|
140
|
-
// itemContainerClassName={itemContainerClassName} valueClassName={valueClassName}
|
|
141
|
-
// labelClassName={labelClassName} className={className} error={error} label={label}
|
|
142
|
-
// arrowClassName={arrowClassName} itemsPosition={itemsPosition} ref={(ref: DvrdSelect) => {
|
|
143
|
-
// this.select = ref
|
|
144
|
-
// }} searchValue={searchValue} onChangeSearch={this.onChangeSearch} selectOnly={selectOnly}
|
|
145
|
-
// optionsContainerHeight={optionsContainerHeight}/>
|
|
146
|
-
// );
|
|
147
|
-
// }
|
|
148
|
-
// };
|
|
89
|
+
}
|