PluginProbe
Code Snippets / trunk
Code Snippets vtrunk
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / js / components / EditMenu / SnippetForm / fields / SnippetLocationInput.tsx

SnippetLocationInput.tsx in Code Snippets trunk, at js/components/EditMenu/SnippetForm/fields/SnippetLocationInput.tsx

50 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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