@react-stately/steplist 3.0.0-nightly-b7f8ed1a9-250516 → 3.0.0-nightly-77b3442e4-250520
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/dist/types.d.ts +3 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/useStepListState.main.js +7 -1
- package/dist/useStepListState.main.js.map +1 -1
- package/dist/useStepListState.mjs +7 -1
- package/dist/useStepListState.module.js +7 -1
- package/dist/useStepListState.module.js.map +1 -1
- package/package.json +4 -4
- package/src/useStepListState.ts +12 -3
package/dist/types.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CollectionBase, Key, SingleSelection } from "@react-types/shared";
|
|
2
2
|
import { SingleSelectListState } from "@react-stately/list";
|
|
3
|
-
export interface StepListProps<T> extends CollectionBase<T>, SingleSelection {
|
|
3
|
+
export interface StepListProps<T> extends CollectionBase<T>, Omit<SingleSelection, 'onSelectionChange'> {
|
|
4
4
|
/** The key of the last completed step (controlled). */
|
|
5
5
|
lastCompletedStep?: Key;
|
|
6
6
|
/** The key of the initially last completed step (uncontrolled). */
|
|
@@ -11,6 +11,8 @@ export interface StepListProps<T> extends CollectionBase<T>, SingleSelection {
|
|
|
11
11
|
isDisabled?: boolean;
|
|
12
12
|
/** Whether the step list is read only. Steps will be focusable but non-interactive. */
|
|
13
13
|
isReadOnly?: boolean;
|
|
14
|
+
/** Handler that is called when the selection changes. */
|
|
15
|
+
onSelectionChange?: (key: Key) => void;
|
|
14
16
|
}
|
|
15
17
|
export interface StepListState<T> extends SingleSelectListState<T> {
|
|
16
18
|
readonly lastCompletedStep?: Key;
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;AAiBA,+BAA+B,CAAC,CAAE,SAAQ,eAAe,CAAC,CAAC,EAAE,eAAe;
|
|
1
|
+
{"mappings":";;AAiBA,+BAA+B,CAAC,CAAE,SAAQ,eAAe,CAAC,CAAC,EAAE,IAAI,CAAC,eAAe,EAAE,mBAAmB,CAAC;IACrG,uDAAuD;IACvD,iBAAiB,CAAC,EAAE,GAAG,CAAC;IACxB,mEAAmE;IACnE,wBAAwB,CAAC,EAAE,GAAG,CAAC;IAC/B,yDAAyD;IACzD,yBAAyB,CAAC,EAAE,CAAC,GAAG,EAAE,GAAG,GAAG,IAAI,KAAK,IAAI,CAAC;IACtD,qFAAqF;IACrF,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,uFAAuF;IACvF,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,yDAAyD;IACzD,iBAAiB,CAAC,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,IAAI,CAAA;CACvC;AAED,+BAA+B,CAAC,CAAE,SAAQ,sBAAsB,CAAC,CAAC;IAChE,QAAQ,CAAC,iBAAiB,CAAC,EAAE,GAAG,CAAC;IACjC,oBAAoB,CAAC,GAAG,EAAE,GAAG,GAAG,IAAI,CAAC;IACrC,WAAW,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAAC;IAC/B,YAAY,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAAA;CAChC;AAED,iCAAiC,CAAC,SAAS,MAAM,EAAE,KAAK,EAAE,cAAc,CAAC,CAAC,GAAG,cAAc,CAAC,CAAC,CAwF5F","sources":["packages/@react-stately/steplist/src/packages/@react-stately/steplist/src/useStepListState.ts","packages/@react-stately/steplist/src/packages/@react-stately/steplist/src/index.ts","packages/@react-stately/steplist/src/index.ts"],"sourcesContent":[null,null,"/*\n * Copyright 2023 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {useStepListState} from './useStepListState';\nexport type {StepListProps, StepListState} from './useStepListState';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
|
|
@@ -22,7 +22,13 @@ $parcel$export(module.exports, "useStepListState", () => $e8f7edc240568f41$expor
|
|
|
22
22
|
|
|
23
23
|
|
|
24
24
|
function $e8f7edc240568f41$export$a993ed2a771330b(props) {
|
|
25
|
-
let state = (0, $bBs4a$reactstatelylist.useSingleSelectListState)(
|
|
25
|
+
let state = (0, $bBs4a$reactstatelylist.useSingleSelectListState)({
|
|
26
|
+
...props,
|
|
27
|
+
onSelectionChange: props.onSelectionChange ? (key)=>{
|
|
28
|
+
var _props_onSelectionChange;
|
|
29
|
+
if (key != null) (_props_onSelectionChange = props.onSelectionChange) === null || _props_onSelectionChange === void 0 ? void 0 : _props_onSelectionChange.call(props, key);
|
|
30
|
+
} : undefined
|
|
31
|
+
});
|
|
26
32
|
var _props_defaultLastCompletedStep;
|
|
27
33
|
let [lastCompletedStep, setLastCompletedStep] = (0, $bBs4a$reactstatelyutils.useControlledState)(props.lastCompletedStep, (_props_defaultLastCompletedStep = props.defaultLastCompletedStep) !== null && _props_defaultLastCompletedStep !== void 0 ? _props_defaultLastCompletedStep : null, props.onLastCompletedStepChange);
|
|
28
34
|
const { setSelectedKey: realSetSelectedKey, selectedKey: selectedKey, collection: collection } = state;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;AAAA;;;;;;;;;;CAUC;;;
|
|
1
|
+
{"mappings":";;;;;;;;;;AAAA;;;;;;;;;;CAUC;;;AA6BM,SAAS,yCAAmC,KAAuB;IACxE,IAAI,QAAQ,CAAA,GAAA,gDAAuB,EAAK;QACtC,GAAG,KAAK;QACR,mBAAmB,MAAM,iBAAiB,GAAI,CAAA;gBAE1C;YADF,IAAI,OAAO,OACT,2BAAA,MAAM,iBAAiB,cAAvB,+CAAA,8BAAA,OAA0B;QAE9B,IAAK;IACP;QAEwG;IAAxG,IA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,CAAC;QAE5C;QAEA,IAAI,MAAM,gBAAgB,CAAC,UAAU,IAAI,MACvC,MAAM,gBAAgB,CAAC,aAAa,CAAC;YAG5B;QAAX,IAAI,MAAM,CAAC,OAAA,sBAAsB,OAAO,SAAS,GAAG,CAAC,qBAAqB,gBAA/D,kBAAA,OAAsE;QACjF,IAAI,gBAAgB,aAAa,cAAc,KAAK,cAAc,MAAM,KAAK,gBAAgB,QAAQ,eAAe,GAAG,CAAC,cACtH,qBAAqB,eAAe,GAAG,CAAC;IAE5C;IAEA,SAAS,aAAa,IAAS;QAC7B,IAAI,MAAM,UAAU,IAAI,MAAM,YAAY,CAAC,GAAG,CAAC,SAAS,MAAM,UAAU,EACtE,OAAO;QAGT,IAAI,YAAY,OACd,OAAO;QAGT,MAAM,WAAW,eAAe,GAAG,CAAC;QACpC,OAAO,YAAY,aAAa,SAAS,MAAM,UAAU,CAAC,WAAW;IACvE;IAEA,SAAS,eAAe,GAAQ;QAC9B,MAAM,UAAU,eAAe,GAAG,CAAC;QACnC,IAAI,WAAW,CAAC,YAAY,UAC1B,qBAAqB;QAEvB,mBAAmB;IACrB;IAEA,OAAO;QACL,GAAG,KAAK;wBACR;8BACA;qBACA;sBACA;IACF;AACF;AAEA,SAAS,oCAAiB,IAAyB;IACjD,MAAM,WAAW,IAAI;IACrB,MAAM,iBAAiB,IAAI;IAC3B,IAAI,IAAI;IACR,IAAI,OAA4B;IAChC,KAAK,MAAM,QAAQ,KAAM;QACvB,SAAS,GAAG,CAAC,KAAK,GAAG,EAAE;QACvB,eAAe,GAAG,CAAC,KAAK,GAAG,EAAE,iBAAA,2BAAA,KAAM,GAAG;QACtC,OAAO;QACP;IACF;IACA,OAAO;kBACL;wBACA;IACF;AACF","sources":["packages/@react-stately/steplist/src/useStepListState.ts"],"sourcesContent":["/*\n * Copyright 2023 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {Collection, CollectionBase, Key, Node, SingleSelection} from '@react-types/shared';\nimport {SingleSelectListState, useSingleSelectListState} from '@react-stately/list';\nimport {useCallback, useEffect, useMemo} from 'react';\nimport {useControlledState} from '@react-stately/utils';\n\nexport interface StepListProps<T> extends CollectionBase<T>, Omit<SingleSelection, 'onSelectionChange'> {\n /** The key of the last completed step (controlled). */\n lastCompletedStep?: Key,\n /** The key of the initially last completed step (uncontrolled). */\n defaultLastCompletedStep?: Key,\n /** Callback for when the last completed step changes. */\n onLastCompletedStepChange?: (key: Key | null) => void,\n /** Whether the step list is disabled. Steps will not be focusable or interactive. */\n isDisabled?: boolean,\n /** Whether the step list is read only. Steps will be focusable but non-interactive. */\n isReadOnly?: boolean,\n /** Handler that is called when the selection changes. */\n onSelectionChange?: (key: Key) => void\n}\n\nexport interface StepListState<T> extends SingleSelectListState<T> {\n readonly lastCompletedStep?: Key,\n setLastCompletedStep(key: Key): void,\n isCompleted(key: Key): boolean,\n isSelectable(key: Key): boolean\n}\n\nexport function useStepListState<T extends object>(props: StepListProps<T>): StepListState<T> {\n let state = useSingleSelectListState<T>({\n ...props,\n onSelectionChange: props.onSelectionChange ? (key => {\n if (key != null) {\n props.onSelectionChange?.(key);\n }\n }) : undefined\n });\n\n let [lastCompletedStep, setLastCompletedStep] = useControlledState<Key | null>(props.lastCompletedStep, props.defaultLastCompletedStep ?? null, props.onLastCompletedStepChange);\n const {setSelectedKey: realSetSelectedKey, selectedKey, collection} = state;\n const {indexMap, keysLinkedList} = useMemo(() => buildKeysMaps(collection), [collection]);\n const selectedIdx = selectedKey != null ? indexMap.get(selectedKey) : 0;\n\n const isCompleted = useCallback((step: Key | null | undefined) => {\n if (step === undefined) {\n return false;\n }\n\n return (step !== null &&\n lastCompletedStep !== null &&\n indexMap.has(step) &&\n indexMap.has(lastCompletedStep) &&\n indexMap.get(step)! <= indexMap.get(lastCompletedStep)!);\n }, [indexMap, lastCompletedStep]);\n\n const findDefaultSelectedKey = useCallback((collection: Collection<Node<T>> | null, disabledKeys: Set<Key>) => {\n let selectedKey: Key | null = null;\n if (collection && collection.size > 0) {\n selectedKey = collection.getFirstKey();\n // loop over keys until we find one that isn't completed or disabled and select that\n while (selectedKey !== collection.getLastKey() && selectedKey && (disabledKeys.has(selectedKey) || isCompleted(selectedKey))) {\n selectedKey = collection.getKeyAfter(selectedKey);\n }\n }\n\n return selectedKey;\n }, [isCompleted]);\n\n useEffect(() => {\n // Ensure a step is always selected (in case no selected key was specified or if selected item was deleted from collection)\n let selectedKey: Key | null = state.selectedKey;\n if (state.selectionManager.isEmpty || selectedKey == null || !state.collection.getItem(selectedKey)) {\n selectedKey = findDefaultSelectedKey(state.collection, state.disabledKeys);\n if (selectedKey !== null) {\n state.selectionManager.replaceSelection(selectedKey);\n }\n }\n\n if (state.selectionManager.focusedKey == null) {\n state.selectionManager.setFocusedKey(selectedKey);\n }\n\n let lcs = (lastCompletedStep !== null ? indexMap.get(lastCompletedStep) : -1) ?? -1;\n if (selectedIdx !== undefined && selectedIdx > 0 && selectedIdx > lcs + 1 && selectedKey !== null && keysLinkedList.has(selectedKey)) {\n setLastCompletedStep(keysLinkedList.get(selectedKey)!);\n }\n });\n\n function isSelectable(step: Key) {\n if (props.isDisabled || state.disabledKeys.has(step) || props.isReadOnly) {\n return false;\n }\n\n if (isCompleted(step)) {\n return true;\n }\n\n const prevStep = keysLinkedList.get(step);\n return isCompleted(prevStep) || step === state.collection.getFirstKey();\n }\n\n function setSelectedKey(key: Key) {\n const prevKey = keysLinkedList.get(key);\n if (prevKey && !isCompleted(prevKey)) {\n setLastCompletedStep(prevKey);\n }\n realSetSelectedKey(key);\n }\n\n return {\n ...state,\n setSelectedKey,\n setLastCompletedStep,\n isCompleted,\n isSelectable\n };\n}\n\nfunction buildKeysMaps<T>(coll: Collection<Node<T>>) {\n const indexMap = new Map<Key, number>();\n const keysLinkedList = new Map<Key, Key | undefined>();\n let i = 0;\n let prev: Node<T> | undefined = undefined;\n for (const item of coll) {\n indexMap.set(item.key, i);\n keysLinkedList.set(item.key, prev?.key);\n prev = item;\n i++;\n }\n return {\n indexMap,\n keysLinkedList\n };\n}\n"],"names":[],"version":3,"file":"useStepListState.main.js.map"}
|
|
@@ -16,7 +16,13 @@ import {useControlledState as $4pToV$useControlledState} from "@react-stately/ut
|
|
|
16
16
|
|
|
17
17
|
|
|
18
18
|
function $cc8d414d37c075f2$export$a993ed2a771330b(props) {
|
|
19
|
-
let state = (0, $4pToV$useSingleSelectListState)(
|
|
19
|
+
let state = (0, $4pToV$useSingleSelectListState)({
|
|
20
|
+
...props,
|
|
21
|
+
onSelectionChange: props.onSelectionChange ? (key)=>{
|
|
22
|
+
var _props_onSelectionChange;
|
|
23
|
+
if (key != null) (_props_onSelectionChange = props.onSelectionChange) === null || _props_onSelectionChange === void 0 ? void 0 : _props_onSelectionChange.call(props, key);
|
|
24
|
+
} : undefined
|
|
25
|
+
});
|
|
20
26
|
var _props_defaultLastCompletedStep;
|
|
21
27
|
let [lastCompletedStep, setLastCompletedStep] = (0, $4pToV$useControlledState)(props.lastCompletedStep, (_props_defaultLastCompletedStep = props.defaultLastCompletedStep) !== null && _props_defaultLastCompletedStep !== void 0 ? _props_defaultLastCompletedStep : null, props.onLastCompletedStepChange);
|
|
22
28
|
const { setSelectedKey: realSetSelectedKey, selectedKey: selectedKey, collection: collection } = state;
|
|
@@ -16,7 +16,13 @@ import {useControlledState as $4pToV$useControlledState} from "@react-stately/ut
|
|
|
16
16
|
|
|
17
17
|
|
|
18
18
|
function $cc8d414d37c075f2$export$a993ed2a771330b(props) {
|
|
19
|
-
let state = (0, $4pToV$useSingleSelectListState)(
|
|
19
|
+
let state = (0, $4pToV$useSingleSelectListState)({
|
|
20
|
+
...props,
|
|
21
|
+
onSelectionChange: props.onSelectionChange ? (key)=>{
|
|
22
|
+
var _props_onSelectionChange;
|
|
23
|
+
if (key != null) (_props_onSelectionChange = props.onSelectionChange) === null || _props_onSelectionChange === void 0 ? void 0 : _props_onSelectionChange.call(props, key);
|
|
24
|
+
} : undefined
|
|
25
|
+
});
|
|
20
26
|
var _props_defaultLastCompletedStep;
|
|
21
27
|
let [lastCompletedStep, setLastCompletedStep] = (0, $4pToV$useControlledState)(props.lastCompletedStep, (_props_defaultLastCompletedStep = props.defaultLastCompletedStep) !== null && _props_defaultLastCompletedStep !== void 0 ? _props_defaultLastCompletedStep : null, props.onLastCompletedStepChange);
|
|
22
28
|
const { setSelectedKey: realSetSelectedKey, selectedKey: selectedKey, collection: collection } = state;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;AAAA;;;;;;;;;;CAUC;;;
|
|
1
|
+
{"mappings":";;;;AAAA;;;;;;;;;;CAUC;;;AA6BM,SAAS,yCAAmC,KAAuB;IACxE,IAAI,QAAQ,CAAA,GAAA,+BAAuB,EAAK;QACtC,GAAG,KAAK;QACR,mBAAmB,MAAM,iBAAiB,GAAI,CAAA;gBAE1C;YADF,IAAI,OAAO,OACT,2BAAA,MAAM,iBAAiB,cAAvB,+CAAA,8BAAA,OAA0B;QAE9B,IAAK;IACP;QAEwG;IAAxG,IAAI,CAAC,mBAAmB,qBAAqB,GAAG,CAAA,GAAA,yBAAiB,EAAc,MAAM,iBAAiB,EAAE,CAAA,kCAAA,MAAM,wBAAwB,cAA9B,6CAAA,kCAAkC,MAAM,MAAM,yBAAyB;IAC/K,MAAM,EAAC,gBAAgB,kBAAkB,eAAE,WAAW,cAAE,UAAU,EAAC,GAAG;IACtE,MAAM,YAAC,QAAQ,kBAAE,cAAc,EAAC,GAAG,CAAA,GAAA,cAAM,EAAE,IAAM,oCAAc,aAAa;QAAC;KAAW;IACxF,MAAM,cAAc,eAAe,OAAO,SAAS,GAAG,CAAC,eA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AI,MAAM,YAAY,CAAC,GAAG,CAAC,SAAS,MAAM,UAAU,EACtE,OAAO;QAGT,IAAI,YAAY,OACd,OAAO;QAGT,MAAM,WAAW,eAAe,GAAG,CAAC;QACpC,OAAO,YAAY,aAAa,SAAS,MAAM,UAAU,CAAC,WAAW;IACvE;IAEA,SAAS,eAAe,GAAQ;QAC9B,MAAM,UAAU,eAAe,GAAG,CAAC;QACnC,IAAI,WAAW,CAAC,YAAY,UAC1B,qBAAqB;QAEvB,mBAAmB;IACrB;IAEA,OAAO;QACL,GAAG,KAAK;wBACR;8BACA;qBACA;sBACA;IACF;AACF;AAEA,SAAS,oCAAiB,IAAyB;IACjD,MAAM,WAAW,IAAI;IACrB,MAAM,iBAAiB,IAAI;IAC3B,IAAI,IAAI;IACR,IAAI,OAA4B;IAChC,KAAK,MAAM,QAAQ,KAAM;QACvB,SAAS,GAAG,CAAC,KAAK,GAAG,EAAE;QACvB,eAAe,GAAG,CAAC,KAAK,GAAG,EAAE,iBAAA,2BAAA,KAAM,GAAG;QACtC,OAAO;QACP;IACF;IACA,OAAO;kBACL;wBACA;IACF;AACF","sources":["packages/@react-stately/steplist/src/useStepListState.ts"],"sourcesContent":["/*\n * Copyright 2023 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {Collection, CollectionBase, Key, Node, SingleSelection} from '@react-types/shared';\nimport {SingleSelectListState, useSingleSelectListState} from '@react-stately/list';\nimport {useCallback, useEffect, useMemo} from 'react';\nimport {useControlledState} from '@react-stately/utils';\n\nexport interface StepListProps<T> extends CollectionBase<T>, Omit<SingleSelection, 'onSelectionChange'> {\n /** The key of the last completed step (controlled). */\n lastCompletedStep?: Key,\n /** The key of the initially last completed step (uncontrolled). */\n defaultLastCompletedStep?: Key,\n /** Callback for when the last completed step changes. */\n onLastCompletedStepChange?: (key: Key | null) => void,\n /** Whether the step list is disabled. Steps will not be focusable or interactive. */\n isDisabled?: boolean,\n /** Whether the step list is read only. Steps will be focusable but non-interactive. */\n isReadOnly?: boolean,\n /** Handler that is called when the selection changes. */\n onSelectionChange?: (key: Key) => void\n}\n\nexport interface StepListState<T> extends SingleSelectListState<T> {\n readonly lastCompletedStep?: Key,\n setLastCompletedStep(key: Key): void,\n isCompleted(key: Key): boolean,\n isSelectable(key: Key): boolean\n}\n\nexport function useStepListState<T extends object>(props: StepListProps<T>): StepListState<T> {\n let state = useSingleSelectListState<T>({\n ...props,\n onSelectionChange: props.onSelectionChange ? (key => {\n if (key != null) {\n props.onSelectionChange?.(key);\n }\n }) : undefined\n });\n\n let [lastCompletedStep, setLastCompletedStep] = useControlledState<Key | null>(props.lastCompletedStep, props.defaultLastCompletedStep ?? null, props.onLastCompletedStepChange);\n const {setSelectedKey: realSetSelectedKey, selectedKey, collection} = state;\n const {indexMap, keysLinkedList} = useMemo(() => buildKeysMaps(collection), [collection]);\n const selectedIdx = selectedKey != null ? indexMap.get(selectedKey) : 0;\n\n const isCompleted = useCallback((step: Key | null | undefined) => {\n if (step === undefined) {\n return false;\n }\n\n return (step !== null &&\n lastCompletedStep !== null &&\n indexMap.has(step) &&\n indexMap.has(lastCompletedStep) &&\n indexMap.get(step)! <= indexMap.get(lastCompletedStep)!);\n }, [indexMap, lastCompletedStep]);\n\n const findDefaultSelectedKey = useCallback((collection: Collection<Node<T>> | null, disabledKeys: Set<Key>) => {\n let selectedKey: Key | null = null;\n if (collection && collection.size > 0) {\n selectedKey = collection.getFirstKey();\n // loop over keys until we find one that isn't completed or disabled and select that\n while (selectedKey !== collection.getLastKey() && selectedKey && (disabledKeys.has(selectedKey) || isCompleted(selectedKey))) {\n selectedKey = collection.getKeyAfter(selectedKey);\n }\n }\n\n return selectedKey;\n }, [isCompleted]);\n\n useEffect(() => {\n // Ensure a step is always selected (in case no selected key was specified or if selected item was deleted from collection)\n let selectedKey: Key | null = state.selectedKey;\n if (state.selectionManager.isEmpty || selectedKey == null || !state.collection.getItem(selectedKey)) {\n selectedKey = findDefaultSelectedKey(state.collection, state.disabledKeys);\n if (selectedKey !== null) {\n state.selectionManager.replaceSelection(selectedKey);\n }\n }\n\n if (state.selectionManager.focusedKey == null) {\n state.selectionManager.setFocusedKey(selectedKey);\n }\n\n let lcs = (lastCompletedStep !== null ? indexMap.get(lastCompletedStep) : -1) ?? -1;\n if (selectedIdx !== undefined && selectedIdx > 0 && selectedIdx > lcs + 1 && selectedKey !== null && keysLinkedList.has(selectedKey)) {\n setLastCompletedStep(keysLinkedList.get(selectedKey)!);\n }\n });\n\n function isSelectable(step: Key) {\n if (props.isDisabled || state.disabledKeys.has(step) || props.isReadOnly) {\n return false;\n }\n\n if (isCompleted(step)) {\n return true;\n }\n\n const prevStep = keysLinkedList.get(step);\n return isCompleted(prevStep) || step === state.collection.getFirstKey();\n }\n\n function setSelectedKey(key: Key) {\n const prevKey = keysLinkedList.get(key);\n if (prevKey && !isCompleted(prevKey)) {\n setLastCompletedStep(prevKey);\n }\n realSetSelectedKey(key);\n }\n\n return {\n ...state,\n setSelectedKey,\n setLastCompletedStep,\n isCompleted,\n isSelectable\n };\n}\n\nfunction buildKeysMaps<T>(coll: Collection<Node<T>>) {\n const indexMap = new Map<Key, number>();\n const keysLinkedList = new Map<Key, Key | undefined>();\n let i = 0;\n let prev: Node<T> | undefined = undefined;\n for (const item of coll) {\n indexMap.set(item.key, i);\n keysLinkedList.set(item.key, prev?.key);\n prev = item;\n i++;\n }\n return {\n indexMap,\n keysLinkedList\n };\n}\n"],"names":[],"version":3,"file":"useStepListState.module.js.map"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-stately/steplist",
|
|
3
|
-
"version": "3.0.0-nightly-
|
|
3
|
+
"version": "3.0.0-nightly-77b3442e4-250520",
|
|
4
4
|
"description": "Spectrum UI components in React",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -22,9 +22,9 @@
|
|
|
22
22
|
"url": "https://github.com/adobe/react-spectrum"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@react-stately/list": "3.0.0-nightly-
|
|
26
|
-
"@react-stately/utils": "3.0.0-nightly-
|
|
27
|
-
"@react-types/shared": "3.0.0-nightly-
|
|
25
|
+
"@react-stately/list": "3.0.0-nightly-77b3442e4-250520",
|
|
26
|
+
"@react-stately/utils": "3.0.0-nightly-77b3442e4-250520",
|
|
27
|
+
"@react-types/shared": "3.0.0-nightly-77b3442e4-250520",
|
|
28
28
|
"@swc/helpers": "^0.5.0"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
package/src/useStepListState.ts
CHANGED
|
@@ -15,7 +15,7 @@ import {SingleSelectListState, useSingleSelectListState} from '@react-stately/li
|
|
|
15
15
|
import {useCallback, useEffect, useMemo} from 'react';
|
|
16
16
|
import {useControlledState} from '@react-stately/utils';
|
|
17
17
|
|
|
18
|
-
export interface StepListProps<T> extends CollectionBase<T>, SingleSelection {
|
|
18
|
+
export interface StepListProps<T> extends CollectionBase<T>, Omit<SingleSelection, 'onSelectionChange'> {
|
|
19
19
|
/** The key of the last completed step (controlled). */
|
|
20
20
|
lastCompletedStep?: Key,
|
|
21
21
|
/** The key of the initially last completed step (uncontrolled). */
|
|
@@ -25,7 +25,9 @@ export interface StepListProps<T> extends CollectionBase<T>, SingleSelection {
|
|
|
25
25
|
/** Whether the step list is disabled. Steps will not be focusable or interactive. */
|
|
26
26
|
isDisabled?: boolean,
|
|
27
27
|
/** Whether the step list is read only. Steps will be focusable but non-interactive. */
|
|
28
|
-
isReadOnly?: boolean
|
|
28
|
+
isReadOnly?: boolean,
|
|
29
|
+
/** Handler that is called when the selection changes. */
|
|
30
|
+
onSelectionChange?: (key: Key) => void
|
|
29
31
|
}
|
|
30
32
|
|
|
31
33
|
export interface StepListState<T> extends SingleSelectListState<T> {
|
|
@@ -36,7 +38,14 @@ export interface StepListState<T> extends SingleSelectListState<T> {
|
|
|
36
38
|
}
|
|
37
39
|
|
|
38
40
|
export function useStepListState<T extends object>(props: StepListProps<T>): StepListState<T> {
|
|
39
|
-
let state = useSingleSelectListState<T>(
|
|
41
|
+
let state = useSingleSelectListState<T>({
|
|
42
|
+
...props,
|
|
43
|
+
onSelectionChange: props.onSelectionChange ? (key => {
|
|
44
|
+
if (key != null) {
|
|
45
|
+
props.onSelectionChange?.(key);
|
|
46
|
+
}
|
|
47
|
+
}) : undefined
|
|
48
|
+
});
|
|
40
49
|
|
|
41
50
|
let [lastCompletedStep, setLastCompletedStep] = useControlledState<Key | null>(props.lastCompletedStep, props.defaultLastCompletedStep ?? null, props.onLastCompletedStepChange);
|
|
42
51
|
const {setSelectedKey: realSetSelectedKey, selectedKey, collection} = state;
|