-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathFormulaField.js
More file actions
106 lines (98 loc) · 3.51 KB
/
FormulaField.js
File metadata and controls
106 lines (98 loc) · 3.51 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
import { Center, CircularLoader } from '@dhis2/ui'
import { useDroppable } from '@dnd-kit/core'
import { SortableContext } from '@dnd-kit/sortable'
import PropTypes from 'prop-types'
import React from 'react'
import FormulaIcon from '../../../assets/FormulaIcon.js'
import i18n from '../../../locales/index.js'
import DropZone from './DropZone.js'
import FormulaItem from './FormulaItem.js'
import styles from './styles/FormulaField.style.js'
export const LAST_DROPZONE_ID = 'lastdropzone'
export const FORMULA_BOX_ID = 'formulabox'
const Placeholder = () => (
<div className="placeholder" data-test="placeholder">
<FormulaIcon />
<span className="help-text">
{i18n.t(
'Drag items here, or double click in the list, to start building a calculation formula'
)}
</span>
<style jsx>{styles}</style>
</div>
)
const FormulaField = ({
items = [],
selectedItemId,
focusItemId,
onChange,
onClick,
onDoubleClick,
loading,
}) => {
const { over, setNodeRef: setLastDropzoneRef } = useDroppable({
id: LAST_DROPZONE_ID,
})
const itemIds = items.map((item) => item.id)
const overLastDropZone = over?.id === LAST_DROPZONE_ID
return (
<div className="container">
<div className="border"></div>
<div
className="formula-field"
ref={setLastDropzoneRef}
data-test="formula-field"
>
{loading && (
<Center>
<CircularLoader small />
</Center>
)}
{!loading && itemIds && (
<SortableContext id={FORMULA_BOX_ID} items={itemIds}>
<DropZone
firstElementId={itemIds[0]}
overLastDropZone={overLastDropZone}
/>
{!items.length && <Placeholder />}
{Boolean(items.length) &&
items.map(({ id, label, type, value }, index) => (
<FormulaItem
key={id}
id={id}
label={label}
type={type}
value={value}
hasFocus={focusItemId === id}
isHighlighted={selectedItemId === id}
isLast={index === items.length - 1}
onChange={onChange}
onClick={onClick}
onDoubleClick={onDoubleClick}
overLastDropZone={overLastDropZone}
/>
))}
</SortableContext>
)}
</div>
<style jsx>{styles}</style>
</div>
)
}
FormulaField.propTypes = {
onChange: PropTypes.func.isRequired,
onClick: PropTypes.func.isRequired,
onDoubleClick: PropTypes.func.isRequired,
focusItemId: PropTypes.string,
items: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.string,
label: PropTypes.string,
type: PropTypes.string,
value: PropTypes.string,
})
),
loading: PropTypes.bool,
selectedItemId: PropTypes.string,
}
export default FormulaField