@devtron-labs/devtron-fe-common-lib 0.0.53 → 0.0.54-beta-1

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": "@devtron-labs/devtron-fe-common-lib",
3
- "version": "0.0.53",
3
+ "version": "0.0.54-beta-1",
4
4
  "description": "Supporting common component library",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -0,0 +1,6 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
2
+ <g fill="none" fill-rule="evenodd">
3
+ <path d="M0 0h24v24H0z"/>
4
+ <path fill="#959BA1" d="M10 2a8 8 0 0 1 6.32 12.905l5.044 5.045a1 1 0 1 1-1.414 1.414l-5.045-5.043A8 8 0 1 1 10 2zm0 2a6 6 0 1 0 0 12 6 6 0 0 0 0-12z"/>
5
+ </g>
6
+ </svg>
@@ -28,7 +28,8 @@ export function CustomInput({
28
28
  defaultValue,
29
29
  onKeyDown,
30
30
  required,
31
- additionalErrorInfo
31
+ additionalErrorInfo,
32
+ inputWrapperClassName = ''
32
33
  }: CustomInputProps) {
33
34
 
34
35
  function handleError(error: any): any[] {
@@ -92,7 +93,7 @@ export function CustomInput({
92
93
  }
93
94
 
94
95
  return (
95
- <div className="flex column left top">
96
+ <div className={`flex column left top ${inputWrapperClassName}`}>
96
97
  {renderInputLabel()}
97
98
  <input
98
99
  data-testid={dataTestid}
@@ -27,4 +27,5 @@ export interface CustomInputProps {
27
27
  onKeyDown?: (e) => void
28
28
  required?: boolean
29
29
  additionalErrorInfo?: React.ReactNode
30
+ inputWrapperClassName?: string
30
31
  }
@@ -4,6 +4,8 @@ import { ReactComponent as ICClear } from '../../Assets/Icon/ic-error-cross.svg'
4
4
  import { DebouncedSearchProps } from './Types'
5
5
 
6
6
  /**
7
+ * @deprecated Use `SearchBar` component instead.
8
+ *
7
9
  * @param onSearch - Callback function to be called on search
8
10
  * @param Icon - (Optional) Icon to be shown before the input
9
11
  * @param iconClass - (Optional) Class for the icon
@@ -559,3 +559,18 @@ export function getLockedJSON(json, jsonPathArray: string[]) {
559
559
  return resultJson['$']
560
560
  }
561
561
 
562
+ /**
563
+ * Returns a debounced variant of the function
564
+ */
565
+ export const debounce = (func, timeout = 500) => {
566
+ let timer
567
+
568
+ return function (this, ...args) {
569
+ const context = this
570
+ if (timer) clearTimeout(timer)
571
+ timer = setTimeout(() => {
572
+ timer = null
573
+ func.apply(context, args)
574
+ }, timeout)
575
+ }
576
+ }
@@ -0,0 +1,120 @@
1
+ import { ChangeEvent, useCallback, useRef, useState, KeyboardEvent } from 'react'
2
+ import { ReactComponent as Search } from '../../Assets/Icon/ic-search.svg'
3
+ import { ReactComponent as Clear } from '../../Assets/Icon/ic-error-cross.svg'
4
+ import { SearchBarProps } from './types'
5
+ import './searchBar.scss'
6
+ import { debounce } from '../Helper'
7
+
8
+ /**
9
+ * Generic search input component with support for enter based and debounced search
10
+ * Note: The component is uncontrolled
11
+ *
12
+ * @example Usage with default debounced search
13
+ * ```tsx
14
+ * <SearchBar handleSearchChange={triggerSearchApi} shouldDebounce />
15
+ * ```
16
+ *
17
+ * @example Initial search text
18
+ * ```tsx
19
+ * <SearchBar initialSearchText="Keyword" />
20
+ * ```
21
+ *
22
+ * @example Custom width
23
+ * ```tsx
24
+ * <SearchBar containerClassName="w-300" />
25
+ * ```
26
+ *
27
+ * @example Trigger search on enter key press
28
+ * ```tsx
29
+ * <SearchBar handleEnter={triggerSearchApi} />
30
+ * ```
31
+ *
32
+ * @example Custom timeout for debounced search
33
+ * ```tsx
34
+ * <SearchBar handleSearchChange={triggerSearchApi} shouldDebounce debounceTimeout={500} />
35
+ * ```
36
+ *
37
+ * @example Placeholder for the search
38
+ * ```tsx
39
+ * <SearchBar inputProps={{ placeholder: 'Enter search text' }} />
40
+ * ```
41
+ */
42
+ const SearchBar = ({
43
+ initialSearchText = '',
44
+ handleSearchChange = () => {},
45
+ handleEnter = () => {},
46
+ inputProps = {},
47
+ containerClassName,
48
+ shouldDebounce = false,
49
+ debounceTimeout = 300,
50
+ }: SearchBarProps) => {
51
+ const [showClearButton, setShowClearButton] = useState(!!initialSearchText)
52
+ const inputRef = useRef<HTMLInputElement>()
53
+ const debouncedSearchChange = useCallback(debounce(handleSearchChange, debounceTimeout), [
54
+ handleSearchChange,
55
+ debounceTimeout,
56
+ ])
57
+
58
+ const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
59
+ const { value } = e.target
60
+ setShowClearButton(!!value)
61
+ if (shouldDebounce) {
62
+ debouncedSearchChange(value)
63
+ } else {
64
+ handleSearchChange(value)
65
+ }
66
+ }
67
+
68
+ const _applySearch = (value: string) => {
69
+ handleSearchChange(value)
70
+ handleEnter(value)
71
+ }
72
+
73
+ const handleKeyDown = (e: KeyboardEvent<HTMLInputElement>) => {
74
+ const { key } = e
75
+
76
+ if (key === 'Enter') {
77
+ const inputTarget = e.target as HTMLInputElement
78
+ const value = inputTarget.value.trim()
79
+ _applySearch(value)
80
+ }
81
+ }
82
+
83
+ const clearSearch = () => {
84
+ inputRef.current.value = ''
85
+ _applySearch('')
86
+ }
87
+
88
+ return (
89
+ <div className={containerClassName}>
90
+ <div className="search-bar bcn-0 dc__block w-100 min-w-200 dc__position-rel en-2 bw-1 br-4 h-32">
91
+ <Search className="search-bar__icon dc__position-abs icon-color-n6 icon-dim-18" />
92
+ <input
93
+ placeholder="Search"
94
+ data-testid="search-bar"
95
+ type="text"
96
+ {...inputProps}
97
+ defaultValue={initialSearchText}
98
+ className={`search-bar__input bcn-0 dc__position-abs w-100 h-100 br-4 dc__no-border pt-6 pr-10 pb-6 pl-30 fs-13 lh-20 fw-4 cn-9 placeholder-cn5 ${
99
+ showClearButton ? 'pr-30' : 'pr-10'
100
+ }`}
101
+ onChange={handleChange}
102
+ onKeyDown={handleKeyDown}
103
+ ref={inputRef}
104
+ />
105
+ {showClearButton && (
106
+ <button
107
+ className="flex search-bar__clear-button dc__position-abs dc__transparent mt-0 mb-0 mr-5 ml-5"
108
+ type="button"
109
+ onClick={clearSearch}
110
+ aria-label="Clear search"
111
+ >
112
+ <Clear className="icon-dim-18 icon-n4 dc__vertical-align-middle" />
113
+ </button>
114
+ )}
115
+ </div>
116
+ </div>
117
+ )
118
+ }
119
+
120
+ export default SearchBar
@@ -0,0 +1,2 @@
1
+ export { default as SearchBar } from './SearchBar.component'
2
+ export type { SearchBarProps } from './types'
@@ -0,0 +1,31 @@
1
+ .search-bar {
2
+
3
+ &:focus,
4
+ &:focus-within {
5
+ outline: none;
6
+ border: solid 1px var(--B500);
7
+ }
8
+
9
+ &__input {
10
+
11
+ &:focus,
12
+ &:focus-visible {
13
+ border: none;
14
+ outline: none;
15
+ }
16
+ }
17
+
18
+ &__icon {
19
+ z-index: 1;
20
+ top: 50%;
21
+ left: 8px;
22
+ transform: translateY(-50%);
23
+ }
24
+
25
+ &__clear-button {
26
+ right: 0;
27
+ top: 50%;
28
+ transform: translateY(-50%);
29
+ z-index: 1;
30
+ }
31
+ }
@@ -0,0 +1,36 @@
1
+ import { InputHTMLAttributes } from 'react'
2
+
3
+ export interface SearchBarProps {
4
+ /**
5
+ * Initial search text
6
+ *
7
+ * @default ''
8
+ */
9
+ initialSearchText?: string
10
+ /**
11
+ * Search handler for the search input
12
+ */
13
+ handleSearchChange?: (searchText: string) => void
14
+ /**
15
+ * Enter event handler for the search input
16
+ */
17
+ handleEnter?: (searchText: string) => void
18
+ /**
19
+ * Input props for the search input
20
+ */
21
+ inputProps?: InputHTMLAttributes<HTMLInputElement>
22
+ /**
23
+ * Class name for the container; can be used for handling width
24
+ */
25
+ containerClassName?: string
26
+ /**
27
+ * If true, the change handler would be triggered with debounce
28
+ *
29
+ * @default false
30
+ */
31
+ shouldDebounce?: boolean
32
+ /**
33
+ * Timeout for the debounce handler to be triggered
34
+ */
35
+ debounceTimeout?: number
36
+ }
@@ -3,10 +3,23 @@ import { Placement } from 'tippy.js'
3
3
  import { ImageComment, ReleaseTag } from './ImageTags.Types'
4
4
  import { DockerConfigOverrideType, TaskErrorObj } from '.'
5
5
 
6
- export interface ResponseType {
6
+ /**
7
+ * Generic response type object with support for overriding the result type
8
+ *
9
+ * @example Default usage:
10
+ * ```ts
11
+ * interface UserResponse extends ResponseType {}
12
+ * ```
13
+ *
14
+ * @example Override the response type:
15
+ * ```ts
16
+ * interface UserResponse extends ResponseType<Record<string, string>> {}
17
+ * ```
18
+ */
19
+ export interface ResponseType<T = any> {
7
20
  code: number
8
21
  status: string
9
- result?: any
22
+ result?: T
10
23
  errors?: any
11
24
  }
12
25
 
@@ -26,6 +26,7 @@ export * from './Common.service'
26
26
  export * from './Checkbox'
27
27
  export { default as EmptyState } from './EmptyState/EmptyState'
28
28
  export { default as GenericEmptyState } from './EmptyState/GenericEmptyState'
29
+ export * from './SearchBar'
29
30
  export { default as Toggle } from './Toggle/Toggle'
30
31
  export { default as ScanVulnerabilitiesTable } from './Security/ScanVulnerabilitiesTable'
31
32
  export { default as StyledRadioGroup } from './RadioGroup/RadioGroup'