Skip to content

Instantly share code, notes, and snippets.

@devstojko
Last active April 15, 2023 00:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save devstojko/110700dde70d36c51d08718a3fa04ab5 to your computer and use it in GitHub Desktop.
Save devstojko/110700dde70d36c51d08718a3fa04ab5 to your computer and use it in GitHub Desktop.
Payload Input - ColorPicker
import React, { useCallback } from "react";
import { useField, withCondition } from "payload/components/forms";
import { TextField } from "payload/dist/fields/config/types";
import ColorInput from "../PayloadInput";
import { text } from "payload/dist/fields/validations";
export type Props = Omit<TextField, "type"> & {
path?: string;
};
const ColorPicker: React.FC<Props> = (props) => {
const {
path: pathFromProps,
name,
required,
validate = text,
label,
minLength,
maxLength,
admin: {
placeholder,
readOnly,
style,
className,
width,
description,
condition,
} = {},
} = props;
const path = pathFromProps || name;
const memoizedValidate = useCallback(
(value, options) => {
return validate(value, { ...options, minLength, maxLength, required });
},
[validate, minLength, maxLength, required]
);
const field = useField<string>({
path,
validate: memoizedValidate,
enableDebouncedValue: true,
condition,
});
const { value, showError, setValue, errorMessage } = field;
return (
<ColorInput
path={path}
name={name}
onChange={(e) => {
setValue(e.target.value);
}}
showError={showError}
errorMessage={errorMessage}
required={required}
label={label}
value={value}
placeholder={placeholder}
readOnly={readOnly}
style={style}
className={className}
width={width}
description={description}
inputType="color"
/>
);
};
export default withCondition(ColorPicker);
{
name: 'backgroundColor',
type: 'text',
label: "Background Color",
required: true,
admin: {
width: '50%',
components: {
Field: ColorPicker
}
}
}
import React from "react";
import { TextInputProps } from "payload/dist/admin/components/forms/field-types/Text/Input";
import Error from "payload/dist/admin/components/forms/Error";
import FieldDescription from "payload/dist/admin/components/forms/FieldDescription";
import Label from "payload/dist/admin/components/forms/Label";
const Input: React.FC<
TextInputProps & { inputType: React.HTMLInputTypeAttribute }
> = (props) => {
const {
showError,
errorMessage,
placeholder,
readOnly,
path,
label,
required,
value,
onChange,
description,
style,
className,
width,
inputType,
} = props;
const classes = [
"field-type",
"text",
className,
showError && "error",
readOnly && "read-only",
]
.filter(Boolean)
.join(" ");
return (
<div
className={classes}
style={{
...style,
width,
}}
>
<Error showError={showError} message={errorMessage} />
<Label
htmlFor={`field-${path.replace(/\./gi, "__")}`}
label={label}
required={required}
/>
<input
id={`field-${path.replace(/\./gi, "__")}`}
value={value || ""}
onChange={onChange}
disabled={readOnly}
placeholder={placeholder}
type={inputType}
name={path}
/>
<FieldDescription value={value} description={description} />
</div>
);
};
export default Input;
@kodiyak
Copy link

kodiyak commented Nov 22, 2022

Awesome!!!! πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment