colouring-montreal/app/src/frontend/building/data-components/data-entry-input.tsx

91 lines
2.9 KiB
TypeScript
Raw Normal View History

2020-01-06 14:48:47 -05:00
import React, { useState } from 'react';
import { AutofillDropdown } from './autofill-dropdown/autofill-dropdown';
2020-01-06 11:21:44 -05:00
export interface TextDataEntryInputProps {
slug: string;
2020-01-06 14:48:47 -05:00
name?: string;
2020-01-07 14:06:42 -05:00
id?: string;
2020-01-06 14:48:47 -05:00
onChange?: (key: string, val: any) => void;
2020-03-27 10:32:19 -04:00
onConfirm?: (key: string, val: any) => void;
2020-01-06 14:48:47 -05:00
2020-01-06 11:21:44 -05:00
maxLength?: number;
disabled?: boolean;
placeholder?: string;
isUrl?: boolean;
required?: boolean;
2020-01-06 11:21:44 -05:00
valueTransform?: (val: string) => string;
2020-03-27 10:32:19 -04:00
confirmOnEnter?: boolean;
2020-01-06 14:48:47 -05:00
autofill?: boolean;
2020-03-19 14:17:56 -04:00
showAllOptionsOnEmpty?: boolean;
2020-03-27 10:32:19 -04:00
confirmOnAutofillSelect?: boolean;
2020-01-06 11:21:44 -05:00
}
2020-01-06 14:48:47 -05:00
export const DataEntryInput: React.FC<TextDataEntryInputProps & {value?: string}> = props => {
const [isEditing, setEditing] = useState(false);
2020-01-07 14:06:42 -05:00
const nameAttr = props.name || props.slug;
const idAttr = props.id || props.slug;
2020-01-06 14:48:47 -05:00
2020-03-27 10:32:19 -04:00
const transformValue = (value: string) => {
2020-01-06 14:48:47 -05:00
const transform = props.valueTransform || (x => x);
const transformedValue = value === '' ?
null :
transform(value);
2020-03-27 10:32:19 -04:00
return transformedValue;
};
const handleChange = (value: string) => {
props.onChange?.(props.slug, transformValue(value));
};
const handleConfirm = () => {
props.onConfirm?.(props.slug, props.value);
};
const handleAutofillSelect = (value: string) => {
const transformedValue = transformValue(value);
if(props.confirmOnAutofillSelect) {
props.onConfirm?.(props.slug, transformedValue);
} else {
props.onChange?.(props.slug, transformedValue);
}
2020-01-06 14:48:47 -05:00
};
2020-01-06 11:21:44 -05:00
return (
2020-01-07 14:06:42 -05:00
<>
<input className="form-control" type={props.isUrl? "url" : "text"}
2020-01-07 14:06:42 -05:00
id={idAttr}
name={nameAttr}
2020-01-06 14:48:47 -05:00
value={props.value || ''}
maxLength={props.maxLength}
required={props.required}
2020-01-06 14:48:47 -05:00
disabled={props.disabled}
placeholder={props.placeholder}
2020-03-27 10:32:19 -04:00
onKeyDown={e => {
if(e.keyCode === 13) {
if(props.confirmOnEnter) {
handleConfirm();
}
}
}}
2020-01-06 14:48:47 -05:00
onChange={e => handleChange(e.target.value)}
2020-01-07 14:06:42 -05:00
onInput={e => setEditing(true)}
onFocus={e => setEditing(true)}
onBlur={e => setEditing(false)}
2020-01-06 14:48:47 -05:00
/>
{
props.autofill &&
<AutofillDropdown
editing={isEditing}
2020-03-27 10:32:19 -04:00
onSelect={handleAutofillSelect}
2020-01-06 14:48:47 -05:00
onClose={() => setEditing(false)}
fieldName={props.slug}
fieldValue={props.value}
2020-03-19 14:17:56 -04:00
showAllOptionsOnEmpty={props.showAllOptionsOnEmpty}
2020-01-06 14:48:47 -05:00
/>
}
2020-01-07 14:06:42 -05:00
</>
2020-01-06 11:21:44 -05:00
);
};