Skip to content

Commit cde73e4

Browse files
Allow setting initial value for autocomplete field
1 parent 05763dc commit cde73e4

File tree

4 files changed

+51
-25
lines changed

4 files changed

+51
-25
lines changed

lib/components/Inputs/Autocomplete/Autocomplete.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import { getHighlightBackgroundColor, shouldHighlightBackground, shouldShowRequi
2323
import parse from 'autosuggest-highlight/parse';
2424
import match from 'autosuggest-highlight/match';
2525
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;
26+
var _b;
27+
var _c = _a.customProperties, _d = _c.options, options = _d === void 0 ? [] : _d, registerReturn = _c.registerReturn, fieldId = _a.fieldId, _e = _a.uiSettings, disabled = _e.disabled, description = _e.description, label = _e.label, placeholder = _e.placeholder, size = _e.size, variant = _e.variant, validation = _a.validation;
2728
var register = useFormContext().register;
2829
if (register === undefined && registerReturn === undefined) {
2930
throw new Error('Either register or registerReturn must be supplied');
@@ -45,15 +46,17 @@ export var Autocomplete = function (_a) {
4546
var isRequired = required !== false;
4647
var showRequiredLabel = shouldShowRequiredLabel(isRequired, disabled);
4748
var highlightBackground = shouldHighlightBackground(value, isRequired, disabled);
48-
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) {
49+
return _jsx(MUIAutocomplete, { disabled: disabled, fullWidth: true, id: 'combo-box-demo', options: options, onBlur: onBlur, onChange: handleChange, renderInput: function (params) {
50+
return _createElement(TextField, __assign({}, params, { error: isErroneous, helperText: isErroneous ? error.message : description, InputLabelProps: { required: showRequiredLabel, shrink: true }, key: fieldId, label: label, name: fieldId, placeholder: placeholder, variant: variant }));
51+
}, renderOption: function (props, option, _a) {
4952
var inputValue = _a.inputValue;
5053
var matches = match(option.label, inputValue);
5154
var parts = parse(option.label, matches);
5255
return (_jsx("li", __assign({}, props, { children: _jsx("div", { children: parts.map(function (part, index) { return (_jsx("span", __assign({ style: {
5356
fontWeight: part.highlight ? 700 : 400,
5457
} }, { children: part.text }), index)); }) }) })));
55-
}, sx: function (theme) { return ({
58+
}, size: size, sx: function (theme) { return ({
5659
backgroundColor: getHighlightBackgroundColor(theme, highlightBackground),
57-
}); } });
60+
}); }, value: (_b = options.find(function (option) { return option.value === value; })) !== null && _b !== void 0 ? _b : null });
5861
};
5962
export default Autocomplete;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pikobytes/pikobytes-react-forms",
3-
"version": "0.2.1",
3+
"version": "0.2.2",
44
"main": "./lib/index.js",
55
"module": "./lib/index.js",
66
"types": "./lib/index.d.ts",

src/components/Form/Form.stories.tsx

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ export default {
1818
};
1919

2020
export function FormProviderWrapper({
21-
children,
22-
defaultValues,
23-
}: {
21+
children,
22+
defaultValues,
23+
}: {
2424
children: JSX.Element;
2525
defaultValues?: { [key: string]: string };
2626
}) {
@@ -33,7 +33,7 @@ function Template(args: any) {
3333
return (
3434
<>
3535
<Form {...args} />
36-
<button form="test" type="submit">
36+
<button form='test' type='submit'>
3737
Submit
3838
</button>
3939
</>
@@ -54,6 +54,27 @@ Default.args = {
5454
uiConfiguration: exampleUiConfiguration,
5555
};
5656

57+
// with initial values
58+
export const InitialValues = Template.bind({});
59+
// @ts-ignore
60+
InitialValues.args = {
61+
formId: 'test_initial_values',
62+
onError: (e) => {
63+
console.log(e);
64+
},
65+
onSubmit: (data) => {
66+
console.log(data);
67+
},
68+
initialValues: {
69+
sensor: '1',
70+
title: 'Initial Title',
71+
country: 'DE',
72+
dataType: '1',
73+
},
74+
configuration: exampleConfiguration,
75+
uiConfiguration: exampleUiConfiguration,
76+
};
77+
5778
export const MultipleFields = Template.bind({});
5879
// @ts-ignore
5980
MultipleFields.args = {

src/components/Inputs/Autocomplete/Autocomplete.tsx

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import match from 'autosuggest-highlight/match';
1414

1515

1616
export const Autocomplete = ({
17-
customProperties: { options, registerReturn },
17+
customProperties: { options = [], registerReturn },
1818
fieldId,
1919
uiSettings: { disabled, description, label, placeholder, size, variant },
2020
validation,
@@ -55,25 +55,25 @@ export const Autocomplete = ({
5555

5656

5757
return <MUIAutocomplete
58-
fullWidth
59-
size={size}
6058
disabled={disabled}
59+
fullWidth
6160
id='combo-box-demo'
6261
options={options}
62+
onBlur={onBlur}
6363
onChange={handleChange}
64-
renderInput={(params) => <TextField
65-
{...params}
66-
error={isErroneous}
67-
helperText={isErroneous ? error!.message : description}
68-
label={label}
69-
placeholder={placeholder}
70-
InputLabelProps={{ required: showRequiredLabel, shrink: true }}
71-
onBlur={onBlur}
72-
inputRef={ref}
73-
variant={variant}
74-
key={fieldId}
75-
name={fieldId}
76-
/>}
64+
renderInput={(params) => {
65+
return <TextField
66+
{...params}
67+
error={isErroneous}
68+
helperText={isErroneous ? error!.message : description}
69+
InputLabelProps={{ required: showRequiredLabel, shrink: true }}
70+
key={fieldId}
71+
label={label}
72+
name={fieldId}
73+
placeholder={placeholder}
74+
variant={variant}
75+
/>;
76+
}}
7777
renderOption={(props, option, { inputValue }) => {
7878
const matches = match(option.label, inputValue);
7979
const parts = parse(option.label, matches);
@@ -95,12 +95,14 @@ export const Autocomplete = ({
9595
</li>
9696
);
9797
}}
98+
size={size}
9899
sx={(theme) => ({
99100
backgroundColor: getHighlightBackgroundColor(
100101
theme,
101102
highlightBackground,
102103
),
103104
})}
105+
value={options.find(option => option.value === value) ?? null}
104106
/>;
105107
};
106108

0 commit comments

Comments
 (0)