-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '447-create-input-component-and-replace-local-storage-in…
…put-with-it' into 449-create-toggle-component-and-replace-local-storage-toggle-with-it
- Loading branch information
Showing
7 changed files
with
190 additions
and
165 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import React, { useCallback, useEffect, useState } from 'react' | ||
import { InputDescription } from './input-description.js' | ||
import { InputLabel } from './input-label.js' | ||
|
||
export interface InputProps extends Omit<React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>, 'onChange'> { | ||
label: string | ||
placeholder?: string | ||
value: string | ||
validationFn?(value: string): Error | null | ||
resetKey: number | ||
description?: string | ||
preSaveFormat?(value: string): any | ||
onChange(value: string): void | ||
} | ||
|
||
export default ({ resetKey, onChange, label, placeholder, validationFn, value, description, preSaveFormat, ...props }: InputProps): JSX.Element => { | ||
const [internalValue, setInternalValue] = useState(value) | ||
const [error, setError] = useState<null | Error>(null) | ||
|
||
useEffect(() => { | ||
setInternalValue(value) | ||
}, [resetKey, value]) | ||
|
||
const updateValue = useCallback((e: React.ChangeEvent<HTMLTextAreaElement>) => { | ||
setInternalValue(e.target.value) | ||
}, []) | ||
|
||
const saveValue = useCallback((e: React.ChangeEvent<HTMLTextAreaElement>) => { | ||
const newValue = e.target.value | ||
try { | ||
const err = validationFn?.(newValue) | ||
if (err != null) { | ||
throw err | ||
} | ||
setInternalValue(newValue) | ||
|
||
onChange(preSaveFormat?.(newValue) ?? newValue) | ||
setError(null) | ||
} catch (err) { | ||
setError(err as Error) | ||
} | ||
}, [onChange, preSaveFormat, validationFn]) | ||
|
||
props = { | ||
...props, | ||
className: `${props.className ?? ''} flex-column items-start` | ||
} | ||
|
||
return ( | ||
<div {...props}> | ||
<InputLabel label={label} /> | ||
<InputDescription description={description} /> | ||
<textarea | ||
className='input-reset ba br2 b--light-silver code lh-copy black-80 bg-white pa2 w-100 mt2' | ||
id={label} | ||
name={label} | ||
placeholder={placeholder} | ||
value={internalValue} | ||
onChange={updateValue} | ||
onBlur={saveValue} | ||
/> | ||
{error != null && <span className='db lh-copy red pt1 tr f6 w-100'>⬑ {error.message}</span>} | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export const convertUrlArrayToInput = (urls: string[]): string => { | ||
return urls.join('\n') | ||
} | ||
|
||
export const convertUrlInputToArray = (newlineDelimitedString: string): string[] => { | ||
return newlineDelimitedString.length > 0 ? newlineDelimitedString.split('\n').map((u) => u.trim()) : [] | ||
} | ||
|
||
export const convertDnsResolverObjectToInput = (dnsResolvers: Record<string, string>): string => { | ||
return Object.entries(dnsResolvers).map(([key, url]) => `${key} ${url}`).join('\n') | ||
} | ||
|
||
export const convertDnsResolverInputToObject = (dnsResolverInput: string): Record<string, string> => { | ||
return dnsResolverInput.split('\n').map((u) => u.trim().split(' ')).reduce((acc, [key, url]) => { | ||
acc[key] = url | ||
return acc | ||
}, {}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.