MCPcopy Create free account
hub / github.com/pgadmin-org/pgadmin4 / HOCComponent

Function HOCComponent

web/pgadmin/static/js/helpers/withColorPicker.js:19–141  ·  view source on GitHub ↗
({value, currObj, onChange, onSave, options, ...props})

Source from the content-addressed store, hash-verified

17export function withColorPicker(Component) {
18
19 const HOCComponent = ({value, currObj, onChange, onSave, options, ...props})=>{
20 const pickrOptions = {
21 showPalette: true,
22 allowEmpty: true,
23 allowSave: false,
24 colorFormat: 'HEX',
25 defaultColor: null,
26 position: 'right-middle',
27 clearText: gettext('Clear'),
28 ...options,
29 };
30 const eleRef = useRef();
31 const pickrObj = useRef();
32 const onChangeRef = useRef();
33 const onSaveRef = useRef();
34
35 onChangeRef.current = onChange;
36 onSaveRef.current = onSave;
37
38 const setColor = (newVal, silent) => {
39 pickrObj.current?.setColor((_.isUndefined(newVal) || newVal == '') ? pickrOptions.defaultColor : newVal, silent);
40 };
41
42 const destroyPickr = () => {
43 if (pickrObj.current) {
44 pickrObj.current.destroy();
45 pickrObj.current = null;
46 }
47 };
48
49 const initPickr = () => {
50 /* pickr does not have way to update options, need to
51 destroy and recreate pickr to reflect options */
52 destroyPickr();
53
54 pickrObj.current = new Pickr({
55 el: eleRef.current,
56 useAsButton: true,
57 theme: 'monolith',
58 swatches: [
59 '#000', '#666', '#ccc', '#fff', '#f90', '#ff0', '#0f0',
60 '#f0f', '#f4cccc', '#fce5cd', '#d0e0e3', '#cfe2f3', '#ead1dc', '#ea9999',
61 '#b6d7a8', '#a2c4c9', '#d5a6bd', '#e06666', '#93c47d', '#76a5af', '#c27ba0',
62 '#f1c232', '#6aa84f', '#45818e', '#a64d79', '#bf9000', '#0c343d', '#4c1130',
63 ],
64 position: pickrOptions.position,
65 strings: {
66 clear: pickrOptions.clearText,
67 },
68 components: {
69 palette: pickrOptions.showPalette,
70 preview: true,
71 hue: pickrOptions.showPalette,
72 interaction: {
73 clear: pickrOptions.allowEmpty,
74 defaultRepresentation: pickrOptions.colorFormat,
75 disabled: pickrOptions.disabled,
76 save: pickrOptions.allowSave,

Callers

nothing calls this directly

Calls 4

gettextFunction · 0.85
initPickrFunction · 0.85
destroyPickrFunction · 0.85
setColorFunction · 0.85

Tested by

no test coverage detected