| 1 |
import { __ } from '@wordpress/i18n' |
| 2 |
import React, { useId } from 'react' |
| 3 |
import Select from 'react-select' |
| 4 |
import { useSnippetForm } from '../WithSnippetFormContext' |
| 5 |
import { SNIPPET_TYPE_SCOPES } from '../../../../types/Snippet' |
| 6 |
import { SNIPPET_SCOPE_DESCRIPTIONS, SNIPPET_SCOPE_ICONS, getSnippetType, isCondition } from '../../../../utils/snippets/snippets' |
| 7 |
import type { SnippetCodeScope } from '../../../../types/Snippet' |
| 8 |
import type { SelectOption } from '../../../../types/SelectOption' |
| 9 |
|
| 10 |
export const SnippetLocationInput: React.FC = () => { |
| 11 |
const { snippet, setSnippet, isReadOnly } = useSnippetForm() |
| 12 |
const locationId = useId() |
| 13 |
|
| 14 |
const options: SelectOption<SnippetCodeScope>[] = SNIPPET_TYPE_SCOPES[getSnippetType(snippet)] |
| 15 |
.filter(scope => 'condition' !== scope) |
| 16 |
.map(scope => ({ |
| 17 |
key: scope, |
| 18 |
value: scope, |
| 19 |
label: SNIPPET_SCOPE_DESCRIPTIONS[scope] |
| 20 |
})) |
| 21 |
|
| 22 |
return isCondition(snippet) |
| 23 |
? null |
| 24 |
: <div className="block-form-field"> |
| 25 |
<label htmlFor={locationId}> |
| 26 |
{__('Location', 'code-snippets')} |
| 27 |
</label> |
| 28 |
|
| 29 |
<Select |
| 30 |
inputId={locationId} |
| 31 |
className="code-snippets-select code-snippets-select-location" |
| 32 |
options={options} |
| 33 |
isSearchable={false} |
| 34 |
isDisabled={isReadOnly} |
| 35 |
styles={{ |
| 36 |
menu: provided => ({ ...provided, zIndex: 9999 }), |
| 37 |
input: provided => ({ ...provided, ':focus': { boxShadow: 'none' } }) |
| 38 |
}} |
| 39 |
value={options.find(option => option.value === snippet.scope)} |
| 40 |
formatOptionLabel={({ label, value }) => |
| 41 |
<> |
| 42 |
<span className={`dashicons dashicons-${SNIPPET_SCOPE_ICONS[value]}`} aria-hidden="true"></span>{` ${label}`} |
| 43 |
</> |
| 44 |
} |
| 45 |
onChange={option => |
| 46 |
option?.value && setSnippet(previous => ({ ...previous, scope: option.value }))} |
| 47 |
/> |
| 48 |
</div> |
| 49 |
} |
| 50 |
|