@shopify/cli-kit 3.44.1 → 3.45.0-pre.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -7
- package/assets/cli-ruby/lib/project_types/extension/messages/messages.rb +0 -2
- package/assets/cli-ruby/lib/project_types/theme/commands/pull.rb +6 -0
- package/assets/cli-ruby/lib/project_types/theme/commands/push.rb +6 -0
- package/assets/cli-ruby/lib/shopify_cli/constants.rb +1 -0
- package/assets/cli-ruby/lib/shopify_cli/environment.rb +4 -0
- package/assets/cli-ruby/lib/shopify_cli/theme/dev_server/proxy.rb +3 -0
- package/assets/cli-ruby/lib/shopify_cli/theme/extension/dev_server.rb +3 -3
- package/assets/cli-ruby/lib/shopify_cli/theme/extension/host_theme.rb +4 -4
- package/assets/cli-ruby/lib/shopify_cli/theme/extension/syncer/extension_serve_job.rb +4 -6
- package/assets/cli-ruby/lib/shopify_cli/theme/extension/ui/host_theme_raw_progress_bar.rb +40 -0
- package/assets/cli-ruby/vendor/deps/cli-ui/lib/cli/ui/progress_plain.rb +50 -0
- package/assets/cli-ruby/vendor/deps/cli-ui/lib/cli/ui.rb +15 -14
- package/dist/private/node/api/graphql.js +12 -23
- package/dist/private/node/api/graphql.js.map +1 -1
- package/dist/private/node/api.d.ts +1 -1
- package/dist/private/node/api.js +28 -9
- package/dist/private/node/api.js.map +1 -1
- package/dist/private/node/constants.d.ts +0 -1
- package/dist/private/node/constants.js +0 -1
- package/dist/private/node/constants.js.map +1 -1
- package/dist/private/node/testing/ui.d.ts +11 -0
- package/dist/private/node/testing/ui.js +15 -1
- package/dist/private/node/testing/ui.js.map +1 -1
- package/dist/private/node/ui/alert.d.ts +5 -1
- package/dist/private/node/ui/alert.js +2 -2
- package/dist/private/node/ui/alert.js.map +1 -1
- package/dist/private/node/ui/components/AutocompletePrompt.js +38 -12
- package/dist/private/node/ui/components/AutocompletePrompt.js.map +1 -1
- package/dist/private/node/ui/components/AutocompletePrompt.test.js +56 -36
- package/dist/private/node/ui/components/AutocompletePrompt.test.js.map +1 -1
- package/dist/private/node/ui/components/ConcurrentOutput.d.ts +5 -1
- package/dist/private/node/ui/components/ConcurrentOutput.js +15 -13
- package/dist/private/node/ui/components/ConcurrentOutput.js.map +1 -1
- package/dist/private/node/ui/components/ConcurrentOutput.test.js +20 -11
- package/dist/private/node/ui/components/ConcurrentOutput.test.js.map +1 -1
- package/dist/private/node/ui/components/FullScreen.js +1 -1
- package/dist/private/node/ui/components/FullScreen.js.map +1 -1
- package/dist/private/node/ui/components/SelectInput.d.ts +7 -2
- package/dist/private/node/ui/components/SelectInput.js +73 -60
- package/dist/private/node/ui/components/SelectInput.js.map +1 -1
- package/dist/private/node/ui/components/SelectInput.test.js +72 -2
- package/dist/private/node/ui/components/SelectInput.test.js.map +1 -1
- package/dist/private/node/ui/components/SelectPrompt.js +38 -12
- package/dist/private/node/ui/components/SelectPrompt.js.map +1 -1
- package/dist/private/node/ui/components/SelectPrompt.test.js +52 -1
- package/dist/private/node/ui/components/SelectPrompt.test.js.map +1 -1
- package/dist/private/node/ui/components/Tasks.js +9 -1
- package/dist/private/node/ui/components/Tasks.js.map +1 -1
- package/dist/private/node/ui/components/TextAnimation.js +4 -4
- package/dist/private/node/ui/components/TextAnimation.js.map +1 -1
- package/dist/private/node/ui/components/TextPrompt.js +4 -4
- package/dist/private/node/ui/components/TextPrompt.js.map +1 -1
- package/dist/private/node/ui/hooks/use-layout.d.ts +6 -0
- package/dist/private/node/ui/hooks/use-layout.js +31 -12
- package/dist/private/node/ui/hooks/use-layout.js.map +1 -1
- package/dist/private/node/ui.d.ts +11 -3
- package/dist/private/node/ui.js +14 -10
- package/dist/private/node/ui.js.map +1 -1
- package/dist/public/common/object.d.ts +1 -1
- package/dist/public/common/object.js +1 -1
- package/dist/public/common/object.js.map +1 -1
- package/dist/public/common/version.d.ts +1 -1
- package/dist/public/common/version.js +1 -1
- package/dist/public/common/version.js.map +1 -1
- package/dist/public/node/base-command.d.ts +1 -4
- package/dist/public/node/base-command.js +17 -19
- package/dist/public/node/base-command.js.map +1 -1
- package/dist/public/node/context/local.d.ts +0 -7
- package/dist/public/node/context/local.js +0 -9
- package/dist/public/node/context/local.js.map +1 -1
- package/dist/public/node/environments.d.ts +7 -8
- package/dist/public/node/environments.js +23 -25
- package/dist/public/node/environments.js.map +1 -1
- package/dist/public/node/node-package-manager.d.ts +1 -1
- package/dist/public/node/node-package-manager.js.map +1 -1
- package/dist/public/node/output.d.ts +1 -1
- package/dist/public/node/output.js.map +1 -1
- package/dist/public/node/path.js +1 -1
- package/dist/public/node/path.js.map +1 -1
- package/dist/public/node/ruby.d.ts +1 -0
- package/dist/public/node/ruby.js +35 -15
- package/dist/public/node/ruby.js.map +1 -1
- package/dist/public/node/themes/theme-manager.d.ts +1 -1
- package/dist/public/node/themes/theme-manager.js.map +1 -1
- package/dist/public/node/ui.d.ts +182 -121
- package/dist/public/node/ui.js +172 -120
- package/dist/public/node/ui.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +20 -18
- package/dist/private/node/ui/components/TextWithBackground.d.ts +0 -12
- package/dist/private/node/ui/components/TextWithBackground.js +0 -39
- package/dist/private/node/ui/components/TextWithBackground.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ConcurrentOutput.js","sourceRoot":"","sources":["../../../../../src/private/node/ui/components/ConcurrentOutput.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,kBAAkB,EAAC,MAAM,yBAAyB,CAAA;AAE1D,OAAO,kBAAkB,MAAM,mCAAmC,CAAA;AAElE,OAAO,EAAC,WAAW,EAAC,MAAM,aAAa,CAAA;AACvC,OAAO,KAAK,EAAE,EAAoB,WAAW,EAAE,QAAQ,EAAC,MAAM,OAAO,CAAA;AACrE,OAAO,EAAC,GAAG,EAAO,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAC,MAAM,KAAK,CAAA;AACpD,OAAO,SAAS,MAAM,YA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,CAAC,QAAQ,CAAC,CAAC,CAAC,CACjB,oBAAC,GAAG,IAAC,SAAS,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC;gBAC5B,oBAAC,IAAI,QAAE,MAAM,CAAC,QAAQ,CAAQ,CAC1B,CACP,CAAC,CAAC,CAAC,IAAI,CACJ,CACP,CAAC,CAAC,CAAC,IAAI,CACP,CACJ,CAAA;AACH,CAAC,CAAA;AAED,OAAO,EAAC,gBAAgB,EAAC,CAAA","sourcesContent":["import {TextWithBackground} from './TextWithBackground.js'\nimport {OutputProcess} from '../../../../public/node/output.js'\nimport useAsyncAndUnmount from '../hooks/use-async-and-unmount.js'\nimport {AbortController} from '../../../../public/node/abort.js'\nimport {handleCtrlC} from '../../ui.js'\nimport React, {FunctionComponent, useCallback, useState} from 'react'\nimport {Box, Key, Static, Text, useInput} from 'ink'\nimport stripAnsi from 'strip-ansi'\nimport treeKill from 'tree-kill'\nimport {Writable} from 'stream'\n\nexport type WritableStream = (process: OutputProcess, index: number) => Writable\n\nexport interface ConcurrentOutputProps {\n processes: OutputProcess[]\n abortController: AbortController\n showTimestamps?: boolean\n onInput?: (input: string, key: Key, exit: () => void) => void\n footer?: {\n title: string\n subTitle?: string\n }\n}\ninterface Chunk {\n color: string\n prefix: string\n lines: string[]\n}\n\n/**\n * Renders output from concurrent processes to the terminal.\n * Output will be divided in a three column layout\n * with the left column containing the timestamp,\n * the right column containing the output,\n * and the middle column containing the process prefix.\n * Every process will be rendered with a different color, up to 4 colors.\n *\n * For example running `shopify app dev`:\n *\n * ```shell\n * 2022-10-10 13:11:03 | backend | npm\n * 2022-10-10 13:11:03 | backend | WARN ignoring workspace config at ...\n * 2022-10-10 13:11:03 | backend |\n * 2022-10-10 13:11:03 | backend |\n * 2022-10-10 13:11:03 | backend | > shopify-app-template-node@0.1.0 dev\n * 2022-10-10 13:11:03 | backend | > cross-env NODE_ENV=development nodemon backend/index.js --watch ./backend\n * 2022-10-10 13:11:03 | backend |\n * 2022-10-10 13:11:03 | backend |\n * 2022-10-10 13:11:03 | frontend |\n * 2022-10-10 13:11:03 | frontend | > starter-react-frontend-app@0.1.0 dev\n * 2022-10-10 13:11:03 | frontend | > cross-env NODE_ENV=development node vite-server.js\n * 2022-10-10 13:11:03 | frontend |\n * 2022-10-10 13:11:03 | frontend |\n * 2022-10-10 13:11:03 | backend |\n * 2022-10-10 13:11:03 | backend | [nodemon] to restart at any time, enter `rs`\n * 2022-10-10 13:11:03 | backend | [nodemon] watching path(s): backend/\n * 2022-10-10 13:11:03 | backend | [nodemon] watching extensions: js,mjs,json\n * 2022-10-10 13:11:03 | backend | [nodemon] starting `node backend/index.js`\n * 2022-10-10 13:11:03 | backend |\n *\n * ```\n */\nconst ConcurrentOutput: FunctionComponent<ConcurrentOutputProps> = ({\n processes,\n abortController,\n showTimestamps = true,\n onInput,\n footer,\n}) => {\n const [processOutput, setProcessOutput] = useState<Chunk[]>([])\n const concurrentColors = ['yellow', 'cyan', 'magenta', 'green', 'blue']\n const prefixColumnSize = Math.max(...processes.map((process) => process.prefix.length))\n\n function lineColor(index: number) {\n const colorIndex = index < concurrentColors.length ? index : index % concurrentColors.length\n return concurrentColors[colorIndex]!\n }\n\n const writableStream = (process: OutputProcess, index: number) => {\n return new Writable({\n write(chunk, _encoding, next) {\n const lines = stripAnsi(chunk.toString('ascii').replace(/(\\n)$/, '')).split(/\\n/)\n\n setProcessOutput((previousProcessOutput) => [\n ...previousProcessOutput,\n {\n color: lineColor(index),\n prefix: process.prefix,\n lines,\n },\n ])\n\n next()\n },\n })\n }\n\n const runProcesses = () => {\n return Promise.all(\n processes.map(async (process, index) => {\n const stdout = writableStream(process, index)\n const stderr = writableStream(process, index)\n\n await process.action(stdout, stderr, abortController.signal)\n }),\n )\n }\n\n if (onInput) {\n useInput(\n useCallback(\n (input, key) => {\n handleCtrlC(input, key)\n onInput(input, key, () => treeKill(process.pid, 'SIGINT'))\n },\n [onInput],\n ),\n )\n }\n\n useAsyncAndUnmount(runProcesses, {onRejected: () => abortController.abort()})\n\n return (\n <>\n <Static items={processOutput}>\n {(chunk, index) => {\n return (\n <Box flexDirection=\"column\" key={index}>\n {chunk.lines.map((line, index) => (\n <Box key={index} flexDirection=\"row\">\n {showTimestamps ? (\n <Box>\n <Box marginRight={1}>\n <Text color={chunk.color}>\n {new Date().toISOString().replace(/T/, ' ').replace(/\\..+/, '')}\n </Text>\n </Box>\n\n <Text bold color={chunk.color}>\n |\n </Text>\n </Box>\n ) : null}\n\n <Box width={prefixColumnSize} marginX={1}>\n <Text color={chunk.color}>{chunk.prefix}</Text>\n </Box>\n\n <Text bold color={chunk.color}>\n |\n </Text>\n\n <Box flexGrow={1} paddingLeft={1}>\n <Text color={chunk.color}>{line}</Text>\n </Box>\n </Box>\n ))}\n </Box>\n )\n }}\n </Static>\n {footer ? (\n <Box marginY={1} flexDirection=\"column\">\n <Box flexGrow={1}>\n <TextWithBackground text={footer.title} inverse paddingX={2} paddingY={1} />\n </Box>\n {footer.subTitle ? (\n <Box marginTop={1} flexGrow={1}>\n <Text>{footer.subTitle}</Text>\n </Box>\n ) : null}\n </Box>\n ) : null}\n </>\n )\n}\n\nexport {ConcurrentOutput}\n"]}
|
|
1
|
+
{"version":3,"file":"ConcurrentOutput.js","sourceRoot":"","sources":["../../../../../src/private/node/ui/components/ConcurrentOutput.tsx"],"names":[],"mappings":"AACA,OAAO,kBAAkB,MAAM,mCAAmC,CAAA;AAElE,OAAO,EAAC,WAAW,EAAC,MAAM,aAAa,CAAA;AACvC,OAAO,KAAK,EAAE,EAAoB,QAAQ,EAAC,MAAM,OAAO,CAAA;AACxD,OAAO,EAAC,GAAG,EAAO,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAC,MAAM,KAAK,CAAA;AACpD,OAAO,SAAS,MAAM,YAAY,CAAA;AAClC,OAAO,QAAQ,MAAM,WAAW,CAAA;AAChC,OAAO,OAAO,MAAM,SAAS,CAAA;AAC7B,OAAO,EAAC,QAAQ,EAAC,MAAM,QAAQ,CAA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;;gBAAI,QAAQ,CAAC,MAAM,CAC3E,CACR,CAAC,CACE;YACL,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CACjB,oBAAC,GAAG,IAAC,SAAS,EAAE,CAAC;gBACf,oBAAC,IAAI,QAAE,MAAM,CAAC,QAAQ,CAAQ,CAC1B,CACP,CAAC,CAAC,CAAC,IAAI,CACJ,CACP,CAAC,CAAC,CAAC,IAAI,CACP,CACJ,CAAA;AACH,CAAC,CAAA;AAED,OAAO,EAAC,gBAAgB,EAAC,CAAA","sourcesContent":["import {OutputProcess} from '../../../../public/node/output.js'\nimport useAsyncAndUnmount from '../hooks/use-async-and-unmount.js'\nimport {AbortController} from '../../../../public/node/abort.js'\nimport {handleCtrlC} from '../../ui.js'\nimport React, {FunctionComponent, useState} from 'react'\nimport {Box, Key, Static, Text, useInput} from 'ink'\nimport stripAnsi from 'strip-ansi'\nimport treeKill from 'tree-kill'\nimport figures from 'figures'\nimport {Writable} from 'stream'\n\nexport type WritableStream = (process: OutputProcess, index: number) => Writable\n\ninterface Shortcut {\n key: string\n action: string\n}\nexport interface ConcurrentOutputProps {\n processes: OutputProcess[]\n abortController: AbortController\n showTimestamps?: boolean\n onInput?: (input: string, key: Key, exit: () => void) => void\n footer?: {\n shortcuts: Shortcut[]\n subTitle?: string\n }\n}\ninterface Chunk {\n color: string\n prefix: string\n lines: string[]\n}\n\n/**\n * Renders output from concurrent processes to the terminal.\n * Output will be divided in a three column layout\n * with the left column containing the timestamp,\n * the right column containing the output,\n * and the middle column containing the process prefix.\n * Every process will be rendered with a different color, up to 4 colors.\n *\n * For example running `shopify app dev`:\n *\n * ```shell\n * 2022-10-10 13:11:03 | backend | npm\n * 2022-10-10 13:11:03 | backend | WARN ignoring workspace config at ...\n * 2022-10-10 13:11:03 | backend |\n * 2022-10-10 13:11:03 | backend |\n * 2022-10-10 13:11:03 | backend | > shopify-app-template-node@0.1.0 dev\n * 2022-10-10 13:11:03 | backend | > cross-env NODE_ENV=development nodemon backend/index.js --watch ./backend\n * 2022-10-10 13:11:03 | backend |\n * 2022-10-10 13:11:03 | backend |\n * 2022-10-10 13:11:03 | frontend |\n * 2022-10-10 13:11:03 | frontend | > starter-react-frontend-app@0.1.0 dev\n * 2022-10-10 13:11:03 | frontend | > cross-env NODE_ENV=development node vite-server.js\n * 2022-10-10 13:11:03 | frontend |\n * 2022-10-10 13:11:03 | frontend |\n * 2022-10-10 13:11:03 | backend |\n * 2022-10-10 13:11:03 | backend | [nodemon] to restart at any time, enter `rs`\n * 2022-10-10 13:11:03 | backend | [nodemon] watching path(s): backend/\n * 2022-10-10 13:11:03 | backend | [nodemon] watching extensions: js,mjs,json\n * 2022-10-10 13:11:03 | backend | [nodemon] starting `node backend/index.js`\n * 2022-10-10 13:11:03 | backend |\n *\n * ```\n */\nconst ConcurrentOutput: FunctionComponent<ConcurrentOutputProps> = ({\n processes,\n abortController,\n showTimestamps = true,\n onInput,\n footer,\n}) => {\n const [processOutput, setProcessOutput] = useState<Chunk[]>([])\n const concurrentColors = ['yellow', 'cyan', 'magenta', 'green', 'blue']\n const prefixColumnSize = Math.max(...processes.map((process) => process.prefix.length))\n\n function lineColor(index: number) {\n const colorIndex = index < concurrentColors.length ? index : index % concurrentColors.length\n return concurrentColors[colorIndex]!\n }\n\n const writableStream = (process: OutputProcess, index: number) => {\n return new Writable({\n write(chunk, _encoding, next) {\n const lines = stripAnsi(chunk.toString('utf8').replace(/(\\n)$/, '')).split(/\\n/)\n\n setProcessOutput((previousProcessOutput) => [\n ...previousProcessOutput,\n {\n color: lineColor(index),\n prefix: process.prefix,\n lines,\n },\n ])\n\n next()\n },\n })\n }\n\n const runProcesses = () => {\n return Promise.all(\n processes.map(async (process, index) => {\n const stdout = writableStream(process, index)\n const stderr = writableStream(process, index)\n\n await process.action(stdout, stderr, abortController.signal)\n }),\n )\n }\n\n useInput(\n (input, key) => {\n handleCtrlC(input, key)\n\n onInput!(input, key, () => treeKill(process.pid, 'SIGINT'))\n },\n {isActive: typeof onInput !== 'undefined'},\n )\n\n useAsyncAndUnmount(runProcesses, {onRejected: () => abortController.abort()})\n\n return (\n <>\n <Static items={processOutput}>\n {(chunk, index) => {\n return (\n <Box flexDirection=\"column\" key={index}>\n {chunk.lines.map((line, index) => (\n <Box key={index} flexDirection=\"row\">\n {showTimestamps ? (\n <Box>\n <Box marginRight={1}>\n <Text color={chunk.color}>\n {new Date().toISOString().replace(/T/, ' ').replace(/\\..+/, '')}\n </Text>\n </Box>\n\n <Text bold color={chunk.color}>\n |\n </Text>\n </Box>\n ) : null}\n\n <Box width={prefixColumnSize} marginX={1}>\n <Text color={chunk.color}>{chunk.prefix}</Text>\n </Box>\n\n <Text bold color={chunk.color}>\n |\n </Text>\n\n <Box flexGrow={1} paddingLeft={1}>\n <Text color={chunk.color}>{line}</Text>\n </Box>\n </Box>\n ))}\n </Box>\n )\n }}\n </Static>\n {footer ? (\n <Box marginY={1} flexDirection=\"column\" flexGrow={1}>\n <Box flexDirection=\"column\">\n {footer.shortcuts.map((shortcut, index) => (\n <Text key={index}>\n {figures.pointerSmall} Press <Text bold>{shortcut.key}</Text> | {shortcut.action}\n </Text>\n ))}\n </Box>\n {footer.subTitle ? (\n <Box marginTop={1}>\n <Text>{footer.subTitle}</Text>\n </Box>\n ) : null}\n </Box>\n ) : null}\n </>\n )\n}\n\nexport {ConcurrentOutput}\n"]}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ConcurrentOutput } from './ConcurrentOutput.js';
|
|
2
2
|
import { getLastFrameAfterUnmount, waitForInputsToBeReady } from '../../testing/ui.js';
|
|
3
3
|
import { AbortController } from '../../../../public/node/abort.js';
|
|
4
|
+
import { unstyled } from '../../../../public/node/output.js';
|
|
4
5
|
import React from 'react';
|
|
5
6
|
import { describe, expect, test, vi } from 'vitest';
|
|
6
7
|
import { render } from 'ink-testing-library';
|
|
@@ -36,23 +37,31 @@ describe('ConcurrentOutput', () => {
|
|
|
36
37
|
};
|
|
37
38
|
// When
|
|
38
39
|
const renderInstance = render(React.createElement(ConcurrentOutput, { processes: [backendProcess, frontendProcess], abortController: new AbortController(), footer: {
|
|
39
|
-
|
|
40
|
+
shortcuts: [
|
|
41
|
+
{
|
|
42
|
+
key: 'p',
|
|
43
|
+
action: 'open your browser',
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
key: 'q',
|
|
47
|
+
action: 'quit',
|
|
48
|
+
},
|
|
49
|
+
],
|
|
40
50
|
subTitle: `Preview URL: https://shopify.com`,
|
|
41
51
|
} }));
|
|
42
52
|
// wait for all output to be rendered
|
|
43
53
|
await frontendPromise;
|
|
44
54
|
// Then
|
|
45
|
-
expect(getLastFrameAfterUnmount(renderInstance).replace(/\d/g, '0')).toMatchInlineSnapshot(`
|
|
46
|
-
"
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
55
|
+
expect(unstyled(getLastFrameAfterUnmount(renderInstance).replace(/\d/g, '0'))).toMatchInlineSnapshot(`
|
|
56
|
+
"0000-00-00 00:00:00 | backend | first backend message
|
|
57
|
+
0000-00-00 00:00:00 | backend | second backend message
|
|
58
|
+
0000-00-00 00:00:00 | backend | third backend message
|
|
59
|
+
0000-00-00 00:00:00 | frontend | first frontend message
|
|
60
|
+
0000-00-00 00:00:00 | frontend | second frontend message
|
|
61
|
+
0000-00-00 00:00:00 | frontend | third frontend message
|
|
52
62
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
[0m [00m
|
|
63
|
+
› Press p | open your browser
|
|
64
|
+
› Press q | quit
|
|
56
65
|
|
|
57
66
|
Preview URL: https://shopify.com
|
|
58
67
|
"
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ConcurrentOutput.test.js","sourceRoot":"","sources":["../../../../../src/private/node/ui/components/ConcurrentOutput.test.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,gBAAgB,EAAC,MAAM,uBAAuB,CAAA;AACtD,OAAO,EAAC,wBAAwB,EAAE,sB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yBAAyB,CAAC,CAAA;gBACvC,MAAM,CAAC,KAAK,CAAC,wBAAwB,CAAC,CAAA;gBAEtC,sBAAsB,EAAE,CAAA;YAC1B,CAAC;SACF,CAAA;QACD,OAAO;QAEP,MAAM,cAAc,GAAG,MAAM,CAC3B,oBAAC,gBAAgB,IACf,SAAS,EAAE,CAAC,cAAc,EAAE,eAAe,CAAC,EAC5C,eAAe,EAAE,IAAI,eAAe,EAAE,EACtC,MAAM,EAAE;gBACN,
|
|
1
|
+
{"version":3,"file":"ConcurrentOutput.test.js","sourceRoot":"","sources":["../../../../../src/private/node/ui/components/ConcurrentOutput.test.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,gBAAgB,EAAC,MAAM,uBAAuB,CAAA;AACtD,OAAO,EAAC,wBAAwB,EAAE,sBAAsB,EAAC,MAAM,qBAAqB,CAAA;AACpF,OAAO,EAAC,eAAe,EAAc,MAAM,kCAAkC,CAAA;AAC7E,OAAO,EAAC,QAAQ,EAAC,MAAM,mCAAmC,CAAA;AAC1D,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,EAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,EAAC,MAAM,QAAQ,CAAA;AACjD,OAAO,EAAC,MAAM,EAAC,MAAM,qBAAqB,CAAA;AAG1C,QAAQ,CAAC,kBAAkB,EAAE,GAAG,EAAE;IAChC,IAAI,CAAC,2DAA2D,EAAE,KAAK,IAAI,EAAE;QAC3E,QAAQ;QACR,IAAI,qBAAiC,CAAA;QACrC,IAAI,sBAAkC,CAAA;QAEtC,MAAM,cAAc,GAAG,IAAI,OAAO,CAAO,UAAU,OAAO,EAAE,OAAO;YACjE,qBAAqB,GAAG,OAAO,CAAA;QACjC,CAAC,CAAC,CAAA;QAEF,MAAM,eAAe,GAAG,IAAI,OAAO,CAAO,UAAU,OAAO,EAAE,OAAO;YAClE,sBAAsB,GAAG,OAAO,CAAA;QAClC,CAAC,CAAC,CAAA;QAEF,MAAM,cAA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;;KAarG,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;IAEF,IAAI,CAAC,6DAA6D,EAAE,KAAK,IAAI,EAAE;QAC7E,MAAM,kBAAkB,GAAG,IAAI,OAAO,CAAO,UAAU,QAAQ,EAAE,OAAO,IAAG,CAAC,CAAC,CAAA;QAE7E,MAAM,kBAAkB,GAAG;YACzB,MAAM,EAAE,sBAAsB;YAC9B,MAAM,EAAE,KAAK,IAAI,EAAE;gBACjB,MAAM,kBAAkB,CAAA;YAC1B,CAAC;SACF,CAAA;QAED,MAAM,OAAO,GAAG,EAAE,CAAC,EAAE,EAAE,CAAA;QAEvB,MAAM,cAAc,GAAG,MAAM,CAC3B,oBAAC,gBAAgB,IACf,SAAS,EAAE,CAAC,kBAAkB,CAAC,EAC/B,eAAe,EAAE,IAAI,eAAe,EAAE,EACtC,OAAO,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,GAC5C,CACH,CAAA;QAED,MAAM,sBAAsB,EAAE,CAAA;QAC9B,MAAM,CAAC,OAAO,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAA;QAExC,cAAc,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QAC/B,MAAM,CAAC,OAAO,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAA;QACxC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAE,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;IAC7C,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA","sourcesContent":["import {ConcurrentOutput} from './ConcurrentOutput.js'\nimport {getLastFrameAfterUnmount, waitForInputsToBeReady} from '../../testing/ui.js'\nimport {AbortController, AbortSignal} from '../../../../public/node/abort.js'\nimport {unstyled} from '../../../../public/node/output.js'\nimport React from 'react'\nimport {describe, expect, test, vi} from 'vitest'\nimport {render} from 'ink-testing-library'\nimport {Writable} from 'stream'\n\ndescribe('ConcurrentOutput', () => {\n test('renders a stream of concurrent outputs from sub-processes', async () => {\n // Given\n let backendPromiseResolve: () => void\n let frontendPromiseResolve: () => void\n\n const backendPromise = new Promise<void>(function (resolve, _reject) {\n backendPromiseResolve = resolve\n })\n\n const frontendPromise = new Promise<void>(function (resolve, _reject) {\n frontendPromiseResolve = resolve\n })\n\n const backendProcess = {\n prefix: 'backend',\n action: async (stdout: Writable, _stderr: Writable, _signal: AbortSignal) => {\n stdout.write('first backend message')\n stdout.write('second backend message')\n stdout.write('third backend message')\n\n backendPromiseResolve()\n },\n }\n\n const frontendProcess = {\n prefix: 'frontend',\n action: async (stdout: Writable, _stderr: Writable, _signal: AbortSignal) => {\n await backendPromise\n\n stdout.write('first frontend message')\n stdout.write('second frontend message')\n stdout.write('third frontend message')\n\n frontendPromiseResolve()\n },\n }\n // When\n\n const renderInstance = render(\n <ConcurrentOutput\n processes={[backendProcess, frontendProcess]}\n abortController={new AbortController()}\n footer={{\n shortcuts: [\n {\n key: 'p',\n action: 'open your browser',\n },\n {\n key: 'q',\n action: 'quit',\n },\n ],\n subTitle: `Preview URL: https://shopify.com`,\n }}\n />,\n )\n\n // wait for all output to be rendered\n await frontendPromise\n\n // Then\n expect(unstyled(getLastFrameAfterUnmount(renderInstance)!.replace(/\\d/g, '0'))).toMatchInlineSnapshot(`\n \"0000-00-00 00:00:00 | backend | first backend message\n 0000-00-00 00:00:00 | backend | second backend message\n 0000-00-00 00:00:00 | backend | third backend message\n 0000-00-00 00:00:00 | frontend | first frontend message\n 0000-00-00 00:00:00 | frontend | second frontend message\n 0000-00-00 00:00:00 | frontend | third frontend message\n\n › Press p | open your browser\n › Press q | quit\n\n Preview URL: https://shopify.com\n \"\n `)\n })\n\n test('accepts a onInput function that fires when a key is pressed', async () => {\n const neverEndingPromise = new Promise<void>(function (_resolve, _reject) {})\n\n const neverEndingProcess = {\n prefix: 'never-ending-process',\n action: async () => {\n await neverEndingPromise\n },\n }\n\n const onInput = vi.fn()\n\n const renderInstance = render(\n <ConcurrentOutput\n processes={[neverEndingProcess]}\n abortController={new AbortController()}\n onInput={(input, key) => onInput(input, key)}\n />,\n )\n\n await waitForInputsToBeReady()\n expect(onInput).toHaveBeenCalledTimes(0)\n\n renderInstance.stdin.write('a')\n expect(onInput).toHaveBeenCalledTimes(1)\n expect(onInput.mock.calls[0]![0]).toBe('a')\n })\n})\n"]}
|
|
@@ -27,7 +27,7 @@ const FullScreen = ({ children }) => {
|
|
|
27
27
|
// switch back to the main buffer
|
|
28
28
|
standardOutput.write('\u001B[?1049l');
|
|
29
29
|
};
|
|
30
|
-
}, []);
|
|
30
|
+
}, [standardOutput]);
|
|
31
31
|
return (React.createElement(Box, { width: size.columns, height: size.rows }, children));
|
|
32
32
|
};
|
|
33
33
|
export { FullScreen };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FullScreen.js","sourceRoot":"","sources":["../../../../../src/private/node/ui/components/FullScreen.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,GAAG,EAAE,SAAS,EAAC,MAAM,KAAK,CAAA;AAClC,OAAO,KAAK,EAAE,EAAoB,SAAS,EAAE,QAAQ,EAAC,MAAM,OAAO,CAAA;AAEnE;;;;GAIG;AACH,MAAM,UAAU,GAAsB,CAAC,EAAC,QAAQ,EAAC,EAAe,EAAE;IAChE,MAAM,EAAC,MAAM,EAAC,GAAG,SAAS,EAAE,CAAA;IAC5B,MAAM,cAAc,GAAG,MAAO,CAAA;IAE9B,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,QAAQ,CAAC;QAC/B,OAAO,EAAE,cAAc,CAAC,OAAO;QAC/B,IAAI,EAAE,cAAc,CAAC,IAAI;KAC1B,CAAC,CAAA;IAEF,SAAS,CAAC,GAAG,EAAE;QACb,SAAS,QAAQ;YACf,OAAO,CAAC;gBACN,OAAO,EAAE,cAAc,CAAC,OAAO;gBAC/B,IA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AG,EAAE;YACV,cAAc,CAAC,GAAG,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAA;YACtC,iCAAiC;YACjC,cAAc,CAAC,KAAK,CAAC,eAAe,CAAC,CAAA;QACvC,CAAC,CAAA;IACH,CAAC,EAAE,CAAC,cAAc,CAAC,CAAC,CAAA;IAEpB,OAAO,CACL,oBAAC,GAAG,IAAC,KAAK,EAAE,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,IAAI,IACxC,QAAQ,CACL,CACP,CAAA;AACH,CAAC,CAAA;AAED,OAAO,EAAC,UAAU,EAAC,CAAA","sourcesContent":["import {Box, useStdout} from 'ink'\nimport React, {FunctionComponent, useEffect, useState} from 'react'\n\n/**\n * `FullScreen` renders all output in a new buffer and makes it full screen. This is useful when:\n * - You want to preserve terminal history. `ink` [normally clears the terminal history](https://github.com/vadimdemedes/ink/issues/382) if the rendered output is taller than the terminal window. By rendering in a separate buffer history will be preserved and will be visible after pressing `Ctrl+C`.\n * - You want to respond to the resize event of the terminal. Whenever the user resizes their terminal window the output's height and width will be recalculated and re-rendered properly.\n */\nconst FullScreen: FunctionComponent = ({children}): JSX.Element => {\n const {stdout} = useStdout()\n const standardOutput = stdout!\n\n const [size, setSize] = useState({\n columns: standardOutput.columns,\n rows: standardOutput.rows,\n })\n\n useEffect(() => {\n function onResize() {\n setSize({\n columns: standardOutput.columns,\n rows: standardOutput.rows,\n })\n }\n\n standardOutput.on('resize', onResize)\n // switch to an alternate buffer\n standardOutput.write('\\u001B[?1049h')\n return () => {\n standardOutput.off('resize', onResize)\n // switch back to the main buffer\n standardOutput.write('\\u001B[?1049l')\n }\n }, [standardOutput])\n\n return (\n <Box width={size.columns} height={size.rows}>\n {children}\n </Box>\n )\n}\n\nexport {FullScreen}\n"]}
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
+
import { DOMElement } from 'ink';
|
|
3
|
+
declare module 'react' {
|
|
4
|
+
function forwardRef<T, P>(render: (props: P, ref: React.Ref<T>) => JSX.Element | null): (props: P & React.RefAttributes<T>) => JSX.Element | null;
|
|
5
|
+
}
|
|
2
6
|
interface OnChangeOptions<T> {
|
|
3
7
|
item: Item<T> | undefined;
|
|
4
8
|
usedShortcut: boolean;
|
|
@@ -16,6 +20,7 @@ export interface SelectInputProps<T> {
|
|
|
16
20
|
hasMorePages?: boolean;
|
|
17
21
|
morePagesMessage?: string;
|
|
18
22
|
infoMessage?: string;
|
|
23
|
+
limit?: number;
|
|
19
24
|
}
|
|
20
25
|
export interface Item<T> {
|
|
21
26
|
label: string;
|
|
@@ -23,5 +28,5 @@ export interface Item<T> {
|
|
|
23
28
|
key?: string;
|
|
24
29
|
group?: string;
|
|
25
30
|
}
|
|
26
|
-
declare
|
|
27
|
-
export {
|
|
31
|
+
export declare const SelectInput: <T>(props: SelectInputProps<T> & React.RefAttributes<DOMElement>) => JSX.Element | null;
|
|
32
|
+
export {};
|
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import { isEqual } from '../../../../public/common/lang.js';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import React, { useState, useEffect, useRef, useCallback } from 'react';
|
|
2
|
+
import { debounce } from '../../../../public/common/function.js';
|
|
3
|
+
import React, { useState, useEffect, useRef, useCallback, forwardRef } from 'react';
|
|
5
4
|
import { Box, useInput, Text } from 'ink';
|
|
6
|
-
import { debounce } from '@shopify/cli-kit/common/function';
|
|
7
5
|
import chalk from 'chalk';
|
|
8
6
|
import figures from 'figures';
|
|
7
|
+
import { createRequire } from 'module';
|
|
8
|
+
const require = createRequire(import.meta.url);
|
|
9
|
+
function rotateArray(array, rotation) {
|
|
10
|
+
const arrayCopy = array.slice();
|
|
11
|
+
return arrayCopy.splice(-(rotation ?? 0) % arrayCopy.length).concat(arrayCopy);
|
|
12
|
+
}
|
|
9
13
|
function highlightedLabel(label, term) {
|
|
10
14
|
if (!term) {
|
|
11
15
|
return label;
|
|
@@ -15,53 +19,48 @@ function highlightedLabel(label, term) {
|
|
|
15
19
|
return chalk.bold(match);
|
|
16
20
|
});
|
|
17
21
|
}
|
|
18
|
-
function groupItems(items) {
|
|
19
|
-
let index = 0;
|
|
20
|
-
const [withGroup, withoutGroup] = partition(items, 'group');
|
|
21
|
-
const withGroupMapped = mapValues(groupBy(withGroup, 'group'), (groupItems) => groupItems.map((groupItem) => {
|
|
22
|
-
const item = { ...groupItem, key: groupItem.key ?? (index + 1).toString(), index };
|
|
23
|
-
index += 1;
|
|
24
|
-
return item;
|
|
25
|
-
}));
|
|
26
|
-
const withoutGroupMapped = withoutGroup.map((item) => {
|
|
27
|
-
const newItem = { ...item, key: item.key ?? (index + 1).toString(), index };
|
|
28
|
-
index += 1;
|
|
29
|
-
return newItem;
|
|
30
|
-
});
|
|
31
|
-
return [withGroupMapped, withoutGroupMapped];
|
|
32
|
-
}
|
|
33
22
|
// eslint-disable-next-line react/function-component-definition
|
|
34
|
-
function
|
|
35
|
-
|
|
23
|
+
function Item({ item, previousItem, selectedIndex, highlightedTerm, enableShortcuts, items, hasAnyGroup, }) {
|
|
24
|
+
const isSelected = items.indexOf(item) === selectedIndex;
|
|
25
|
+
const label = highlightedLabel(item.label, highlightedTerm);
|
|
26
|
+
let title;
|
|
27
|
+
if (typeof previousItem === 'undefined' || item.group !== previousItem.group) {
|
|
28
|
+
title = item.group ?? (hasAnyGroup ? 'Other' : undefined);
|
|
29
|
+
}
|
|
30
|
+
return (React.createElement(Box, { key: item.key, flexDirection: "column", marginTop: items.indexOf(item) !== 0 && title ? 1 : 0 },
|
|
36
31
|
title ? (React.createElement(Box, { marginLeft: 3 },
|
|
37
32
|
React.createElement(Text, { bold: true }, title))) : null,
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
return (React.createElement(Box, { key: item.key },
|
|
42
|
-
React.createElement(Box, { marginRight: 2 }, isSelected ? React.createElement(Text, { color: "cyan" }, `>`) : React.createElement(Text, null, " ")),
|
|
43
|
-
React.createElement(Text, { color: isSelected ? 'cyan' : undefined }, enableShortcuts ? `(${item.key}) ${label}` : label)));
|
|
44
|
-
})));
|
|
33
|
+
React.createElement(Box, { key: item.key },
|
|
34
|
+
React.createElement(Box, { marginRight: 2 }, isSelected ? React.createElement(Text, { color: "cyan" }, `>`) : React.createElement(Text, null, " ")),
|
|
35
|
+
React.createElement(Text, { color: isSelected ? 'cyan' : undefined }, enableShortcuts ? `(${item.key}) ${label}` : label))));
|
|
45
36
|
}
|
|
46
37
|
// eslint-disable-next-line react/function-component-definition
|
|
47
|
-
function
|
|
38
|
+
function SelectInputInner({ items: initialItems, onChange, enableShortcuts = true, focus = true, emptyMessage = 'No items to select.', defaultValue, highlightedTerm, loading = false, errorMessage, hasMorePages = false, morePagesMessage, infoMessage, limit, }, ref) {
|
|
39
|
+
const sortBy = require('lodash/sortBy');
|
|
40
|
+
const hasAnyGroup = initialItems.some((item) => typeof item.group !== 'undefined');
|
|
41
|
+
const items = sortBy(initialItems, 'group');
|
|
42
|
+
const itemsWithKeys = items.map((item, index) => ({
|
|
43
|
+
...item,
|
|
44
|
+
key: item.key ?? (index + 1).toString(),
|
|
45
|
+
}));
|
|
48
46
|
const defaultValueIndex = defaultValue ? items.findIndex((item) => item.value === defaultValue.value) : -1;
|
|
49
47
|
const initialIndex = defaultValueIndex === -1 ? 0 : defaultValueIndex;
|
|
48
|
+
const hasLimit = typeof limit !== 'undefined' && items.length > limit;
|
|
50
49
|
const inputStack = useRef(null);
|
|
51
50
|
const [selectedIndex, setSelectedIndex] = useState(initialIndex);
|
|
52
|
-
const [
|
|
53
|
-
const
|
|
54
|
-
const
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
51
|
+
const [rotateIndex, setRotateIndex] = useState(0);
|
|
52
|
+
const slicedItemsWithKeys = hasLimit ? rotateArray(itemsWithKeys, rotateIndex).slice(0, limit) : itemsWithKeys;
|
|
53
|
+
const previousItems = useRef(undefined);
|
|
54
|
+
const changeSelection = useCallback(({ newSelectedIndex, newRotateIndex, usedShortcut = false, }) => {
|
|
55
|
+
setSelectedIndex(newSelectedIndex);
|
|
56
|
+
if (typeof newRotateIndex !== 'undefined')
|
|
57
|
+
setRotateIndex(newRotateIndex);
|
|
58
|
+
const rotatedItems = hasLimit ? rotateArray(items, newRotateIndex).slice(0, limit) : items;
|
|
60
59
|
onChange({
|
|
61
|
-
item:
|
|
60
|
+
item: rotatedItems[newSelectedIndex],
|
|
62
61
|
usedShortcut,
|
|
63
62
|
});
|
|
64
|
-
}, [items]);
|
|
63
|
+
}, [hasLimit, items, limit, onChange]);
|
|
65
64
|
useEffect(() => {
|
|
66
65
|
if (items.length === 0) {
|
|
67
66
|
// reset selection when items are empty
|
|
@@ -69,35 +68,50 @@ function SelectInput({ items, onChange, enableShortcuts = true, focus = true, em
|
|
|
69
68
|
item: undefined,
|
|
70
69
|
usedShortcut: false,
|
|
71
70
|
});
|
|
71
|
+
// reset index when items change or the first time
|
|
72
|
+
}
|
|
73
|
+
else if (!previousItems.current) {
|
|
74
|
+
changeSelection({ newSelectedIndex: initialIndex, newRotateIndex: 0 });
|
|
72
75
|
}
|
|
73
|
-
else if (
|
|
74
|
-
|
|
75
|
-
!isEqual(previousItems.current.map((item) => item.value), items.map((item) => item.value))) {
|
|
76
|
-
changeSelection({ index: 0 });
|
|
76
|
+
else if (!isEqual(previousItems.current.map((item) => item.value), items.map((item) => item.value))) {
|
|
77
|
+
changeSelection({ newSelectedIndex: 0, newRotateIndex: 0 });
|
|
77
78
|
}
|
|
78
79
|
previousItems.current = items;
|
|
79
|
-
}, [items]);
|
|
80
|
-
const handleArrows =
|
|
81
|
-
const lastIndex = items.length - 1;
|
|
80
|
+
}, [changeSelection, initialIndex, items, onChange]);
|
|
81
|
+
const handleArrows = (key) => {
|
|
82
82
|
if (key.upArrow) {
|
|
83
|
-
|
|
83
|
+
const lastIndex = items.length - 1;
|
|
84
|
+
const atFirstIndex = selectedIndex === 0;
|
|
85
|
+
const nextIndex = hasLimit ? selectedIndex : lastIndex;
|
|
86
|
+
const nextRotateIndex = atFirstIndex ? rotateIndex + 1 : rotateIndex;
|
|
87
|
+
const nextSelectedIndex = atFirstIndex ? nextIndex : selectedIndex - 1;
|
|
88
|
+
changeSelection({ newSelectedIndex: nextSelectedIndex, newRotateIndex: nextRotateIndex });
|
|
84
89
|
}
|
|
85
90
|
else if (key.downArrow) {
|
|
86
|
-
|
|
91
|
+
const atLastIndex = selectedIndex === (hasLimit ? limit : items.length) - 1;
|
|
92
|
+
const nextIndex = hasLimit ? selectedIndex : 0;
|
|
93
|
+
const nextRotateIndex = atLastIndex ? rotateIndex - 1 : rotateIndex;
|
|
94
|
+
const nextSelectedIndex = atLastIndex ? nextIndex : selectedIndex + 1;
|
|
95
|
+
changeSelection({ newSelectedIndex: nextSelectedIndex, newRotateIndex: nextRotateIndex });
|
|
87
96
|
}
|
|
88
|
-
}
|
|
97
|
+
};
|
|
89
98
|
const handleShortcuts = useCallback((input) => {
|
|
90
|
-
if (
|
|
91
|
-
const
|
|
92
|
-
if (
|
|
93
|
-
changeSelection({
|
|
99
|
+
if (slicedItemsWithKeys.map((item) => item.key).includes(input)) {
|
|
100
|
+
const itemWithKey = slicedItemsWithKeys.find((item) => item.key === input);
|
|
101
|
+
if (itemWithKey !== undefined) {
|
|
102
|
+
changeSelection({
|
|
103
|
+
newSelectedIndex: items.findIndex((item) => item.value === itemWithKey.value),
|
|
104
|
+
usedShortcut: true,
|
|
105
|
+
});
|
|
94
106
|
}
|
|
95
107
|
}
|
|
96
|
-
}, [items]);
|
|
108
|
+
}, [changeSelection, items, slicedItemsWithKeys]);
|
|
109
|
+
// disable exhaustive-deps because we want to memoize the debounce function itself
|
|
110
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
97
111
|
const debounceHandleShortcuts = useCallback(debounce((newInputStack) => {
|
|
98
112
|
handleShortcuts(newInputStack);
|
|
99
113
|
inputStack.current = null;
|
|
100
|
-
}, 300), []);
|
|
114
|
+
}, 300), [handleShortcuts]);
|
|
101
115
|
useInput((input, key) => {
|
|
102
116
|
// check that no special modifier (shift, control, etc.) is being pressed
|
|
103
117
|
if (enableShortcuts && input.length > 0 && Object.values(key).every((value) => value === false)) {
|
|
@@ -111,7 +125,6 @@ function SelectInput({ items, onChange, enableShortcuts = true, focus = true, em
|
|
|
111
125
|
handleArrows(key);
|
|
112
126
|
}
|
|
113
127
|
}, { isActive: focus });
|
|
114
|
-
const ungroupedItemsTitle = groupTitles.length > 0 ? 'Other' : undefined;
|
|
115
128
|
if (loading) {
|
|
116
129
|
return (React.createElement(Box, { marginLeft: 3 },
|
|
117
130
|
React.createElement(Text, { dimColor: true }, "Loading...")));
|
|
@@ -125,9 +138,8 @@ function SelectInput({ items, onChange, enableShortcuts = true, focus = true, em
|
|
|
125
138
|
React.createElement(Text, { dimColor: true }, emptyMessage)));
|
|
126
139
|
}
|
|
127
140
|
else {
|
|
128
|
-
return (React.createElement(Box, { flexDirection: "column" },
|
|
129
|
-
|
|
130
|
-
ungroupedItems.length > 0 && (React.createElement(SelectItemsGroup, { title: ungroupedItemsTitle, selectedIndex: selectedIndex, items: ungroupedItems, hasMarginTop: groupTitles.length > 0, enableShortcuts: enableShortcuts, highlightedTerm: highlightedTerm })),
|
|
141
|
+
return (React.createElement(Box, { flexDirection: "column", ref: ref },
|
|
142
|
+
slicedItemsWithKeys.map((item, index) => (React.createElement(Item, { key: item.key, item: item, previousItem: slicedItemsWithKeys[index - 1], highlightedTerm: highlightedTerm, selectedIndex: selectedIndex, items: slicedItemsWithKeys, enableShortcuts: enableShortcuts, hasAnyGroup: hasAnyGroup }))),
|
|
131
143
|
React.createElement(Box, { marginTop: 1, marginLeft: 3, flexDirection: "column" },
|
|
132
144
|
hasMorePages ? (React.createElement(Text, null,
|
|
133
145
|
React.createElement(Text, { bold: true },
|
|
@@ -135,10 +147,11 @@ function SelectInput({ items, onChange, enableShortcuts = true, focus = true, em
|
|
|
135
147
|
items.length,
|
|
136
148
|
" of many"),
|
|
137
149
|
morePagesMessage ? ` ${morePagesMessage}` : null)) : null,
|
|
150
|
+
hasLimit ? React.createElement(Text, { dimColor: true }, `Showing ${limit} of ${items.length} items.`) : null,
|
|
138
151
|
React.createElement(Text, { dimColor: true }, infoMessage
|
|
139
152
|
? infoMessage
|
|
140
153
|
: `Press ${figures.arrowUp}${figures.arrowDown} arrows to select, enter to confirm`))));
|
|
141
154
|
}
|
|
142
155
|
}
|
|
143
|
-
export
|
|
156
|
+
export const SelectInput = forwardRef(SelectInputInner);
|
|
144
157
|
//# sourceMappingURL=SelectInput.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SelectInput.js","sourceRoot":"","sources":["../../../../../src/private/node/ui/components/SelectInput.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,EAAC,MAAM,mCAAmC,CAAA;AACzD,OAAO,EAAC,OAAO,EAAE,SAAS,EAAC,MAAM,yCAAyC,CAAA;AAC1E,OAAO,EAAC,SAAS,EAAC,MAAM,qCAAqC,CAAA;AAC7D,OAAO,KAAK,EAAE,EAAC,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,WAAW,EAAC,MAAM,OAAO,CAAA;AACrE,OAAO,EAAC,GAAG,EAAO,QAAQ,EAAE,IAAI,EAAC,MAAM,KAAK,CAAA;AAC5C,OAAO,EAAC,QAAQ,EAAC,MAAM,kCAAkC,CAAA;AACzD,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,OAAO,MAAM,SAAS,CAAA;AAiC7B,SAAS,gBAAgB,CAAC,KAAa,EAAE,IAAwB;IAC/D,IAAI,CAAC,IAAI,EAAE;QACT,OAAO,KAAK,CAAA;KACb;IAED,MAAM,KAAK,GAAG,IAAI,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,CAAA;IACnC,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE;QACpC,OAAO,KAAK,CAAC,IAAI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AE,aAAa,EAAC,QAAQ;gBACrD,YAAY,CAAC,CAAC,CAAC,CACd,oBAAC,IAAI;oBACH,oBAAC,IAAI,IAAC,IAAI;;wBAAI,KAAK,CAAC,MAAM;mCAAgB;oBACzC,gBAAgB,CAAC,CAAC,CAAC,KAAK,gBAAgB,EAAE,CAAC,CAAC,CAAC,IAAI,CAC7C,CACR,CAAC,CAAC,CAAC,IAAI;gBACR,oBAAC,IAAI,IAAC,QAAQ,UACX,WAAW;oBACV,CAAC,CAAC,WAAW;oBACb,CAAC,CAAC,SAAS,OAAO,CAAC,OAAO,GAAG,OAAO,CAAC,SAAS,qCAAqC,CAChF,CACH,CACF,CACP,CAAA;KACF;AACH,CAAC;AAED,OAAO,EAAC,WAAW,EAAC,CAAA","sourcesContent":["import {isEqual} from '../../../../public/common/lang.js'\nimport {groupBy, partition} from '../../../../public/common/collection.js'\nimport {mapValues} from '../../../../public/common/object.js'\nimport React, {useState, useEffect, useRef, useCallback} from 'react'\nimport {Box, Key, useInput, Text} from 'ink'\nimport {debounce} from '@shopify/cli-kit/common/function'\nimport chalk from 'chalk'\nimport figures from 'figures'\n\ninterface OnChangeOptions<T> {\n item: Item<T> | undefined\n usedShortcut: boolean\n}\nexport interface SelectInputProps<T> {\n items: Item<T>[]\n onChange: ({item, usedShortcut}: OnChangeOptions<T>) => void\n enableShortcuts?: boolean\n focus?: boolean\n emptyMessage?: string\n defaultValue?: Item<T>\n highlightedTerm?: string\n loading?: boolean\n errorMessage?: string\n hasMorePages?: boolean\n morePagesMessage?: string\n infoMessage?: string\n}\n\nexport interface Item<T> {\n label: string\n value: T\n key?: string\n group?: string\n}\n\ninterface ItemWithIndex<T> extends Item<T> {\n key: string\n index: number\n}\n\nfunction highlightedLabel(label: string, term: string | undefined) {\n if (!term) {\n return label\n }\n\n const regex = new RegExp(term, 'i')\n return label.replace(regex, (match) => {\n return chalk.bold(match)\n })\n}\n\nfunction groupItems<T>(items: Item<T>[]): [{[key: string]: ItemWithIndex<T>[]}, ItemWithIndex<T>[]] {\n let index = 0\n\n const [withGroup, withoutGroup] = partition(items, 'group')\n\n const withGroupMapped = mapValues(groupBy(withGroup, 'group'), (groupItems) =>\n groupItems.map((groupItem) => {\n const item = {...groupItem, key: groupItem.key ?? (index + 1).toString(), index}\n index += 1\n return item\n }),\n )\n const withoutGroupMapped = withoutGroup.map((item) => {\n const newItem = {...item, key: item.key ?? (index + 1).toString(), index}\n index += 1\n return newItem\n })\n\n return [withGroupMapped, withoutGroupMapped]\n}\n\ninterface SelectItemsGroupProps<T> {\n title: string | undefined\n items: ItemWithIndex<T>[]\n selectedIndex: number\n hasMarginTop: boolean\n enableShortcuts: boolean\n highlightedTerm?: string\n}\n\n// eslint-disable-next-line react/function-component-definition\nfunction SelectItemsGroup<T>({\n title,\n items,\n selectedIndex,\n hasMarginTop,\n enableShortcuts,\n highlightedTerm,\n}: SelectItemsGroupProps<T>): JSX.Element {\n return (\n <Box key={title} flexDirection=\"column\" marginTop={hasMarginTop ? 1 : 0}>\n {title ? (\n <Box marginLeft={3}>\n <Text bold>{title}</Text>\n </Box>\n ) : null}\n\n {items.map((item) => {\n const isSelected = item.index === selectedIndex\n const label = highlightedLabel(item.label, highlightedTerm)\n\n return (\n <Box key={item.key}>\n <Box marginRight={2}>{isSelected ? <Text color=\"cyan\">{`>`}</Text> : <Text> </Text>}</Box>\n\n <Text color={isSelected ? 'cyan' : undefined}>{enableShortcuts ? `(${item.key}) ${label}` : label}</Text>\n </Box>\n )\n })}\n </Box>\n )\n}\n\n// eslint-disable-next-line react/function-component-definition\nfunction SelectInput<T>({\n items,\n onChange,\n enableShortcuts = true,\n focus = true,\n emptyMessage = 'No items to select.',\n defaultValue,\n highlightedTerm,\n loading = false,\n errorMessage,\n hasMorePages = false,\n morePagesMessage,\n infoMessage,\n}: React.PropsWithChildren<SelectInputProps<T>>): JSX.Element | null {\n const defaultValueIndex = defaultValue ? items.findIndex((item) => item.value === defaultValue.value) : -1\n const initialIndex = defaultValueIndex === -1 ? 0 : defaultValueIndex\n const inputStack = useRef<string | null>(null)\n const [selectedIndex, setSelectedIndex] = useState(initialIndex)\n const [groupedItems, ungroupedItems] = groupItems(items)\n const groupedItemsValues = [...Object.values(groupedItems).flat(), ...ungroupedItems]\n const keys = groupedItemsValues.map((item) => item.key)\n const groupTitles = Object.keys(groupedItems)\n const previousItems = useRef<Item<T>[]>(items)\n\n const changeSelection = useCallback(\n ({index, usedShortcut = false}: {index: number; usedShortcut?: boolean}) => {\n const groupedItem = groupedItemsValues.find((item) => item.index === index)!\n setSelectedIndex(index)\n onChange({\n item: items.find((item) => item.value === groupedItem.value),\n usedShortcut,\n })\n },\n [items],\n )\n\n useEffect(() => {\n if (items.length === 0) {\n // reset selection when items are empty\n onChange({\n item: undefined,\n usedShortcut: false,\n })\n } else if (\n // reset index when items change\n !isEqual(\n previousItems.current.map((item) => item.value),\n items.map((item) => item.value),\n )\n ) {\n changeSelection({index: 0})\n }\n\n previousItems.current = items\n }, [items])\n\n const handleArrows = useCallback(\n (key: Key) => {\n const lastIndex = items.length - 1\n\n if (key.upArrow) {\n changeSelection({index: selectedIndex === 0 ? lastIndex : selectedIndex - 1})\n } else if (key.downArrow) {\n changeSelection({index: selectedIndex === lastIndex ? 0 : selectedIndex + 1})\n }\n },\n [selectedIndex, items],\n )\n\n const handleShortcuts = useCallback(\n (input: string) => {\n if (keys.includes(input)) {\n const groupedItem = groupedItemsValues.find((item) => item.key === input)\n if (groupedItem !== undefined) {\n changeSelection({index: groupedItem.index, usedShortcut: true})\n }\n }\n },\n [items],\n )\n\n const debounceHandleShortcuts = useCallback(\n debounce((newInputStack) => {\n handleShortcuts(newInputStack)\n inputStack.current = null\n }, 300),\n [],\n )\n\n useInput(\n (input, key) => {\n // check that no special modifier (shift, control, etc.) is being pressed\n if (enableShortcuts && input.length > 0 && Object.values(key).every((value) => value === false)) {\n const newInputStack = inputStack.current === null ? input : inputStack.current + input\n\n inputStack.current = newInputStack\n debounceHandleShortcuts(newInputStack)\n } else {\n debounceHandleShortcuts.cancel()\n inputStack.current = null\n handleArrows(key)\n }\n },\n {isActive: focus},\n )\n\n const ungroupedItemsTitle = groupTitles.length > 0 ? 'Other' : undefined\n\n if (loading) {\n return (\n <Box marginLeft={3}>\n <Text dimColor>Loading...</Text>\n </Box>\n )\n } else if (errorMessage && errorMessage.length > 0) {\n return (\n <Box marginLeft={3}>\n <Text color=\"red\">{errorMessage}</Text>\n </Box>\n )\n } else if (items.length === 0) {\n return (\n <Box marginLeft={3}>\n <Text dimColor>{emptyMessage}</Text>\n </Box>\n )\n } else {\n return (\n <Box flexDirection=\"column\">\n {groupTitles.map((title, index) => (\n <SelectItemsGroup\n title={title}\n selectedIndex={selectedIndex}\n items={groupedItems[title]!}\n key={title}\n hasMarginTop={index !== 0}\n enableShortcuts={enableShortcuts}\n highlightedTerm={highlightedTerm}\n />\n ))}\n\n {ungroupedItems.length > 0 && (\n <SelectItemsGroup\n title={ungroupedItemsTitle}\n selectedIndex={selectedIndex}\n items={ungroupedItems}\n hasMarginTop={groupTitles.length > 0}\n enableShortcuts={enableShortcuts}\n highlightedTerm={highlightedTerm}\n />\n )}\n\n <Box marginTop={1} marginLeft={3} flexDirection=\"column\">\n {hasMorePages ? (\n <Text>\n <Text bold>1-{items.length} of many</Text>\n {morePagesMessage ? ` ${morePagesMessage}` : null}\n </Text>\n ) : null}\n <Text dimColor>\n {infoMessage\n ? infoMessage\n : `Press ${figures.arrowUp}${figures.arrowDown} arrows to select, enter to confirm`}\n </Text>\n </Box>\n </Box>\n )\n }\n}\n\nexport {SelectInput}\n"]}
|
|
1
|
+
{"version":3,"file":"SelectInput.js","sourceRoot":"","sources":["../../../../../src/private/node/ui/components/SelectInput.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,EAAC,MAAM,mCAAmC,CAAA;AACzD,OAAO,EAAC,QAAQ,EAAC,MAAM,uCAAuC,CAAA;AAC9D,OAAO,KAAK,EAAE,EAAC,QAAQ,EAAE,SAAS,EAAE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KAAK,GAAG,CAAC,CAAC,EAC5C,eAAe,EAAE,eAAe,EAChC,aAAa,EAAE,aAAa,EAC5B,KAAK,EAAE,mBAAmB,EAC1B,eAAe,EAAE,eAAe,EAChC,WAAW,EAAE,WAAW,GACxB,CACH,CAAC;YAEF,oBAAC,GAAG,IAAC,SAAS,EAAE,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,aAAa,EAAC,QAAQ;gBACrD,YAAY,CAAC,CAAC,CAAC,CACd,oBAAC,IAAI;oBACH,oBAAC,IAAI,IAAC,IAAI;;wBAAI,KAAK,CAAC,MAAM;mCAAgB;oBACzC,gBAAgB,CAAC,CAAC,CAAC,KAAK,gBAAgB,EAAE,CAAC,CAAC,CAAC,IAAI,CAC7C,CACR,CAAC,CAAC,CAAC,IAAI;gBACP,QAAQ,CAAC,CAAC,CAAC,oBAAC,IAAI,IAAC,QAAQ,UAAE,WAAW,KAAK,OAAO,KAAK,CAAC,MAAM,SAAS,CAAQ,CAAC,CAAC,CAAC,IAAI;gBACvF,oBAAC,IAAI,IAAC,QAAQ,UACX,WAAW;oBACV,CAAC,CAAC,WAAW;oBACb,CAAC,CAAC,SAAS,OAAO,CAAC,OAAO,GAAG,OAAO,CAAC,SAAS,qCAAqC,CAChF,CACH,CACF,CACP,CAAA;KACF;AACH,CAAC;AAED,MAAM,CAAC,MAAM,WAAW,GAAG,UAAU,CAAC,gBAAgB,CAAC,CAAA","sourcesContent":["import {isEqual} from '../../../../public/common/lang.js'\nimport {debounce} from '../../../../public/common/function.js'\nimport React, {useState, useEffect, useRef, useCallback, forwardRef} from 'react'\nimport {Box, Key, useInput, Text, DOMElement} from 'ink'\nimport chalk from 'chalk'\nimport figures from 'figures'\nimport {createRequire} from 'module'\n\nconst require = createRequire(import.meta.url)\n\ndeclare module 'react' {\n function forwardRef<T, P>(\n render: (props: P, ref: React.Ref<T>) => JSX.Element | null,\n ): (props: P & React.RefAttributes<T>) => JSX.Element | null\n}\n\nfunction rotateArray<T>(array: T[], rotation?: number) {\n const arrayCopy = array.slice()\n return arrayCopy.splice(-(rotation ?? 0) % arrayCopy.length).concat(arrayCopy)\n}\n\ninterface OnChangeOptions<T> {\n item: Item<T> | undefined\n usedShortcut: boolean\n}\nexport interface SelectInputProps<T> {\n items: Item<T>[]\n onChange: ({item, usedShortcut}: OnChangeOptions<T>) => void\n enableShortcuts?: boolean\n focus?: boolean\n emptyMessage?: string\n defaultValue?: Item<T>\n highlightedTerm?: string\n loading?: boolean\n errorMessage?: string\n hasMorePages?: boolean\n morePagesMessage?: string\n infoMessage?: string\n limit?: number\n}\n\nexport interface Item<T> {\n label: string\n value: T\n key?: string\n group?: string\n}\n\ninterface ItemWithKey<T> extends Item<T> {\n key: string\n}\n\nfunction highlightedLabel(label: string, term: string | undefined) {\n if (!term) {\n return label\n }\n\n const regex = new RegExp(term, 'i')\n return label.replace(regex, (match) => {\n return chalk.bold(match)\n })\n}\n\ninterface ItemProps<T> {\n item: ItemWithKey<T>\n previousItem: ItemWithKey<T> | undefined\n items: ItemWithKey<T>[]\n selectedIndex: number\n highlightedTerm?: string\n enableShortcuts: boolean\n hasAnyGroup: boolean\n}\n\n// eslint-disable-next-line react/function-component-definition\nfunction Item<T>({\n item,\n previousItem,\n selectedIndex,\n highlightedTerm,\n enableShortcuts,\n items,\n hasAnyGroup,\n}: ItemProps<T>): JSX.Element {\n const isSelected = items.indexOf(item) === selectedIndex\n const label = highlightedLabel(item.label, highlightedTerm)\n let title: string | undefined\n\n if (typeof previousItem === 'undefined' || item.group !== previousItem.group) {\n title = item.group ?? (hasAnyGroup ? 'Other' : undefined)\n }\n\n return (\n <Box key={item.key} flexDirection=\"column\" marginTop={items.indexOf(item) !== 0 && title ? 1 : 0}>\n {title ? (\n <Box marginLeft={3}>\n <Text bold>{title}</Text>\n </Box>\n ) : null}\n\n <Box key={item.key}>\n <Box marginRight={2}>{isSelected ? <Text color=\"cyan\">{`>`}</Text> : <Text> </Text>}</Box>\n <Text color={isSelected ? 'cyan' : undefined}>{enableShortcuts ? `(${item.key}) ${label}` : label}</Text>\n </Box>\n </Box>\n )\n}\n\n// eslint-disable-next-line react/function-component-definition\nfunction SelectInputInner<T>(\n {\n items: initialItems,\n onChange,\n enableShortcuts = true,\n focus = true,\n emptyMessage = 'No items to select.',\n defaultValue,\n highlightedTerm,\n loading = false,\n errorMessage,\n hasMorePages = false,\n morePagesMessage,\n infoMessage,\n limit,\n }: SelectInputProps<T>,\n ref: React.ForwardedRef<DOMElement>,\n): JSX.Element | null {\n const sortBy = require('lodash/sortBy')\n const hasAnyGroup = initialItems.some((item) => typeof item.group !== 'undefined')\n const items = sortBy(initialItems, 'group') as Item<T>[]\n const itemsWithKeys = items.map((item, index) => ({\n ...item,\n key: item.key ?? (index + 1).toString(),\n })) as ItemWithKey<T>[]\n const defaultValueIndex = defaultValue ? items.findIndex((item) => item.value === defaultValue.value) : -1\n const initialIndex = defaultValueIndex === -1 ? 0 : defaultValueIndex\n const hasLimit = typeof limit !== 'undefined' && items.length > limit\n const inputStack = useRef<string | null>(null)\n const [selectedIndex, setSelectedIndex] = useState(initialIndex)\n const [rotateIndex, setRotateIndex] = useState(0)\n const slicedItemsWithKeys = hasLimit ? rotateArray(itemsWithKeys, rotateIndex).slice(0, limit) : itemsWithKeys\n const previousItems = useRef<Item<T>[] | undefined>(undefined)\n\n const changeSelection = useCallback(\n ({\n newSelectedIndex,\n newRotateIndex,\n usedShortcut = false,\n }: {\n newSelectedIndex: number\n usedShortcut?: boolean\n newRotateIndex?: number\n }) => {\n setSelectedIndex(newSelectedIndex)\n if (typeof newRotateIndex !== 'undefined') setRotateIndex(newRotateIndex)\n\n const rotatedItems = hasLimit ? rotateArray(items, newRotateIndex).slice(0, limit) : items\n\n onChange({\n item: rotatedItems[newSelectedIndex],\n usedShortcut,\n })\n },\n [hasLimit, items, limit, onChange],\n )\n\n useEffect(() => {\n if (items.length === 0) {\n // reset selection when items are empty\n onChange({\n item: undefined,\n usedShortcut: false,\n })\n // reset index when items change or the first time\n } else if (!previousItems.current) {\n changeSelection({newSelectedIndex: initialIndex, newRotateIndex: 0})\n } else if (\n !isEqual(\n previousItems.current.map((item) => item.value),\n items.map((item) => item.value),\n )\n ) {\n changeSelection({newSelectedIndex: 0, newRotateIndex: 0})\n }\n\n previousItems.current = items\n }, [changeSelection, initialIndex, items, onChange])\n\n const handleArrows = (key: Key) => {\n if (key.upArrow) {\n const lastIndex = items.length - 1\n const atFirstIndex = selectedIndex === 0\n const nextIndex = hasLimit ? selectedIndex : lastIndex\n const nextRotateIndex = atFirstIndex ? rotateIndex + 1 : rotateIndex\n const nextSelectedIndex = atFirstIndex ? nextIndex : selectedIndex - 1\n\n changeSelection({newSelectedIndex: nextSelectedIndex, newRotateIndex: nextRotateIndex})\n } else if (key.downArrow) {\n const atLastIndex = selectedIndex === (hasLimit ? limit : items.length) - 1\n const nextIndex = hasLimit ? selectedIndex : 0\n const nextRotateIndex = atLastIndex ? rotateIndex - 1 : rotateIndex\n const nextSelectedIndex = atLastIndex ? nextIndex : selectedIndex + 1\n\n changeSelection({newSelectedIndex: nextSelectedIndex, newRotateIndex: nextRotateIndex})\n }\n }\n\n const handleShortcuts = useCallback(\n (input: string) => {\n if (slicedItemsWithKeys.map((item) => item.key).includes(input)) {\n const itemWithKey = slicedItemsWithKeys.find((item) => item.key === input)\n if (itemWithKey !== undefined) {\n changeSelection({\n newSelectedIndex: items.findIndex((item) => item.value === itemWithKey.value),\n usedShortcut: true,\n })\n }\n }\n },\n [changeSelection, items, slicedItemsWithKeys],\n )\n\n // disable exhaustive-deps because we want to memoize the debounce function itself\n // eslint-disable-next-line react-hooks/exhaustive-deps\n const debounceHandleShortcuts = useCallback(\n debounce((newInputStack) => {\n handleShortcuts(newInputStack)\n inputStack.current = null\n }, 300),\n [handleShortcuts],\n )\n\n useInput(\n (input, key) => {\n // check that no special modifier (shift, control, etc.) is being pressed\n if (enableShortcuts && input.length > 0 && Object.values(key).every((value) => value === false)) {\n const newInputStack = inputStack.current === null ? input : inputStack.current + input\n\n inputStack.current = newInputStack\n debounceHandleShortcuts(newInputStack)\n } else {\n debounceHandleShortcuts.cancel()\n inputStack.current = null\n handleArrows(key)\n }\n },\n {isActive: focus},\n )\n\n if (loading) {\n return (\n <Box marginLeft={3}>\n <Text dimColor>Loading...</Text>\n </Box>\n )\n } else if (errorMessage && errorMessage.length > 0) {\n return (\n <Box marginLeft={3}>\n <Text color=\"red\">{errorMessage}</Text>\n </Box>\n )\n } else if (items.length === 0) {\n return (\n <Box marginLeft={3}>\n <Text dimColor>{emptyMessage}</Text>\n </Box>\n )\n } else {\n return (\n <Box flexDirection=\"column\" ref={ref}>\n {slicedItemsWithKeys.map((item, index) => (\n <Item\n key={item.key}\n item={item}\n previousItem={slicedItemsWithKeys[index - 1]}\n highlightedTerm={highlightedTerm}\n selectedIndex={selectedIndex}\n items={slicedItemsWithKeys}\n enableShortcuts={enableShortcuts}\n hasAnyGroup={hasAnyGroup}\n />\n ))}\n\n <Box marginTop={1} marginLeft={3} flexDirection=\"column\">\n {hasMorePages ? (\n <Text>\n <Text bold>1-{items.length} of many</Text>\n {morePagesMessage ? ` ${morePagesMessage}` : null}\n </Text>\n ) : null}\n {hasLimit ? <Text dimColor>{`Showing ${limit} of ${items.length} items.`}</Text> : null}\n <Text dimColor>\n {infoMessage\n ? infoMessage\n : `Press ${figures.arrowUp}${figures.arrowDown} arrows to select, enter to confirm`}\n </Text>\n </Box>\n </Box>\n )\n }\n}\n\nexport const SelectInput = forwardRef(SelectInputInner)\n"]}
|
|
@@ -146,7 +146,8 @@ describe('SelectInput', async () => {
|
|
|
146
146
|
|
|
147
147
|
[2mPress ↑↓ arrows to select, enter to confirm[22m"
|
|
148
148
|
`);
|
|
149
|
-
expect(onChange).
|
|
149
|
+
expect(onChange).toHaveBeenCalledOnce();
|
|
150
|
+
expect(onChange).toHaveBeenCalledWith({ item: items[0], usedShortcut: false });
|
|
150
151
|
});
|
|
151
152
|
test('handles custom keys', async () => {
|
|
152
153
|
const onChange = vi.fn();
|
|
@@ -313,7 +314,8 @@ describe('SelectInput', async () => {
|
|
|
313
314
|
|
|
314
315
|
[2mPress ↑↓ arrows to select, enter to confirm[22m"
|
|
315
316
|
`);
|
|
316
|
-
expect(onChange).
|
|
317
|
+
expect(onChange).toHaveBeenCalledOnce();
|
|
318
|
+
expect(onChange).toHaveBeenCalledWith({ item: items[0], usedShortcut: false });
|
|
317
319
|
});
|
|
318
320
|
test('accepts a default value', async () => {
|
|
319
321
|
const items = [
|
|
@@ -366,5 +368,73 @@ describe('SelectInput', async () => {
|
|
|
366
368
|
[2mPress ↑↓ arrows to select, enter to confirm[22m"
|
|
367
369
|
`);
|
|
368
370
|
});
|
|
371
|
+
test('supports a limit of items to show', async () => {
|
|
372
|
+
const items = [
|
|
373
|
+
{ label: 'first', value: 'first', key: 'f' },
|
|
374
|
+
{ label: 'second', value: 'second', key: 's' },
|
|
375
|
+
{ label: 'third', value: 'third' },
|
|
376
|
+
{ label: 'fourth', value: 'fourth' },
|
|
377
|
+
{ label: 'fifth', value: 'fifth', group: 'Automations', key: 'a' },
|
|
378
|
+
{ label: 'sixth', value: 'sixth', group: 'Automations' },
|
|
379
|
+
{ label: 'seventh', value: 'seventh' },
|
|
380
|
+
{ label: 'eighth', value: 'eighth', group: 'Merchant Admin' },
|
|
381
|
+
{ label: 'ninth', value: 'ninth', group: 'Merchant Admin' },
|
|
382
|
+
{ label: 'tenth', value: 'tenth' },
|
|
383
|
+
];
|
|
384
|
+
const renderInstance = render(React.createElement(SelectInput, { items: items, onChange: () => { }, limit: 5 }));
|
|
385
|
+
expect(renderInstance.lastFrame()).toMatchInlineSnapshot(`
|
|
386
|
+
" [1mAutomations[22m
|
|
387
|
+
[36m>[39m [36m(a) fifth[39m
|
|
388
|
+
(2) sixth
|
|
389
|
+
|
|
390
|
+
[1mMerchant Admin[22m
|
|
391
|
+
(3) eighth
|
|
392
|
+
(4) ninth
|
|
393
|
+
|
|
394
|
+
[1mOther[22m
|
|
395
|
+
(f) first
|
|
396
|
+
|
|
397
|
+
[2mShowing 5 of 10 items.[22m
|
|
398
|
+
[2mPress ↑↓ arrows to select, enter to confirm[22m"
|
|
399
|
+
`);
|
|
400
|
+
await waitForInputsToBeReady();
|
|
401
|
+
await sendInputAndWaitForChange(renderInstance, ARROW_UP);
|
|
402
|
+
expect(renderInstance.lastFrame()).toMatchInlineSnapshot(`
|
|
403
|
+
" [1mOther[22m
|
|
404
|
+
[36m>[39m [36m(10) tenth[39m
|
|
405
|
+
|
|
406
|
+
[1mAutomations[22m
|
|
407
|
+
(a) fifth
|
|
408
|
+
(2) sixth
|
|
409
|
+
|
|
410
|
+
[1mMerchant Admin[22m
|
|
411
|
+
(3) eighth
|
|
412
|
+
(4) ninth
|
|
413
|
+
|
|
414
|
+
[2mShowing 5 of 10 items.[22m
|
|
415
|
+
[2mPress ↑↓ arrows to select, enter to confirm[22m"
|
|
416
|
+
`);
|
|
417
|
+
await sendInputAndWaitForChange(renderInstance, ARROW_DOWN);
|
|
418
|
+
await sendInputAndWaitForChange(renderInstance, ARROW_DOWN);
|
|
419
|
+
await sendInputAndWaitForChange(renderInstance, ARROW_DOWN);
|
|
420
|
+
await sendInputAndWaitForChange(renderInstance, ARROW_DOWN);
|
|
421
|
+
await sendInputAndWaitForChange(renderInstance, ARROW_DOWN);
|
|
422
|
+
await sendInputAndWaitForChange(renderInstance, ARROW_DOWN);
|
|
423
|
+
expect(renderInstance.lastFrame()).toMatchInlineSnapshot(`
|
|
424
|
+
" [1mAutomations[22m
|
|
425
|
+
(2) sixth
|
|
426
|
+
|
|
427
|
+
[1mMerchant Admin[22m
|
|
428
|
+
(3) eighth
|
|
429
|
+
(4) ninth
|
|
430
|
+
|
|
431
|
+
[1mOther[22m
|
|
432
|
+
(f) first
|
|
433
|
+
[36m>[39m [36m(s) second[39m
|
|
434
|
+
|
|
435
|
+
[2mShowing 5 of 10 items.[22m
|
|
436
|
+
[2mPress ↑↓ arrows to select, enter to confirm[22m"
|
|
437
|
+
`);
|
|
438
|
+
});
|
|
369
439
|
});
|
|
370
440
|
//# sourceMappingURL=SelectInput.test.js.map
|