| 1 |
import { TextControl, ToggleControl } from '@wordpress/components'; |
| 2 |
import { __ } from '@wordpress/i18n'; |
| 3 |
|
| 4 |
/** |
| 5 |
* Get field attributes to render Gutenberg component. |
| 6 |
* |
| 7 |
* @param {Object} attributes - block attributes. |
| 8 |
* |
| 9 |
* @return {Object} attributes list. |
| 10 |
*/ |
| 11 |
export function getFieldAttributes(attributes) { |
| 12 |
const allowedAttributes = [ |
| 13 |
'id', |
| 14 |
'placeholder', |
| 15 |
'default', |
| 16 |
'min', |
| 17 |
'max', |
| 18 |
'step', |
| 19 |
'multiple', |
| 20 |
'required', |
| 21 |
'for', |
| 22 |
]; |
| 23 |
const result = { |
| 24 |
// prevent input focus. |
| 25 |
tabIndex: -1, |
| 26 |
onFocus(e) { |
| 27 |
e.target.blur(); |
| 28 |
}, |
| 29 |
}; |
| 30 |
|
| 31 |
Object.keys(attributes).forEach((k) => { |
| 32 |
let val = attributes[k]; |
| 33 |
|
| 34 |
if (typeof val !== 'undefined') { |
| 35 |
if (k === 'slug') { |
| 36 |
k = 'id'; |
| 37 |
} |
| 38 |
|
| 39 |
if (allowedAttributes.indexOf(k) !== -1) { |
| 40 |
// boolean value. |
| 41 |
if (typeof val === 'boolean') { |
| 42 |
if (val) { |
| 43 |
val = k; |
| 44 |
} else { |
| 45 |
val = false; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
// default attribute. |
| 50 |
if (k === 'default') { |
| 51 |
k = 'value'; |
| 52 |
} |
| 53 |
|
| 54 |
if (val !== false) { |
| 55 |
result[k] = val; |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
}); |
| 60 |
|
| 61 |
return result; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Field Default Settings Class. |
| 66 |
* |
| 67 |
* @param props |
| 68 |
*/ |
| 69 |
export function FieldDefaultSettings(props) { |
| 70 |
const { |
| 71 |
attributes, |
| 72 |
setAttributes, |
| 73 |
|
| 74 |
hideLabelCustom, |
| 75 |
hideDescriptionCustom, |
| 76 |
requiredCustom, |
| 77 |
placeholderCustom, |
| 78 |
defaultCustom, |
| 79 |
slugCustom, |
| 80 |
} = props; |
| 81 |
|
| 82 |
const { |
| 83 |
slug, |
| 84 |
label, |
| 85 |
description, |
| 86 |
hideLabel, |
| 87 |
hideDescription, |
| 88 |
required, |
| 89 |
placeholder, |
| 90 |
default: defaultVal, |
| 91 |
} = attributes; |
| 92 |
|
| 93 |
const hideLabelControl = hideLabelCustom || ( |
| 94 |
<ToggleControl |
| 95 |
label={__('Hide Label', 'ghostkit')} |
| 96 |
checked={hideLabel} |
| 97 |
onChange={() => setAttributes({ hideLabel: !hideLabel })} |
| 98 |
__nextHasNoMarginBottom |
| 99 |
/> |
| 100 |
); |
| 101 |
|
| 102 |
const hideDescriptionControl = hideDescriptionCustom || ( |
| 103 |
<ToggleControl |
| 104 |
label={__('Hide Description', 'ghostkit')} |
| 105 |
checked={hideDescription} |
| 106 |
onChange={() => |
| 107 |
setAttributes({ hideDescription: !hideDescription }) |
| 108 |
} |
| 109 |
__nextHasNoMarginBottom |
| 110 |
/> |
| 111 |
); |
| 112 |
|
| 113 |
return ( |
| 114 |
<> |
| 115 |
{slugCustom || ( |
| 116 |
<TextControl |
| 117 |
label={__('Slug', 'ghostkit')} |
| 118 |
help={__( |
| 119 |
'Slug is used in form field [name] attribute.', |
| 120 |
'ghostkit' |
| 121 |
)} |
| 122 |
value={slug} |
| 123 |
onChange={() => {}} |
| 124 |
readOnly |
| 125 |
__next40pxDefaultSize |
| 126 |
__nextHasNoMarginBottom |
| 127 |
/> |
| 128 |
)} |
| 129 |
{placeholderCustom || ( |
| 130 |
<TextControl |
| 131 |
label={__('Placeholder', 'ghostkit')} |
| 132 |
value={placeholder} |
| 133 |
onChange={(val) => setAttributes({ placeholder: val })} |
| 134 |
__next40pxDefaultSize |
| 135 |
__nextHasNoMarginBottom |
| 136 |
/> |
| 137 |
)} |
| 138 |
{defaultCustom || ( |
| 139 |
<TextControl |
| 140 |
label={__('Default', 'ghostkit')} |
| 141 |
value={defaultVal} |
| 142 |
onChange={(val) => setAttributes({ default: val })} |
| 143 |
__next40pxDefaultSize |
| 144 |
__nextHasNoMarginBottom |
| 145 |
/> |
| 146 |
)} |
| 147 |
{requiredCustom || ( |
| 148 |
<ToggleControl |
| 149 |
label={__('Required', 'ghostkit')} |
| 150 |
checked={required} |
| 151 |
onChange={() => setAttributes({ required: !required })} |
| 152 |
__nextHasNoMarginBottom |
| 153 |
/> |
| 154 |
)} |
| 155 |
{label ? hideLabelControl : ''} |
| 156 |
{description ? hideDescriptionControl : ''} |
| 157 |
</> |
| 158 |
); |
| 159 |
} |
| 160 |
|