"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ComboBoxControlledExample = void 0;
var tslib_1 = require("tslib");
var React = require("react");
var react_1 = require("@fluentui/react");
var react_hooks_1 = require("@fluentui/react-hooks");
var INITIAL_OPTIONS = [
    { key: 'Header1', text: 'First heading', itemType: react_1.SelectableOptionMenuItemType.Header },
    { key: 'A', text: 'Option A' },
    { key: 'B', text: 'Option B' },
    { key: 'C', text: 'Option C' },
    { key: 'D', text: 'Option D' },
    { key: 'divider', text: '-', itemType: react_1.SelectableOptionMenuItemType.Divider },
    { key: 'Header2', text: 'Second heading', itemType: react_1.SelectableOptionMenuItemType.Header },
    { key: 'E', text: 'Option E' },
    { key: 'F', text: 'Option F', disabled: true },
    { key: 'G', text: 'Option G' },
    { key: 'H', text: 'Option H' },
    { key: 'I', text: 'Option I' },
    { key: 'J', text: 'Option J' },
];
var newKey = 1;
// Optional styling to make the example look nicer
var comboBoxStyles = { root: { maxWidth: 300 } };
var stackTokens = { childrenGap: 20 };
var ComboBoxControlledExample = function () {
    var _a = React.useState('C'), selectedKey = _a[0], setSelectedKey = _a[1];
    // Manually updating the options list is only necessary when allowFreeform is true
    var _b = React.useState(INITIAL_OPTIONS), options = _b[0], setOptions = _b[1];
    var _c = react_hooks_1.useBoolean(true), allowFreeform = _c[0], toggleAllowFreeform = _c[1].toggle;
    var onChange = React.useCallback(function (event, option, index, value) {
        var key = option === null || option === void 0 ? void 0 : option.key;
        if (allowFreeform && !option && value) {
            // If allowFreeform is true, the newly selected option might be something the user typed that
            // doesn't exist in the options list yet. So there's extra work to manually add it.
            key = "" + newKey++;
            setOptions(function (prevOptions) { return tslib_1.__spreadArrays(prevOptions, [{ key: key, text: value }]); });
        }
        setSelectedKey(key);
    }, [allowFreeform]);
    return (React.createElement(react_1.Stack, { horizontal: true, tokens: stackTokens },
        React.createElement(react_1.ComboBox, { selectedKey: selectedKey, label: "Controlled single-select ComboBox", autoComplete: "on", allowFreeform: allowFreeform, options: options, onChange: onChange, styles: comboBoxStyles }),
        React.createElement(react_1.Toggle, { label: "Allow freeform", checked: allowFreeform, onChange: toggleAllowFreeform })));
};
exports.ComboBoxControlledExample = ComboBoxControlledExample;
//# sourceMappingURL=ComboBox.Controlled.Example.js.map