-
-
Save HendrikRoth/744ec88d7d9f7cd0dfbeafece5554f0b to your computer and use it in GitHub Desktop.
Payload Input - ColorPicker
This file contains hidden or 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
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); |
This file contains hidden or 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
{ | |
name: 'backgroundColor', | |
type: 'text', | |
label: "Background Color", | |
required: true, | |
admin: { | |
width: '50%', | |
components: { | |
Field: ColorPicker | |
} | |
} | |
} |
This file contains hidden or 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
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment