@devtron-labs/devtron-fe-common-lib 0.0.58 → 0.0.59-beta-2
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/dist/Common/Api.d.ts +9 -0
- package/dist/Common/Constants.d.ts +1 -0
- package/dist/Common/CustomInput/CustomInput.d.ts +1 -1
- package/dist/Common/CustomInput/Types.d.ts +2 -1
- package/dist/Common/SearchBar/types.d.ts +2 -2
- package/dist/Common/Types.d.ts +3 -2
- package/dist/Pages/GlobalConfigurations/BuildInfra/BuildInfraConfigForm.d.ts +4 -0
- package/dist/Pages/GlobalConfigurations/BuildInfra/BuildInfraDescriptionField.d.ts +4 -0
- package/dist/Pages/GlobalConfigurations/BuildInfra/BuildInfraFormAction.d.ts +7 -0
- package/dist/Pages/GlobalConfigurations/BuildInfra/BuildInfraFormItem.d.ts +4 -0
- package/dist/Pages/GlobalConfigurations/BuildInfra/BuildInfraProfileNameField.d.ts +4 -0
- package/dist/Pages/GlobalConfigurations/BuildInfra/Descriptor.d.ts +4 -0
- package/dist/Pages/GlobalConfigurations/BuildInfra/Footer.d.ts +4 -0
- package/dist/Pages/GlobalConfigurations/BuildInfra/constants.d.ts +52 -0
- package/dist/Pages/GlobalConfigurations/BuildInfra/index.d.ts +7 -0
- package/dist/Pages/GlobalConfigurations/BuildInfra/services.d.ts +6 -0
- package/dist/Pages/GlobalConfigurations/BuildInfra/types.d.ts +286 -0
- package/dist/Pages/GlobalConfigurations/BuildInfra/utils.d.ts +12 -0
- package/dist/Pages/GlobalConfigurations/index.d.ts +1 -0
- package/dist/Pages/index.d.ts +1 -0
- package/dist/Shared/Components/APIResponseHandler/APIResponseHandler.d.ts +4 -0
- package/dist/Shared/Components/APIResponseHandler/constants.d.ts +4 -0
- package/dist/Shared/Components/APIResponseHandler/index.d.ts +2 -0
- package/dist/Shared/Components/APIResponseHandler/types.d.ts +37 -0
- package/dist/Shared/Components/BulkSelection/BulkSelection.d.ts +4 -0
- package/dist/Shared/Components/BulkSelection/BulkSelectionDropdownItems.d.ts +4 -0
- package/dist/Shared/Components/BulkSelection/UseBulkSelection.d.ts +3 -0
- package/dist/Shared/Components/BulkSelection/constants.d.ts +8 -0
- package/dist/Shared/Components/BulkSelection/index.d.ts +4 -0
- package/dist/Shared/Components/BulkSelection/types.d.ts +50 -0
- package/dist/Shared/Components/ReactSelect/index.d.ts +1 -0
- package/dist/Shared/Components/ReactSelect/utils.d.ts +12 -0
- package/dist/Shared/Components/index.d.ts +3 -0
- package/dist/Shared/Helpers.d.ts +16 -0
- package/dist/Shared/index.d.ts +3 -0
- package/dist/Shared/validations.d.ts +7 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +1342 -160
- package/package.json +1 -1
- package/src/Assets/Icon/ic-check-all.svg +5 -0
- package/src/Assets/Icon/ic-check-square.svg +4 -0
- package/src/Assets/Icon/ic-cpu.svg +4 -0
- package/src/Assets/Icon/ic-memory.svg +3 -0
- package/src/Assets/Icon/ic-timer.svg +3 -0
- package/src/Common/Api.ts +21 -0
- package/src/Common/BreadCrumb/Types.ts +0 -1
- package/src/Common/Checkbox.tsx +1 -0
- package/src/Common/Constants.ts +2 -1
- package/src/Common/CustomInput/CustomInput.tsx +6 -2
- package/src/Common/CustomInput/Types.ts +4 -3
- package/src/Common/DraggableWrapper/DraggableWrapper.tsx +3 -1
- package/src/Common/RJSF/utils.ts +2 -0
- package/src/Common/SearchBar/SearchBar.component.tsx +26 -15
- package/src/Common/SearchBar/searchBar.scss +1 -1
- package/src/Common/SearchBar/types.ts +1 -3
- package/src/Common/Types.ts +2 -1
- package/src/Pages/GlobalConfigurations/BuildInfra/BuildInfraConfigForm.tsx +128 -0
- package/src/Pages/GlobalConfigurations/BuildInfra/BuildInfraDescriptionField.tsx +44 -0
- package/src/Pages/GlobalConfigurations/BuildInfra/BuildInfraFormAction.tsx +106 -0
- package/src/Pages/GlobalConfigurations/BuildInfra/BuildInfraFormItem.tsx +55 -0
- package/src/Pages/GlobalConfigurations/BuildInfra/BuildInfraProfileNameField.tsx +32 -0
- package/src/Pages/GlobalConfigurations/BuildInfra/Descriptor.tsx +45 -0
- package/src/Pages/GlobalConfigurations/BuildInfra/Footer.tsx +55 -0
- package/src/Pages/GlobalConfigurations/BuildInfra/Readme.md +4 -0
- package/src/Pages/GlobalConfigurations/BuildInfra/constants.tsx +141 -0
- package/src/Pages/GlobalConfigurations/BuildInfra/index.ts +7 -0
- package/src/Pages/GlobalConfigurations/BuildInfra/services.tsx +167 -0
- package/src/Pages/GlobalConfigurations/BuildInfra/types.tsx +339 -0
- package/src/Pages/GlobalConfigurations/BuildInfra/utils.tsx +508 -0
- package/src/Pages/GlobalConfigurations/index.ts +1 -0
- package/src/Pages/index.ts +1 -0
- package/src/Shared/Components/APIResponseHandler/APIResponseHandler.tsx +41 -0
- package/src/Shared/Components/APIResponseHandler/constants.tsx +5 -0
- package/src/Shared/Components/APIResponseHandler/index.ts +2 -0
- package/src/Shared/Components/APIResponseHandler/types.tsx +39 -0
- package/src/Shared/Components/BulkSelection/BulkSelection.tsx +81 -0
- package/src/Shared/Components/BulkSelection/BulkSelectionDropdownItems.tsx +31 -0
- package/src/Shared/Components/BulkSelection/UseBulkSelection.tsx +99 -0
- package/src/Shared/Components/BulkSelection/constants.tsx +12 -0
- package/src/Shared/Components/BulkSelection/index.ts +4 -0
- package/src/Shared/Components/BulkSelection/types.tsx +58 -0
- package/src/Shared/Components/ReactSelect/index.ts +1 -0
- package/src/Shared/Components/ReactSelect/utils.tsx +97 -0
- package/src/Shared/Components/index.ts +3 -0
- package/src/Shared/Helpers.tsx +29 -0
- package/src/Shared/index.ts +3 -0
- package/src/Shared/validations.tsx +72 -0
- package/src/index.ts +3 -1
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { BulkSelectionDropdownItemsProps } from './types'
|
|
2
|
+
|
|
3
|
+
const BulkSelectionDropdownItems = <T,>({
|
|
4
|
+
locator,
|
|
5
|
+
label,
|
|
6
|
+
isSelected,
|
|
7
|
+
handleBulkSelection,
|
|
8
|
+
icon: Icon,
|
|
9
|
+
}: BulkSelectionDropdownItemsProps<T>) => {
|
|
10
|
+
const handleSelect = () => {
|
|
11
|
+
handleBulkSelection({
|
|
12
|
+
action: locator,
|
|
13
|
+
})
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return (
|
|
17
|
+
<button
|
|
18
|
+
data-testid={`bulk-action-${locator}`}
|
|
19
|
+
onClick={handleSelect}
|
|
20
|
+
className={`dc__no-border dc__outline-none-imp w-100 flexbox h-32 dc__align-items-center pt-6 pr-8 pb-6 pl-8 ${
|
|
21
|
+
isSelected ? 'cb-5 bcb-2 fs-13 fw-6 lh-20' : 'dc__no-background cn-9 fs-13 fw-4 lh-20 dc__hover-n50'
|
|
22
|
+
}`}
|
|
23
|
+
type="button"
|
|
24
|
+
>
|
|
25
|
+
<Icon className="icon-dim-16" />
|
|
26
|
+
{label}
|
|
27
|
+
</button>
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export default BulkSelectionDropdownItems
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { useState } from 'react'
|
|
2
|
+
import { SELECT_ALL_ACROSS_PAGES_LOCATOR, getInvalidActionMessage } from './constants'
|
|
3
|
+
import {
|
|
4
|
+
BulkSelectionEvents,
|
|
5
|
+
HandleBulkSelectionType,
|
|
6
|
+
SelectAllDialogStatus,
|
|
7
|
+
UseBulkSelectionProps,
|
|
8
|
+
UseBulkSelectionReturnType,
|
|
9
|
+
} from './types'
|
|
10
|
+
|
|
11
|
+
const useBulkSelection = <T,>({
|
|
12
|
+
identifiers,
|
|
13
|
+
getSelectAllDialogStatus,
|
|
14
|
+
}: UseBulkSelectionProps<T>): UseBulkSelectionReturnType<T> => {
|
|
15
|
+
const [selectedIdentifiers, setSelectedIdentifiers] = useState<T>({} as T)
|
|
16
|
+
|
|
17
|
+
const setIdentifiersAfterClear = (newIdentifiers: T, selectedIds: (number | string)[]) => {
|
|
18
|
+
const _newIdentifiers = JSON.parse(JSON.stringify(newIdentifiers))
|
|
19
|
+
selectedIds.forEach((id) => {
|
|
20
|
+
delete _newIdentifiers[id]
|
|
21
|
+
})
|
|
22
|
+
setSelectedIdentifiers(_newIdentifiers)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const setIdentifiersAfterPageSelection = (baseObject: T) => {
|
|
26
|
+
const _selectedIdentifiers = JSON.parse(JSON.stringify(selectedIdentifiers))
|
|
27
|
+
// removing bulk selection across pages if present
|
|
28
|
+
if (_selectedIdentifiers[SELECT_ALL_ACROSS_PAGES_LOCATOR]) {
|
|
29
|
+
delete _selectedIdentifiers[SELECT_ALL_ACROSS_PAGES_LOCATOR]
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
setSelectedIdentifiers({
|
|
33
|
+
..._selectedIdentifiers,
|
|
34
|
+
...baseObject,
|
|
35
|
+
})
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const handleBulkSelection = ({ action, data }: HandleBulkSelectionType<T>) => {
|
|
39
|
+
const selectedIds = data?.identifierIds ?? []
|
|
40
|
+
const identifierObject = data?.identifierObject ?? {}
|
|
41
|
+
|
|
42
|
+
switch (action) {
|
|
43
|
+
case BulkSelectionEvents.CLEAR_ALL_SELECTIONS:
|
|
44
|
+
setSelectedIdentifiers({} as T)
|
|
45
|
+
break
|
|
46
|
+
|
|
47
|
+
case BulkSelectionEvents.CLEAR_IDENTIFIERS_AFTER_ACROSS_SELECTION: {
|
|
48
|
+
setIdentifiersAfterClear(identifiers, selectedIds)
|
|
49
|
+
break
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
case BulkSelectionEvents.CLEAR_IDENTIFIERS: {
|
|
53
|
+
setIdentifiersAfterClear(selectedIdentifiers, selectedIds)
|
|
54
|
+
break
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
case BulkSelectionEvents.SELECT_ALL_ACROSS_PAGES: {
|
|
58
|
+
if (selectedIdentifiers[SELECT_ALL_ACROSS_PAGES_LOCATOR]) {
|
|
59
|
+
return
|
|
60
|
+
}
|
|
61
|
+
const selectedIdentifiersLength = Object.keys(selectedIdentifiers).length
|
|
62
|
+
if (selectedIdentifiersLength > 0 && getSelectAllDialogStatus() === SelectAllDialogStatus.OPEN) {
|
|
63
|
+
return
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
setSelectedIdentifiers({
|
|
67
|
+
[SELECT_ALL_ACROSS_PAGES_LOCATOR]: true,
|
|
68
|
+
} as unknown as T)
|
|
69
|
+
break
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
case BulkSelectionEvents.CLEAR_SELECTIONS_AND_SELECT_ALL_ACROSS_PAGES:
|
|
73
|
+
setSelectedIdentifiers({
|
|
74
|
+
[SELECT_ALL_ACROSS_PAGES_LOCATOR]: true,
|
|
75
|
+
} as unknown as T)
|
|
76
|
+
break
|
|
77
|
+
|
|
78
|
+
case BulkSelectionEvents.SELECT_ALL_ON_PAGE: {
|
|
79
|
+
setIdentifiersAfterPageSelection(identifiers)
|
|
80
|
+
break
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
case BulkSelectionEvents.SELECT_IDENTIFIER: {
|
|
84
|
+
setIdentifiersAfterPageSelection(identifierObject as T)
|
|
85
|
+
break
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
default:
|
|
89
|
+
throw new Error(getInvalidActionMessage(action))
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
handleBulkSelection,
|
|
95
|
+
selectedIdentifiers,
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export default useBulkSelection
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { BulkSelectionEvents } from './types'
|
|
2
|
+
|
|
3
|
+
export const BULK_DROPDOWN_TEST_ID = 'bulk-selection-pop-up-menu' as const
|
|
4
|
+
export const BulkSelectionOptionsLabels = {
|
|
5
|
+
[BulkSelectionEvents.SELECT_ALL_ON_PAGE]: 'All on this page',
|
|
6
|
+
[BulkSelectionEvents.SELECT_ALL_ACROSS_PAGES]: 'All across pages',
|
|
7
|
+
[BulkSelectionEvents.CLEAR_ALL_SELECTIONS]: 'Clear all selections',
|
|
8
|
+
} as const
|
|
9
|
+
|
|
10
|
+
// Considering application can not be named as *
|
|
11
|
+
export const SELECT_ALL_ACROSS_PAGES_LOCATOR = '*' as const
|
|
12
|
+
export const getInvalidActionMessage = (action: string) => `Invalid action ${action} passed to useBulkSelection`
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { CHECKBOX_VALUE } from '../../../Common'
|
|
2
|
+
|
|
3
|
+
export enum BulkSelectionEvents {
|
|
4
|
+
CLEAR_ALL_SELECTIONS = 'CLEAR_ALL_SELECTIONS',
|
|
5
|
+
CLEAR_IDENTIFIERS = 'CLEAR_IDENTIFIERS',
|
|
6
|
+
SELECT_ALL_ACROSS_PAGES = 'SELECT_ALL_ACROSS_PAGES',
|
|
7
|
+
SELECT_ALL_ON_PAGE = 'SELECT_ALL_ON_PAGE',
|
|
8
|
+
SELECT_IDENTIFIER = 'SELECT_IDENTIFIER',
|
|
9
|
+
CLEAR_IDENTIFIERS_AFTER_ACROSS_SELECTION = 'CLEAR_IDENTIFIERS_AFTER_ACROSS_SELECTION',
|
|
10
|
+
CLEAR_SELECTIONS_AND_SELECT_ALL_ACROSS_PAGES = 'CLEAR_SELECTIONS_AND_SELECT_ALL_ACROSS_PAGES',
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface HandleBulkSelectionType<T> {
|
|
14
|
+
action: BulkSelectionEvents
|
|
15
|
+
data?: {
|
|
16
|
+
identifierIds?: (number | string)[]
|
|
17
|
+
identifierObject?: T
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface UseBulkSelectionReturnType<T> {
|
|
22
|
+
selectedIdentifiers: T
|
|
23
|
+
handleBulkSelection: ({ action, data }: HandleBulkSelectionType<T>) => void
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface BulkSelectionProps<T> extends UseBulkSelectionReturnType<T> {
|
|
27
|
+
checkboxValue: CHECKBOX_VALUE
|
|
28
|
+
isChecked: boolean
|
|
29
|
+
showPagination: boolean
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface BulkSelectionDropdownItemsType {
|
|
33
|
+
locator: BulkSelectionEvents
|
|
34
|
+
label: string
|
|
35
|
+
isSelected: boolean
|
|
36
|
+
icon: React.FunctionComponent<React.SVGProps<SVGSVGElement>>
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface BulkSelectionDropdownItemsProps<T>
|
|
40
|
+
extends BulkSelectionDropdownItemsType,
|
|
41
|
+
Pick<BulkSelectionProps<T>, 'handleBulkSelection'> {}
|
|
42
|
+
|
|
43
|
+
export enum SelectAllDialogStatus {
|
|
44
|
+
OPEN = 'OPEN',
|
|
45
|
+
CLOSED = 'CLOSED',
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface UseBulkSelectionProps<T> {
|
|
49
|
+
/**
|
|
50
|
+
* Response from API, assuming structure to be array of objects with key and values
|
|
51
|
+
* This will the given ids on current page
|
|
52
|
+
*/
|
|
53
|
+
identifiers: T
|
|
54
|
+
/**
|
|
55
|
+
* Act as buffer between select all across pages and select all on page state
|
|
56
|
+
*/
|
|
57
|
+
getSelectAllDialogStatus: () => SelectAllDialogStatus
|
|
58
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './utils'
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import Tippy from '@tippyjs/react'
|
|
2
|
+
import { components } from 'react-select'
|
|
3
|
+
import { stopPropagation } from '../../../Common'
|
|
4
|
+
|
|
5
|
+
export const getCommonSelectStyle = (styleOverrides = {}) => ({
|
|
6
|
+
menuList: (base) => ({
|
|
7
|
+
...base,
|
|
8
|
+
paddingTop: 0,
|
|
9
|
+
paddingBottom: 0,
|
|
10
|
+
}),
|
|
11
|
+
control: (base, state) => ({
|
|
12
|
+
...base,
|
|
13
|
+
minHeight: '32px',
|
|
14
|
+
boxShadow: 'none',
|
|
15
|
+
backgroundColor: 'var(--N50)',
|
|
16
|
+
border: state.isFocused ? '1px solid var(--B500)' : '1px solid var(--N200)',
|
|
17
|
+
cursor: 'pointer',
|
|
18
|
+
}),
|
|
19
|
+
option: (base, state) => ({
|
|
20
|
+
...base,
|
|
21
|
+
color: 'var(--N900)',
|
|
22
|
+
backgroundColor: state.isFocused ? 'var(--N100)' : 'white',
|
|
23
|
+
padding: '10px 12px',
|
|
24
|
+
}),
|
|
25
|
+
dropdownIndicator: (base, state) => ({
|
|
26
|
+
...base,
|
|
27
|
+
color: 'var(--N400)',
|
|
28
|
+
padding: '0 8px',
|
|
29
|
+
transition: 'all .2s ease',
|
|
30
|
+
transform: state.selectProps.menuIsOpen ? 'rotate(180deg)' : 'rotate(0deg)',
|
|
31
|
+
}),
|
|
32
|
+
valueContainer: (base) => ({
|
|
33
|
+
...base,
|
|
34
|
+
padding: '0 8px',
|
|
35
|
+
fontWeight: '400',
|
|
36
|
+
}),
|
|
37
|
+
loadingMessage: (base) => ({
|
|
38
|
+
...base,
|
|
39
|
+
color: 'var(--N600)',
|
|
40
|
+
}),
|
|
41
|
+
noOptionsMessage: (base) => ({
|
|
42
|
+
...base,
|
|
43
|
+
color: 'var(--N600)',
|
|
44
|
+
}),
|
|
45
|
+
...styleOverrides,
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
const getCustomOptionBackgroundColor = (isSelected: boolean, isFocused: boolean) => {
|
|
49
|
+
if (isSelected) {
|
|
50
|
+
return 'var(--B100)'
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (isFocused) {
|
|
54
|
+
return 'var(--N50)'
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return 'var(--white)'
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export const getCustomOptionSelectionStyle =
|
|
61
|
+
(styleOverrides = {}) =>
|
|
62
|
+
(base, state) => ({
|
|
63
|
+
...base,
|
|
64
|
+
backgroundColor: getCustomOptionBackgroundColor(state.isSelected, state.isFocused),
|
|
65
|
+
color: state.isSelected ? 'var(--B500)' : 'var(--N900)',
|
|
66
|
+
textOverflow: 'ellipsis',
|
|
67
|
+
fontWeight: '500',
|
|
68
|
+
overflow: 'hidden',
|
|
69
|
+
textAlign: 'left',
|
|
70
|
+
whiteSpace: 'nowrap',
|
|
71
|
+
cursor: 'pointer',
|
|
72
|
+
fontSize: '13px',
|
|
73
|
+
...styleOverrides,
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
export const SelectOption = (props: any) => {
|
|
77
|
+
const { selectProps, data, showTippy, style, placement, tippyContent, tippyClass } = props
|
|
78
|
+
selectProps.styles.option = getCustomOptionSelectionStyle(style)
|
|
79
|
+
const getOption = () => (
|
|
80
|
+
<div onClick={stopPropagation}>
|
|
81
|
+
<components.Option {...props} />
|
|
82
|
+
</div>
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
return showTippy ? (
|
|
86
|
+
<Tippy
|
|
87
|
+
className={tippyClass || 'default-white'}
|
|
88
|
+
arrow={false}
|
|
89
|
+
placement={placement || 'right'}
|
|
90
|
+
content={tippyContent || data.label}
|
|
91
|
+
>
|
|
92
|
+
{getOption()}
|
|
93
|
+
</Tippy>
|
|
94
|
+
) : (
|
|
95
|
+
getOption()
|
|
96
|
+
)
|
|
97
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
interface HighlightSearchTextProps {
|
|
2
|
+
/**
|
|
3
|
+
* The text to be highlighted
|
|
4
|
+
*/
|
|
5
|
+
searchText: string
|
|
6
|
+
/**
|
|
7
|
+
* The whole text string
|
|
8
|
+
*/
|
|
9
|
+
text: string
|
|
10
|
+
/**
|
|
11
|
+
* The classes to be applied to the highlighted text
|
|
12
|
+
*/
|
|
13
|
+
highlightClasses?: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Disabling default export since this is a helper function and we would have to export a lot of functions in future.
|
|
17
|
+
// eslint-disable-next-line import/prefer-default-export
|
|
18
|
+
export const highlightSearchText = ({ searchText, text, highlightClasses }: HighlightSearchTextProps): string => {
|
|
19
|
+
if (!searchText) {
|
|
20
|
+
return text
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
const regex = new RegExp(searchText, 'gi')
|
|
25
|
+
return text.replace(regex, (match) => `<span class="${highlightClasses}">${match}</span>`)
|
|
26
|
+
} catch (error) {
|
|
27
|
+
return text
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
export interface ValidationResponseType {
|
|
2
|
+
isValid: boolean
|
|
3
|
+
message?: string
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export const validateName = (name: string): ValidationResponseType => {
|
|
7
|
+
if (!/^.{3,50}$/.test(name)) {
|
|
8
|
+
return {
|
|
9
|
+
isValid: false,
|
|
10
|
+
message: 'Minimum 3 and maximum 50 characters allowed',
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (!/^[a-z0-9-._]+$/.test(name)) {
|
|
15
|
+
return {
|
|
16
|
+
isValid: false,
|
|
17
|
+
message: 'Only lowercase alphanumeric characters, -, _ or . allowed',
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (!/^(?![-._]).*[^-._]$/.test(name)) {
|
|
22
|
+
return {
|
|
23
|
+
isValid: false,
|
|
24
|
+
message: 'Cannot start/end with -, _ or .',
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
isValid: true,
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const validateDescription = (description: string): ValidationResponseType => {
|
|
34
|
+
if (description?.length > 300) {
|
|
35
|
+
return {
|
|
36
|
+
isValid: false,
|
|
37
|
+
message: 'Maximum 300 characters are allowed',
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
isValid: true,
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export const validateRequiredPositiveNumber = (value: string | number): ValidationResponseType => {
|
|
47
|
+
if (!value) {
|
|
48
|
+
return {
|
|
49
|
+
isValid: false,
|
|
50
|
+
message: 'This field is required',
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// TODO: Look if regex is available for this, since 0002 is not a valid number
|
|
55
|
+
if (!/^\d+(\.\d+)?$/.test(value.toString())) {
|
|
56
|
+
return {
|
|
57
|
+
isValid: false,
|
|
58
|
+
message: 'This field should be a valid positive number',
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (Number(value) <= 0) {
|
|
63
|
+
return {
|
|
64
|
+
isValid: false,
|
|
65
|
+
message: 'Only positive numbers are allowed',
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
isValid: true,
|
|
71
|
+
}
|
|
72
|
+
}
|
package/src/index.ts
CHANGED