Skip to content

Commit

Permalink
update url field based on changed default value
Browse files Browse the repository at this point in the history
  • Loading branch information
breeg554 committed Oct 15, 2024
1 parent 13afcf9 commit 88672ae
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions apps/web-remix/app/components/form/fields/text.field.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { forwardRef, useRef } from 'react';
import React, { forwardRef, useEffect, useRef } from 'react';
import { RefreshCcw } from 'lucide-react';
import { useControlField } from 'remix-validated-form';
import type { ValidationBehaviorOptions } from 'remix-validated-form/browser/internal/getInputProps';

import { useFieldContext } from '~/components/form/fields/field.context';
Expand Down Expand Up @@ -30,12 +31,13 @@ export const TextInputField = forwardRef<
aria-errormessage={error ? `${name}-error` : undefined}
aria-label={name}
autoComplete={name}
{...props}
{...getInputProps()}
{...props}
/>
);
});
TextInputField.displayName = 'TextInputField';

export const PasswordInputField = forwardRef<
HTMLInputElement,
Partial<TextInputFieldProps>
Expand All @@ -47,17 +49,41 @@ PasswordInputField.displayName = 'PasswordInputField';

export function ResettableTextInputField({
label,
defaultValue,
...props
}: Partial<TextInputFieldProps> & { label: string }) {
const inputRef = useRef<HTMLInputElement | null>(null);
const inputRef = useRef<HTMLInputElement>(null);
const { name } = useFieldContext({
validationBehavior: {
initial: 'onChange',
whenTouched: 'onChange',
whenSubmitted: 'onChange',
},
});

const [value, setValue] = useControlField<string | undefined>(name);

useEffect(() => {
if (typeof defaultValue === 'string') {
updateAndValidate(defaultValue);
}
}, [defaultValue]);

const onReset = () => {
if (inputRef.current) {
inputRef.current.value = (props.defaultValue ?? '') as string;
if (typeof defaultValue === 'string') {
updateAndValidate(defaultValue);
}
};

const canReset = !!props.defaultValue && !props.readOnly && !props.disabled;
const updateAndValidate = (newValue: string) => {
setValue(newValue);
};

const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setValue(e.target.value);
};

const canReset = !!defaultValue && !props.readOnly && !props.disabled;

return (
<>
Expand All @@ -68,7 +94,12 @@ export function ResettableTextInputField({
{canReset ? <ResettableFieldResetButton onClick={onReset} /> : null}
</div>
</FieldLabel>
<TextInputField ref={inputRef} {...props} />
<TextInputField
ref={inputRef}
onChange={onChange}
value={value}
{...props}
/>
</>
);
}
Expand Down

0 comments on commit 88672ae

Please sign in to comment.