forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchInputDemo.tsx
More file actions
108 lines (99 loc) · 2.78 KB
/
Copy pathSearchInputDemo.tsx
File metadata and controls
108 lines (99 loc) · 2.78 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
import * as React from 'react';
import { SearchInput, SearchInputProps, Button } from '@patternfly/react-core';
interface SearchInputState {
value: string;
resultsCount: number;
currentResult: number;
}
export class SearchInputDemo extends React.Component<SearchInputProps, SearchInputState> {
static displayName = 'SearchInputDemo';
inputRef: React.RefObject<HTMLInputElement>;
constructor(props: SearchInputProps) {
super(props);
this.inputRef = React.createRef();
this.state = {
value: '',
resultsCount: 0,
currentResult: 1
};
}
onChange = (value: string) => {
this.setState({
value,
resultsCount: 3
});
};
onClear = () => {
this.setState({
value: '',
resultsCount: 0,
currentResult: 1
});
};
onNext = () => {
this.setState((prevState) => {
const newCurrentResult = prevState.currentResult + 1;
return {
currentResult: newCurrentResult <= prevState.resultsCount ? newCurrentResult : prevState.resultsCount
};
});
};
onPrevious = () => {
this.setState((prevState) => {
const newCurrentResult = prevState.currentResult - 1;
return {
currentResult: newCurrentResult > 0 ? newCurrentResult : 1
};
});
};
onInputFocus = () => {
if (this.inputRef && this.inputRef.current) {
this.inputRef.current.focus();
}
};
onSearch = (value: string) => {
this.setState({
value
});
};
render() {
return (
<>
<SearchInput
id="enabled-search"
ref={this.inputRef}
attributes={[
{ attr: 'username', display: 'Username' },
{ attr: 'firstname', display: 'First name' }
]}
placeholder="Find by name"
advancedSearchDelimiter=":"
value={this.state.value}
onChange={(_event, value) => this.onChange(value)}
onSearch={(_event, value) => this.onSearch(value)}
onClear={this.onClear}
resultsCount={`${this.state.currentResult} / ${this.state.resultsCount}`}
onNextClick={this.onNext}
onPreviousClick={this.onPrevious}
/>
<Button id="focus_button" onClick={this.onInputFocus}>
Focus on search
</Button>
<SearchInput
id="disabled-search"
attributes={[
{ attr: 'username', display: 'Username' },
{ attr: 'firstname', display: 'First name' }
]}
placeholder="Find by name"
advancedSearchDelimiter=":"
onChange={(_event, value) => this.onChange(value)}
onSearch={(_event, value) => this.onSearch(value)}
onClear={this.onClear}
isDisabled
/>
<SearchInput id="hinted-search" hint="hint" />
</>
);
}
}