Skip to content

Commit

Permalink
Merge branch 'feat/form-onchange' of github.com:payloadcms/payload
Browse files Browse the repository at this point in the history
  • Loading branch information
jmikrut committed Nov 30, 2021
2 parents 6a33abb + 925a33e commit 1d25c7f
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 14 deletions.
26 changes: 26 additions & 0 deletions demo/collections/Images.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { CollectionConfig } from '../../src/collections/config/types';

const Images: CollectionConfig = {
slug: 'images',
labels: {
singular: 'Image',
plural: 'Images',
},
fields: [
{
name: 'upload1',
label: 'Upload 1',
type: 'upload',
relationTo: 'media',
},
{
name: 'upload2',
label: 'Upload 2',
type: 'upload',
relationTo: 'media',
},
],
timestamps: true,
};

export default Images;
4 changes: 2 additions & 2 deletions src/admin/components/forms/field-types/Select/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import { Value as ReactSelectValue } from '../../../elements/ReactSelect/types';
import './index.scss';

export type SelectInputProps = Omit<SelectField, 'type' | 'value' | 'options'> & {
showError: boolean
showError?: boolean
errorMessage?: string
readOnly?: boolean
path?: string
path: string
required?: boolean
value?: string | string[]
description?: Description
Expand Down
1 change: 1 addition & 0 deletions src/admin/components/forms/field-types/Select/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const Select: React.FC<Props> = (props) => {

return (
<SelectInput
path={path}
onChange={onChange}
value={value as string | string[]}
name={name}
Expand Down
4 changes: 2 additions & 2 deletions src/admin/components/forms/field-types/Text/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import { Description } from '../../FieldDescription/types';
import './index.scss';

export type TextInputProps = Omit<TextField, 'type'> & {
showError: boolean
showError?: boolean
errorMessage?: string
readOnly?: boolean
path?: string
path: string
required?: boolean
value?: string
description?: Description
Expand Down
1 change: 1 addition & 0 deletions src/admin/components/forms/field-types/Text/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const Text: React.FC<Props> = (props) => {

return (
<TextInput
path={path}
name={name}
onChange={(e) => {
setValue(e.target.value);
Expand Down
4 changes: 2 additions & 2 deletions src/admin/components/forms/field-types/Textarea/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { Description } from '../../FieldDescription/types';
import './index.scss';

export type TextAreaInputProps = Omit<TextareaField, 'type'> & {
showError: boolean
showError?: boolean
errorMessage?: string
readOnly?: boolean
path?: string
path: string
required?: boolean
value?: string
description?: Description
Expand Down
1 change: 1 addition & 0 deletions src/admin/components/forms/field-types/Textarea/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const Textarea: React.FC<Props> = (props) => {

return (
<TextareaInput
path={path}
name={name}
onChange={(e) => {
setValue(e.target.value);
Expand Down
5 changes: 2 additions & 3 deletions src/admin/components/forms/field-types/Upload/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import './index.scss';
const baseClass = 'upload';

export type UploadInputProps = Omit<UploadField, 'type'> & {
showError: boolean
showError?: boolean
errorMessage?: string
readOnly?: boolean
path?: string
path: string
required?: boolean
value?: string
description?: Description
Expand Down Expand Up @@ -73,7 +73,6 @@ const UploadInput: React.FC<UploadInputProps> = (props) => {
if (typeof value === 'string' && value !== '') {
const fetchFile = async () => {
const response = await fetch(`${serverURL}${api}/${relationTo}/${value}`);

if (response.ok) {
const json = await response.json();
setFile(json);
Expand Down
9 changes: 4 additions & 5 deletions src/admin/components/forms/field-types/Upload/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const Upload: React.FC<Props> = (props) => {
} = useConfig();

const {
path: pathFromProps,
path,
name,
required,
admin: {
Expand All @@ -36,14 +36,12 @@ const Upload: React.FC<Props> = (props) => {

const collection = collections.find((coll) => coll.slug === relationTo);

const path = pathFromProps || name;

const memoizedValidate = useCallback((value) => {
const validationResult = validate(value, { required });
return validationResult;
}, [validate, required]);

const fieldType = useField({
const field = useField({
path,
validate: memoizedValidate,
condition,
Expand All @@ -54,7 +52,7 @@ const Upload: React.FC<Props> = (props) => {
showError,
setValue,
errorMessage,
} = fieldType;
} = field;

const onChange = useCallback((incomingValue) => {
const incomingID = incomingValue?.id || incomingValue;
Expand All @@ -66,6 +64,7 @@ const Upload: React.FC<Props> = (props) => {
if (collection.upload) {
return (
<UploadInput
path={path}
value={value as string}
onChange={onChange}
description={description}
Expand Down

0 comments on commit 1d25c7f

Please sign in to comment.