-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRange.js
128 lines (108 loc) · 2.62 KB
/
Range.js
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import classNames from 'classnames';
/*
* Import components and utilities from our extension API. Warning: for demo experiments only.
*/
import {
Errors,
FormContext,
Numberfield,
Description,
Label
} from '@bpmn-io/form-js';
import {
html,
useContext
} from 'diagram-js/lib/ui';
import './styles.css';
import RangeIcon from './range.svg';
export const rangeType = 'range';
/*
* This is the rendering part of the custom field. We use `htm` to
* to render our components without the need of extra JSX transpilation.
*/
export function RangeRenderer(props) {
const {
disabled,
errors = [],
field,
readonly,
value
} = props;
const {
description,
range = {},
id,
label
} = field;
const {
min,
max,
step
} = range;
const { formId } = useContext(FormContext);
const errorMessageId = errors.length === 0 ? undefined : `${prefixId(id, formId)}-error-message`;
const onChange = ({ target }) => {
props.onChange({
field,
value: Number(target.value)
});
};
return html`<div class=${ formFieldClasses(rangeType) }>
<${Label}
id=${ prefixId(id, formId) }
label=${ label } />
<div class="range-group">
<input
type="range"
disabled=${ disabled }
id=${ prefixId(id, formId) }
max=${ max }
min=${ min }
onInput=${ onChange }
readonly=${ readonly }
value=${ value }
step=${ step } />
<div class="range-value">${ value }</div>
</div>
<${Description} description=${ description } />
<${Errors} errors=${ errors } id=${ errorMessageId } />
</div>`;
}
/*
* This is the configuration part of the custom field. It defines
* the schema type, UI label and icon, palette group, properties panel entries
* and much more.
*/
RangeRenderer.config = {
/* we can extend the default configuration of existing fields */
...Numberfield.config,
type: rangeType,
label: 'Range',
iconUrl: `data:image/svg+xml,${ encodeURIComponent(RangeIcon) }`,
propertiesPanelEntries: [
'key',
'label',
'description',
'min',
'max',
'disabled',
'readonly'
]
};
// helper //////////////////////
function formFieldClasses(type, { errors = [], disabled = false, readonly = false } = {}) {
if (!type) {
throw new Error('type required');
}
return classNames('fjs-form-field', `fjs-form-field-${type}`, {
'fjs-has-errors': errors.length > 0,
'fjs-disabled': disabled,
'fjs-readonly': readonly
});
}
function prefixId(id, formId) {
if (formId) {
return `fjs-form-${ formId }-${ id }`;
}
return `fjs-form-${ id }`;
}