Skip to content

Commit a3a0018

Browse files
Add autocomplete field (#12)
* Add autocomplete field * Fix license header
1 parent df20e8b commit a3a0018

39 files changed

+30382
-48339
lines changed

lib/components/Form/Form.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export interface IFormProps {
1111
[key: string]: string;
1212
};
1313
loadingFields?: Array<string>;
14-
onError: (errors: TStringIndexableObject<FieldError>, values: TStringIndexableObject<string>) => void;
14+
onError: (errors: TStringIndexableObject<FieldError | undefined>, values: TStringIndexableObject<string>) => void;
1515
onPublishValues?: (fieldValues: Array<string>, previousFieldValues: Array<string>, options: {
1616
setValue: (field: string, newValue: string) => void;
1717
}) => void;

lib/components/Form/Form.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ export function Form(props) {
145145
onUpdateResetForm(reset);
146146
}
147147
}, [onUpdateResetForm, reset]);
148-
return (_jsx(ErrorBoundary, __assign({ FallbackComponent: FormErrorFallback, onReset: handleReset }, { children: _jsx(FormProvider, __assign({}, formMethods, { children: _jsx(LocalizationProvider, __assign({ dateAdapter: AdapterDateFns }, { children: _jsxs(_Fragment, { children: [activatePersistence && (_jsx(PersistenceHandler, { fieldConfigs: fieldConfigs, persistenceKey: persistenceKey !== null && persistenceKey !== void 0 ? persistenceKey : formId, sections: sections }, void 0)), _jsx("form", __assign({ id: formId, onSubmit: handleSubmit(handleInternalSubmit) }, { children: sections.map(function (_a, index) {
148+
return (_jsx(ErrorBoundary, __assign({ FallbackComponent: FormErrorFallback, onReset: handleReset }, { children: _jsx(FormProvider, __assign({}, formMethods, { children: _jsx(LocalizationProvider, __assign({ dateAdapter: AdapterDateFns }, { children: _jsxs(_Fragment, { children: [activatePersistence && (_jsx(PersistenceHandler, { fieldConfigs: fieldConfigs, persistenceKey: persistenceKey !== null && persistenceKey !== void 0 ? persistenceKey : formId, sections: sections })), _jsx("form", __assign({ id: formId, onSubmit: handleSubmit(handleInternalSubmit) }, { children: sections.map(function (_a, index) {
149149
var title = _a.title, fields = _a.fields;
150150
return (_jsx(Section, { customMapping: customMapping, fieldConfigs: fieldConfigs, fields: fields, loadingFields: loadingFields, title: title, uiSettings: uiSettings, variant: variant, validationFunctionLookup: validationFunctions }, "".concat(title, "_").concat(index)));
151-
}) }), void 0)] }, void 0) }), void 0) }), void 0) }), void 0));
151+
}) }))] }) })) })) })));
152152
}
153153
export default Form;

lib/components/Form/components/PersistenceHandler/PersistenceHandler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,6 @@ export var PersistenceHandler = function (_a) {
9393
localStorage.removeItem(persistenceKey);
9494
}
9595
}, [persistenceKey, isSubmitted]);
96-
return _jsx(_Fragment, {}, void 0);
96+
return _jsx(_Fragment, {});
9797
};
9898
export default PersistenceHandler;

lib/components/Form/util/conditionMatching.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
import { E_CONDITION_EFFECTS, IFieldCondition } from '../../../typedefs/ConditionalFields';
88
import { TFieldValue, TStringIndexableObject } from '../../../typedefs/typedefs';
9-
export declare const applyConditions: (fieldValues: TStringIndexableObject<string | number>, conditions?: TStringIndexableObject<IFieldCondition> | undefined) => {
9+
export declare const applyConditions: (fieldValues: TStringIndexableObject<string | number>, conditions?: TStringIndexableObject<IFieldCondition>) => {
1010
effect: E_CONDITION_EFFECTS;
1111
isMet: boolean;
1212
};

lib/components/Form/util/util.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export declare const getDefaultValues: (initialValues: TStringIndexableObject<st
77
} & TStringIndexableObject<string | boolean> & TStringIndexableObject<string>;
88
export declare const getDefaultValueForFieldType: (fieldType: FIELD_TYPES) => false | "";
99
export declare const getDependentOnFields: (fieldConfigs: IFieldConfig) => string[];
10-
export declare const resolveValidationFunctions: (validationFunctionLookup?: TValidationFunctionLookup | undefined, validationFunctions?: string[] | undefined) => {
10+
export declare const resolveValidationFunctions: (validationFunctionLookup?: TValidationFunctionLookup, validationFunctions?: Array<string>) => {
1111
validate: TValidationFunctionLookup;
1212
} | {
1313
validate?: undefined;

lib/components/FormErrorFallback/FormErrorFallback.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
1313
import { Button, Grid, Typography, } from '@mui/material';
1414
export function FormErrorFallback(_a) {
1515
var error = _a.error, resetErrorBoundary = _a.resetErrorBoundary;
16-
return (_jsxs(Grid, __assign({ item: true, xs: 12 }, { children: [_jsx(Typography, __assign({ sx: { mb: 2 }, variant: "h4" }, { children: "Something went wrong:" }), void 0), _jsx(Typography, __assign({ align: "center", variant: "h6" }, { children: error === null || error === void 0 ? void 0 : error.message }), void 0), _jsx(Grid, __assign({ container: true, justifyContent: "flex-end" }, { children: _jsx(Grid, __assign({ item: true, xs: "auto" }, { children: _jsx(Button, __assign({ color: "primary", onClick: resetErrorBoundary, variant: "contained" }, { children: "Reset" }), void 0) }), void 0) }), void 0)] }), void 0));
16+
return (_jsxs(Grid, __assign({ item: true, xs: 12 }, { children: [_jsx(Typography, __assign({ sx: { mb: 2 }, variant: "h4" }, { children: "Something went wrong:" })), _jsx(Typography, __assign({ align: "center", variant: "h6" }, { children: error === null || error === void 0 ? void 0 : error.message })), _jsx(Grid, __assign({ container: true, justifyContent: "flex-end" }, { children: _jsx(Grid, __assign({ item: true, xs: "auto" }, { children: _jsx(Button, __assign({ color: "primary", onClick: resetErrorBoundary, variant: "contained" }, { children: "Reset" })) })) }))] })));
1717
}
1818
export default FormErrorFallback;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/// <reference types="react" />
2+
import { IDefaultUiSettings, IGenericField, ISelectCustomProperties } from '../../../typedefs/FieldConfiguration';
3+
export declare const Autocomplete: ({ customProperties: { options, registerReturn }, fieldId, uiSettings: { disabled, description, label, placeholder, size, variant }, validation, }: IGenericField<IDefaultUiSettings, ISelectCustomProperties>) => JSX.Element;
4+
export default Autocomplete;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
var __assign = (this && this.__assign) || function () {
2+
__assign = Object.assign || function(t) {
3+
for (var s, i = 1, n = arguments.length; i < n; i++) {
4+
s = arguments[i];
5+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6+
t[p] = s[p];
7+
}
8+
return t;
9+
};
10+
return __assign.apply(this, arguments);
11+
};
12+
import { jsx as _jsx } from "react/jsx-runtime";
13+
import { createElement as _createElement } from "react";
14+
/**
15+
* Created by [email protected] on 06.07.22
16+
*
17+
* This file is subject to the terms and conditions defined in
18+
* file 'LICENSE.txt', which is part of this source code package.
19+
*/
20+
import { Autocomplete as MUIAutocomplete, TextField } from '@mui/material';
21+
import { useController, useFormContext } from 'react-hook-form';
22+
import { getHighlightBackgroundColor, shouldHighlightBackground, shouldShowRequiredLabel } from '../util';
23+
import parse from 'autosuggest-highlight/parse';
24+
import match from 'autosuggest-highlight/match';
25+
export var Autocomplete = function (_a) {
26+
var _b = _a.customProperties, options = _b.options, registerReturn = _b.registerReturn, fieldId = _a.fieldId, _c = _a.uiSettings, disabled = _c.disabled, description = _c.description, label = _c.label, placeholder = _c.placeholder, size = _c.size, variant = _c.variant, validation = _a.validation;
27+
var register = useFormContext().register;
28+
if (register === undefined && registerReturn === undefined) {
29+
throw new Error('Either register or registerReturn must be supplied');
30+
}
31+
var required = validation.required;
32+
var field = useController({
33+
name: fieldId,
34+
rules: Object.assign({ disabled: disabled }, validation),
35+
}).field;
36+
var onChange = field.onChange, onBlur = field.onBlur, ref = field.ref, value = field.value;
37+
var handleChange = function (e, newValue, reason) {
38+
onChange(newValue.value);
39+
};
40+
var formState = useFormContext().formState;
41+
var errors = formState.errors;
42+
var error = errors[fieldId];
43+
var isErroneous = error !== undefined;
44+
var isRequired = required !== false;
45+
var showRequiredLabel = shouldShowRequiredLabel(isRequired, disabled);
46+
var highlightBackground = shouldHighlightBackground(value, isRequired, disabled);
47+
return _jsx(MUIAutocomplete, { fullWidth: true, size: size, disabled: disabled, id: 'combo-box-demo', options: options, onChange: handleChange, renderInput: function (params) { return _createElement(TextField, __assign({}, params, { error: isErroneous, helperText: isErroneous ? error.message : description, label: label, placeholder: placeholder, InputLabelProps: { required: showRequiredLabel, shrink: true }, onBlur: onBlur, inputRef: ref, variant: variant, key: fieldId, name: fieldId })); }, renderOption: function (props, option, _a) {
48+
var inputValue = _a.inputValue;
49+
var matches = match(option.label, inputValue);
50+
var parts = parse(option.label, matches);
51+
return (_jsx("li", __assign({}, props, { children: _jsx("div", { children: parts.map(function (part, index) { return (_jsx("span", __assign({ style: {
52+
fontWeight: part.highlight ? 700 : 400,
53+
} }, { children: part.text }), index)); }) }) })));
54+
}, sx: function (theme) { return ({
55+
backgroundColor: getHighlightBackgroundColor(theme, highlightBackground),
56+
}); } });
57+
};
58+
export default Autocomplete;

lib/components/Inputs/Checkbox/Checkbox.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default function Checkbox(props) {
2929
var highlightBackground = field.value !== true && isRequired;
3030
return (_jsxs(FormControl, __assign({ disabled: disabled, error: isErroneous, fullWidth: true, required: isRequired, size: size }, { children: [_jsx(FormGroup, { children: _jsx(FormControlLabel, { control: _jsx(MUICheckBox, __assign({}, field, { checked: (_a = field.value) !== null && _a !== void 0 ? _a : false, onChange: function (e) {
3131
field.onChange(e.target.checked);
32-
} }), void 0), label: label !== null && label !== void 0 ? label : '', sx: function (theme) { return ({
32+
} })), label: label !== null && label !== void 0 ? label : '', sx: function (theme) { return ({
3333
backgroundColor: getHighlightBackgroundColor(theme, highlightBackground),
34-
}); } }, void 0) }, void 0), isErroneous && _jsx(FormHelperText, { children: error.message }, void 0)] }), void 0));
34+
}); } }) }), isErroneous && _jsx(FormHelperText, { children: error.message })] })));
3535
}

lib/components/Inputs/DatePicker/DatePicker.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ export function DatePicker(_a) {
4242
var isRequired = required !== false;
4343
var highlightBackground = shouldHighlightBackground(value, isRequired, disabled);
4444
var showRequiredLabel = shouldShowRequiredLabel(isRequired, disabled);
45-
return (_jsx(MUIDatePicker, __assign({ clearable: true, disabled: disabled, disableFuture: disableFuture, renderInput: function (props) { return (_jsx(TextField, __assign({}, props, { InputLabelProps: { required: showRequiredLabel, shrink: true }, fullWidth: true, helperText: isErroneous ? error.message : description, placeholder: placeholder, size: size, variant: variant }), void 0)); }, inputFormat: "yyyy/MM/dd", mask: "____/__/__", InputProps: {
46-
endAdornment: (_jsx(InputAdornment, __assign({ position: "end" }, { children: _jsx(IconButton, { children: _jsx(Event, {}, void 0) }, void 0) }), void 0)),
45+
return (_jsx(MUIDatePicker, __assign({ clearable: true, disabled: disabled, disableFuture: disableFuture, renderInput: function (props) { return (_jsx(TextField, __assign({}, props, { InputLabelProps: { required: showRequiredLabel, shrink: true }, fullWidth: true, helperText: isErroneous ? error.message : description, placeholder: placeholder, size: size, variant: variant }))); }, inputFormat: "yyyy/MM/dd", mask: "____/__/__", InputProps: {
46+
endAdornment: (_jsx(InputAdornment, __assign({ position: "end" }, { children: _jsx(IconButton, { children: _jsx(Event, {}) }) }))),
4747
sx: function (theme) { return ({
4848
backgroundColor: getHighlightBackgroundColor(theme, highlightBackground),
4949
}); },
@@ -53,6 +53,6 @@ export function DatePicker(_a) {
5353
// @ts-ignore
5454
onChange(dateTimeObject.toISOString().split('T')[0]);
5555
}
56-
} }, rest), void 0));
56+
} }, rest)));
5757
}
5858
export default DatePicker;

0 commit comments

Comments
 (0)