@navikt/ds-react
Version:
React components from the Norwegian Labour and Welfare Administration.
71 lines • 3.11 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.useComboboxCustomOptions = exports.CustomOptionsProvider = void 0;
const react_1 = __importStar(require("react"));
const create_context_1 = require("../../util/create-context");
const Input_context_1 = require("./Input/Input.context");
const [ComboboxCustomOptionsProvider, useComboboxCustomOptions] = (0, create_context_1.createContext)({
name: "ComboboxCustomOptions",
errorMessage: "useComboboxCustomOptions must be used within a ComboboxCustomOptionsProvider",
});
exports.useComboboxCustomOptions = useComboboxCustomOptions;
const CustomOptionsProvider = ({ children, value, }) => {
const [customOptions, setCustomOptions] = (0, react_1.useState)([]);
const { focusInput } = (0, Input_context_1.useInputContext)();
const { isMultiSelect } = value;
const removeCustomOption = (0, react_1.useCallback)((option) => {
setCustomOptions((prevCustomOptions) => prevCustomOptions.filter((o) => o.label !== option.label));
focusInput();
}, [focusInput]);
const addCustomOption = (0, react_1.useCallback)((option) => {
if (isMultiSelect) {
setCustomOptions((prevOptions) => [...prevOptions, option]);
}
else {
setCustomOptions([option]);
}
focusInput();
}, [focusInput, isMultiSelect]);
const customOptionsState = {
customOptions,
removeCustomOption,
addCustomOption,
setCustomOptions,
};
return (react_1.default.createElement(ComboboxCustomOptionsProvider, Object.assign({}, customOptionsState), children));
};
exports.CustomOptionsProvider = CustomOptionsProvider;
//# sourceMappingURL=customOptionsContext.js.map