[offers][feat] Add form validation for typeaheads (#508)
parent
35a06c1185
commit
32bbb45f4e
@ -0,0 +1,37 @@
|
||||
import type { ComponentProps } from 'react';
|
||||
import { useFormContext, useWatch } from 'react-hook-form';
|
||||
|
||||
import CitiesTypeahead from '~/components/shared/CitiesTypeahead';
|
||||
|
||||
type Props = Omit<
|
||||
ComponentProps<typeof CitiesTypeahead>,
|
||||
'onSelect' | 'value'
|
||||
> & {
|
||||
names: { label: string; value: string };
|
||||
};
|
||||
|
||||
export default function FormCitiesTypeahead({ names, ...props }: Props) {
|
||||
const { setValue } = useFormContext();
|
||||
const watchCityId = useWatch({
|
||||
name: names.value,
|
||||
});
|
||||
const watchCityName = useWatch({
|
||||
name: names.label,
|
||||
});
|
||||
|
||||
return (
|
||||
<CitiesTypeahead
|
||||
label="Location"
|
||||
{...props}
|
||||
value={{
|
||||
id: watchCityId,
|
||||
label: watchCityName,
|
||||
value: watchCityId,
|
||||
}}
|
||||
onSelect={(option) => {
|
||||
setValue(names.value, option?.value);
|
||||
setValue(names.label, option?.value);
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
import type { ComponentProps } from 'react';
|
||||
import { useFormContext, useWatch } from 'react-hook-form';
|
||||
|
||||
import CompaniesTypeahead from '~/components/shared/CompaniesTypeahead';
|
||||
|
||||
type Props = Omit<
|
||||
ComponentProps<typeof CompaniesTypeahead>,
|
||||
'onSelect' | 'value'
|
||||
> & {
|
||||
names: { label: string; value: string };
|
||||
};
|
||||
|
||||
export default function FormCompaniesTypeahead({ names, ...props }: Props) {
|
||||
const { setValue } = useFormContext();
|
||||
const watchCompanyId = useWatch({
|
||||
name: names.value,
|
||||
});
|
||||
const watchCompanyName = useWatch({
|
||||
name: names.label,
|
||||
});
|
||||
|
||||
return (
|
||||
<CompaniesTypeahead
|
||||
{...props}
|
||||
value={{
|
||||
id: watchCompanyId,
|
||||
label: watchCompanyName,
|
||||
value: watchCompanyId,
|
||||
}}
|
||||
onSelect={(option) => {
|
||||
setValue(names.value, option?.value);
|
||||
setValue(names.label, option?.value);
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
import type { ComponentProps } from 'react';
|
||||
import { useFormContext, useWatch } from 'react-hook-form';
|
||||
|
||||
import type { JobTitleType } from '~/components/shared/JobTitles';
|
||||
import { getLabelForJobTitleType } from '~/components/shared/JobTitles';
|
||||
import JobTitlesTypeahead from '~/components/shared/JobTitlesTypahead';
|
||||
|
||||
type Props = Omit<
|
||||
ComponentProps<typeof JobTitlesTypeahead>,
|
||||
'onSelect' | 'value'
|
||||
> & {
|
||||
name: string;
|
||||
};
|
||||
|
||||
export default function FormJobTitlesTypeahead({ name, ...props }: Props) {
|
||||
const { setValue } = useFormContext();
|
||||
const watchJobTitle = useWatch({
|
||||
name,
|
||||
});
|
||||
|
||||
return (
|
||||
<JobTitlesTypeahead
|
||||
{...props}
|
||||
value={{
|
||||
id: watchJobTitle,
|
||||
label: getLabelForJobTitleType(watchJobTitle as JobTitleType),
|
||||
value: watchJobTitle,
|
||||
}}
|
||||
onSelect={(option) => {
|
||||
setValue(name, option?.value);
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
Loading…
Reference in new issue