Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | 26x 14x 14x 14x 14x 14x 1x 1x 13x 1x 1x 1x 12x 12x 14x 14x 14x 14x 14x 14x 14x 14x 7x 7x 7x 3x 15x 3x 1x 3x 2x 2x 2x 2x 2x 14x 14x 14x 14x 14x 1x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 26x | import PropTypes from 'prop-types';
import { Field } from 'react-final-form';
import orderBy from 'lodash/orderBy';
import {
Col,
Datepicker,
Row,
Select,
TextArea,
TextField,
MultiSelection
} from '@folio/stripes/components';
import * as CUSTOM_PROPERTY_TYPES from '../../constants/customProperties';
import { useKintIntl } from '../../hooks';
import NumberField from '../../NumberField';
const CustomPropertyField = ({
availableCustomProperties,
customProperty,
customPropertyType,
customProperties,
index,
intlKey: passedIntlKey,
intlNS: passedIntlNS,
labelOverrides = {},
name,
onChange,
value,
setCustomProperties
}) => {
const kintIntl = useKintIntl(passedIntlKey, passedIntlNS);
// Set up the validator for the customProperty
const customPropertyValidator = (fieldValue, allValues) => {
const { note, publicNote, value: currentValue } = allValues?.customProperties?.[customProperty?.value]?.[0] ?? {};
Iif ((note && !currentValue) || (publicNote && !currentValue)) {
if (
customProperty.type === CUSTOM_PROPERTY_TYPES.DECIMAL_CLASS_NAME ||
customProperty.type === CUSTOM_PROPERTY_TYPES.INTEGER_CLASS_NAME
) {
return (
kintIntl.formatKintMessage({
id: 'customProperties.errors.customPropertyNoteInvalidNumber',
overrideValue: labelOverrides.customPropertyNoteInvalidNumberError
})
);
} else {
return (
kintIntl.formatKintMessage({
id: 'customProperties.errors.customPropertyNoteWithoutValue',
overrideValue: labelOverrides.customPropertyNoteWithoutValueError
})
);
}
}
if (customProperty.type === CUSTOM_PROPERTY_TYPES.DECIMAL_CLASS_NAME) {
const regexp = /^-?[\d]*(\.[\d]{0,2})?$/;
return (fieldValue && !regexp.test(fieldValue)) ?
kintIntl.formatKintMessage({
id: 'errors.maxTwoDecimals',
overrideValue: labelOverrides.maxTwoDecimalsError
}) : undefined;
}
if (customProperty.type === CUSTOM_PROPERTY_TYPES.INTEGER_CLASS_NAME) {
const min = Number.MIN_SAFE_INTEGER;
const max = Number.MAX_SAFE_INTEGER;
return (fieldValue && (fieldValue > max || fieldValue < min)) ?
kintIntl.formatKintMessage({
id: 'errors.valueNotInRange',
overrideValue: labelOverrides.valueNotInRangeError
}, { min, max }) : undefined;
}
Iif (!customProperty.primary && !currentValue) {
return kintIntl.formatMessage({ id: 'stripes-core.label.missingRequiredField' });
}
return undefined;
};
const getCustomProperty = (customPropertyValue) => {
return availableCustomProperties.find(cp => cp.value === customPropertyValue);
};
const renderCustomPropertyName = () => {
const unsetCustomProperties = availableCustomProperties.filter(t => {
// Only optional properties can be set
if (t.primary) return false;
const custPropValue = value[t.value];
// The customProperty is unset and has no value.
if (custPropValue === undefined) return true;
// The customProperty is set but is marked for deletion. Allow reuse.
if (custPropValue[0]?._delete) return true;
return false;
});
const customPropertyValue = value[customProperty.value];
const id = customPropertyValue?.[0]?.id;
return (
<Select
autoFocus={!id}
dataOptions={[customProperty, ...unsetCustomProperties]} // The selected customProperty, and the available unset customProperties
id={`edit-customproperty-${index}-name`}
label={
kintIntl.formatKintMessage({
id: 'customProperties.name',
overrideValue: labelOverrides.name
})
}
onChange={e => {
const newValue = e.target.value;
// Update `state.customProperties` which controls what customProperties are being edited.
const newCustomProperties = [...customProperties];
newCustomProperties[index] = getCustomProperty(newValue);
setCustomProperties(newCustomProperties);
// Update final-form (which tracks what the values for a given customProperty are) because
// in essence we're deleting a customProperty and creating a new customProperty.
// We do this by 1) marking the current customProperty for deletion and 2) initing
// the new customProperty to an empty object.
const currentValue = value[customProperty.value] ? value[customProperty.value][0] : {};
onChange({
...value,
[customProperty.value]: [{
id: currentValue.id,
_delete: true,
}],
[newValue]: [{}],
});
}}
required
value={customProperty.value}
/>
);
};
const renderCustomPropertyValue = () => {
const currentValue = value[customProperty.value] ? value[customProperty.value][0] : {};
const min = Number.MIN_SAFE_INTEGER;
const max = Number.MAX_SAFE_INTEGER;
// Default change handler
let handleChange = e => {
onChange({
...value,
[customProperty.value]: [{
...currentValue,
_delete: e.target.value === '', // Delete customProperty if removing the value.
value: e.target.value // send down the number for integers and decimal types
}],
});
};
// Figure out which component we're rendering and specify its unique props.
let fieldProps;
switch (customProperty.type) {
case CUSTOM_PROPERTY_TYPES.REFDATA_CLASS_NAME:
fieldProps = {
component: Select,
// don't order dataOptions here, notSet should always be first in list
dataOptions: customProperty.options,
format: (v) => v?.value,
};
break;
case CUSTOM_PROPERTY_TYPES.MULTI_REFDATA_CLASS_NAME:
fieldProps = {
component: MultiSelection,
// The "not set" value is not relevant for a multi select
dataOptions: orderBy(customProperty.options?.filter(opt => !!opt?.value), 'label'),
renderToOverlay: true // FIXME Switch to usePortal for stripes v10
};
// MultiSelection passes the changed array as a value not an event
handleChange = refdataArray => {
onChange({
...value,
[customProperty.value]: [{
...currentValue,
_delete: refdataArray?.length === 0 ? true : undefined, // Delete customProperty if removing all values.
value: refdataArray
}]
});
};
break;
case CUSTOM_PROPERTY_TYPES.INTEGER_CLASS_NAME:
case CUSTOM_PROPERTY_TYPES.DECIMAL_CLASS_NAME:
fieldProps = {
component: NumberField,
max,
min,
step: 'any',
};
handleChange = e => {
onChange({
...value,
[customProperty.value]: [{
...currentValue,
_delete: e.target.value === '' ? true : undefined, // Delete customProperty if removing the value.
value: e.target.value
}],
});
};
break;
case CUSTOM_PROPERTY_TYPES.TEXT_CLASS_NAME:
fieldProps = {
component: TextArea,
parse: v => v // Lets us send an empty string instead of `undefined`
};
break;
case CUSTOM_PROPERTY_TYPES.DATE_CLASS_NAME:
fieldProps = {
component: Datepicker,
backendDateStandard: 'YYYY-MM-DD',
timeZone: 'UTC',
usePortal: true
};
break;
default:
fieldProps = { component: TextField };
break;
}
return (
<Field
data-test-customproperty-value
id={`edit-customproperty-${index}-value`}
label={
kintIntl.formatKintMessage({
id: 'customProperties.value',
overrideValue: labelOverrides.value
})
}
name={`${name}.${customProperty.value}[0].value`}
onChange={handleChange}
required={!customProperty.primary}
validate={(fieldValue, allValues) => customPropertyValidator(fieldValue, allValues)}
{...fieldProps}
/>
);
};
const renderCustomPropertyVisibility = () => {
const customPropertyObject = value[customProperty.value]?.[0] ?? {};
const handleChange = e => {
onChange({
...value,
[customProperty.value]: [{
...customPropertyObject,
internal: e.target.value
}],
});
};
return (
<Select
data-test-customproperty-visibility
dataOptions={[
{
value: true,
label: kintIntl.formatKintMessage({
id: 'customProperties.internalTrue',
overrideValue: labelOverrides.internalTrue
})
},
{
value: false,
label: kintIntl.formatKintMessage({
id: 'customProperties.internalFalse',
overrideValue: labelOverrides.internalFalse
})
}
]}
id={`edit-customproperty-${index}-visibility`}
label={
kintIntl.formatKintMessage({
id: 'customProperties.visibility',
overrideValue: labelOverrides.visibility
})
}
onChange={handleChange}
value={customPropertyObject?.internal ?? customProperty.defaultInternal}
/>
);
};
const renderCustomPropertyNoteInternal = () => {
const customPropertyObject = value[customProperty.value]?.[0] ?? {};
const handleChange = e => {
onChange({
...value,
[customProperty.value]: [{
...customPropertyObject,
note: e.target.value
}],
});
};
return (
<TextArea
data-test-customproperty-note
id={`edit-customproperty-${index}-internal-note`}
label={
kintIntl.formatKintMessage({
id: 'customProperties.internalNote',
overrideValue: labelOverrides.internalNote
})
}
onChange={handleChange}
value={customPropertyObject?.note}
/>
);
};
const renderCustomPropertyNotePublic = () => {
const customPropertyObject = value[customProperty.value]?.[0] ?? {};
const handleChange = e => {
onChange({
...value,
[customProperty.value]: [{
...customPropertyObject,
publicNote: e.target.value
}],
});
};
return (
<TextArea
data-test-customproperty-public-note
id={`edit-customproperty-${index}-public-note`}
label={
kintIntl.formatKintMessage({
id: 'customProperties.publicNote',
overrideValue: labelOverrides.publicNote
})
}
onChange={handleChange}
value={customPropertyObject?.publicNote}
/>
);
};
return (
<>
<div
data-testid="customPropertyField"
>
{
customPropertyType === 'optional' &&
<Row>
<Col xs={12}>
{renderCustomPropertyName()}
</Col>
</Row>
}
<Row>
<Col md={6} xs={12}>
{renderCustomPropertyValue()}
</Col>
<Col md={6} xs={12}>
{renderCustomPropertyNoteInternal()}
</Col>
</Row>
<Row>
<Col md={6} xs={12}>
{renderCustomPropertyVisibility()}
</Col>
<Col md={6} xs={12}>
{renderCustomPropertyNotePublic()}
</Col>
</Row>
</div>
</>
);
};
CustomPropertyField.propTypes = {
availableCustomProperties: PropTypes.arrayOf(PropTypes.object),
customProperty: PropTypes.object,
customPropertyType: PropTypes.string,
customProperties: PropTypes.arrayOf(PropTypes.object),
index: PropTypes.number,
intlKey: PropTypes.string,
intlNS: PropTypes.string,
labelOverrides: PropTypes.object,
name: PropTypes.string,
onChange: PropTypes.func,
setCustomProperties: PropTypes.func,
value: PropTypes.object
};
export default CustomPropertyField;
|