mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-31 20:09:11 +00:00
refactor: improve template input
This commit is contained in:
committed by
Carlos Valente
parent
5fb30c75c4
commit
6b2e45e7de
@@ -380,13 +380,13 @@ export default function AutomationForm(props: AutomationFormProps) {
|
|||||||
<Panel.Error>{rowErrors?.address?.message}</Panel.Error>
|
<Panel.Error>{rowErrors?.address?.message}</Panel.Error>
|
||||||
</label>
|
</label>
|
||||||
<label>
|
<label>
|
||||||
Parameters
|
Arguments
|
||||||
<TemplateInput
|
<TemplateInput
|
||||||
{...register(`outputs.${index}.args`)}
|
{...register(`outputs.${index}.args`)}
|
||||||
|
value={output.args}
|
||||||
variant='ontime-filled'
|
variant='ontime-filled'
|
||||||
size='sm'
|
size='sm'
|
||||||
placeholder='1'
|
placeholder='1'
|
||||||
autoComplete='off'
|
|
||||||
/>
|
/>
|
||||||
<Panel.Error>{rowErrors?.args?.message}</Panel.Error>
|
<Panel.Error>{rowErrors?.args?.message}</Panel.Error>
|
||||||
</label>
|
</label>
|
||||||
|
|||||||
+4
-6
@@ -33,13 +33,11 @@ const TemplateInput = forwardRef(function TemplateInput(props: TemplateInputProp
|
|||||||
|
|
||||||
if (event.target.value.endsWith('{')) {
|
if (event.target.value.endsWith('{')) {
|
||||||
setShowSuggestions(true);
|
setShowSuggestions(true);
|
||||||
|
setSuggestions(updateSuggestions(event.target.value));
|
||||||
} else if (event.target.value === '' || event.target.value.endsWith('}}')) {
|
} else if (event.target.value === '' || event.target.value.endsWith('}}')) {
|
||||||
setShowSuggestions(false);
|
setShowSuggestions(false);
|
||||||
}
|
} else if (showSuggestions) {
|
||||||
|
setSuggestions(updateSuggestions(event.target.value));
|
||||||
if (showSuggestions) {
|
|
||||||
const suggestions = updateSuggestions(event.target.value);
|
|
||||||
setSuggestions(suggestions);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onChange?.(event);
|
onChange?.(event);
|
||||||
@@ -55,7 +53,7 @@ const TemplateInput = forwardRef(function TemplateInput(props: TemplateInputProp
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={style.wrapper} ref={mergeRefs(localRef, ref)}>
|
<div className={style.wrapper} ref={mergeRefs(localRef, ref)}>
|
||||||
<Input {...rest} value={inputValue} onChange={handleInputChange} autoComplete='off' autoCorrect='off' />
|
<Input value={inputValue} {...rest} onChange={handleInputChange} autoComplete='off' autoCorrect='off' />
|
||||||
{showSuggestions && suggestions.length > 0 && (
|
{showSuggestions && suggestions.length > 0 && (
|
||||||
<ul className={style.suggestions}>
|
<ul className={style.suggestions}>
|
||||||
{suggestions.map((suggestion) => (
|
{suggestions.map((suggestion) => (
|
||||||
|
|||||||
+2
-4
@@ -6,10 +6,8 @@ describe('matchRemaining()', () => {
|
|||||||
expect(matchRemaining('{{{hum', '{{human}}')).toBe('an}}');
|
expect(matchRemaining('{{{hum', '{{human}}')).toBe('an}}');
|
||||||
expect(matchRemaining('send {', '{{human}}')).toBe('{human}}');
|
expect(matchRemaining('send {', '{{human}}')).toBe('{human}}');
|
||||||
|
|
||||||
// we should be able to match the following
|
expect(matchRemaining('{', '{{human}}')).toBe('{human}}');
|
||||||
// however, the current implementation only needs to deal with strings that start with {{
|
expect(matchRemaining('{{', '{{human}}')).toBe('human}}');
|
||||||
// expect(matchRemaining('{', '{{human}}')).toBe('{human}}');
|
|
||||||
// expect(matchRemaining('{{', '{{human}}')).toBe('human}}');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return an empty string if there are no matches or if it is complete', () => {
|
it('should return an empty string if there are no matches or if it is complete', () => {
|
||||||
|
|||||||
+10
@@ -83,6 +83,16 @@ export function matchRemaining(a: string, b: string) {
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// naive match assuming that a template will start with {{
|
||||||
|
if (a.endsWith('{{') && b.startsWith('{{')) {
|
||||||
|
return b.substring(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// naive match assuming that a template will start with {
|
||||||
|
if (a.endsWith('{') && b.startsWith('{{')) {
|
||||||
|
return b.substring(1);
|
||||||
|
}
|
||||||
|
|
||||||
for (let i = 0; i < b.length; i++) {
|
for (let i = 0; i < b.length; i++) {
|
||||||
const searchString = b.substring(0, i + 1);
|
const searchString = b.substring(0, i + 1);
|
||||||
if (a.endsWith(searchString)) {
|
if (a.endsWith(searchString)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user