How to use the @zendeskgarden/container-utilities.getControlledValue function in @zendeskgarden/container-utilities

To help you get started, we’ve selected a few @zendeskgarden/container-utilities examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github zendeskgarden / react-containers / packages / selection / src / useSelection.ts View on Github external
defaultSelectedIndex,
  rtl,
  selectedItem,
  focusedItem,
  onSelect,
  onFocus
}: IUseSelectionProps = {}): UseSelectionReturnValue {
  const refs: React.MutableRefObject[] = [];
  const items: Item[] = [];

  const [state, dispatch] = useReducer(stateReducer, {
    selectedItem,
    focusedItem
  });

  const controlledFocusedItem = getControlledValue(focusedItem, state.focusedItem);
  const controlledSelectedItem = getControlledValue(selectedItem, state.selectedItem);

  useEffect(() => {
    if (controlledFocusedItem !== undefined) {
      const focusedIndex = items.indexOf(controlledFocusedItem);

      refs[focusedIndex] && refs[focusedIndex].current!.focus();
    }
  }, [controlledFocusedItem]); // eslint-disable-line react-hooks/exhaustive-deps

  useEffect(() => {
    if (selectedItem === undefined && defaultSelectedIndex !== undefined) {
      dispatch({
        type: 'KEYBOARD_SELECT',
        payload: items[defaultSelectedIndex],
        onSelect
github zendeskgarden / react-containers / packages / selection / src / useSelection.ts View on Github external
rtl,
  selectedItem,
  focusedItem,
  onSelect,
  onFocus
}: IUseSelectionProps = {}): UseSelectionReturnValue {
  const refs: React.MutableRefObject[] = [];
  const items: Item[] = [];

  const [state, dispatch] = useReducer(stateReducer, {
    selectedItem,
    focusedItem
  });

  const controlledFocusedItem = getControlledValue(focusedItem, state.focusedItem);
  const controlledSelectedItem = getControlledValue(selectedItem, state.selectedItem);

  useEffect(() => {
    if (controlledFocusedItem !== undefined) {
      const focusedIndex = items.indexOf(controlledFocusedItem);

      refs[focusedIndex] && refs[focusedIndex].current!.focus();
    }
  }, [controlledFocusedItem]); // eslint-disable-line react-hooks/exhaustive-deps

  useEffect(() => {
    if (selectedItem === undefined && defaultSelectedIndex !== undefined) {
      dispatch({
        type: 'KEYBOARD_SELECT',
        payload: items[defaultSelectedIndex],
        onSelect
      });
github zendeskgarden / react-components / packages / tabs / src / elements / Tabs.tsx View on Github external
(
    { isVertical, children, onChange, selectedItem: controlledSelectedItem, theme, ...otherProps },
    ref
  ) => {
    const [internalSelectedItem, setSelectedItem] = useState();
    const tabIndexRef = useRef(0);
    const tabPanelIndexRef = useRef(0);
    const selectedItem = getControlledValue(controlledSelectedItem, internalSelectedItem);

    const tabPropGetters = useTabs({
      rtl: theme!.rtl,
      vertical: isVertical,
      selectedItem,
      defaultSelectedIndex: 0,
      onSelect: item => {
        if (onChange) {
          onChange(item);
        } else {
          setSelectedItem(item);
        }
      }
    });

    const tabsContextValue = { ...tabPropGetters, tabIndexRef, tabPanelIndexRef };
github zendeskgarden / react-components / packages / chrome / src / views / subnav / CollapsibleSubNavItem.js View on Github external
const CollapsibleSubNavItem = ({
  header,
  children,
  expanded: controlledExpanded,
  onChange,
  ...other
}) => {
  const panelRef = useRef(undefined);
  const [internalExpanded, setInternalExpanded] = useState(controlledExpanded);
  const expanded = getControlledValue(controlledExpanded, internalExpanded);

  const { getHeaderProps, getTriggerProps, getPanelProps, expandedSections } = useAccordion({
    expandedSections: expanded ? [0] : [],
    onChange: updatedSections => {
      const isExpanded = updatedSections.length !== 0;

      if (onChange) {
        onChange(isExpanded);
      } else {
        setInternalExpanded(isExpanded);
      }
    }
  });

  useEffect(() => {
    if (expanded && panelRef.current) {
github zendeskgarden / react-components / packages / tabs / src / elements / Tabs.js View on Github external
function Tabs({
  vertical,
  children,
  onChange,
  selectedItem: controlledSelectedItem,
  ...otherProps
}) {
  const [internalSelectedItem, setSelectedItem] = useState();
  const firstItemRef = useRef();
  const selectedItem = getControlledValue(controlledSelectedItem, internalSelectedItem);

  const { getTabListProps, getTabPanelProps, getTabProps, focusedItem } = useTabs({
    rtl: isRtl(otherProps),
    vertical,
    selectedItem,
    onSelect: item => {
      if (onChange) {
        onChange(item);
      } else {
        setSelectedItem(item);
      }
    }
  });

  useEffect(() => {
    /**
github zendeskgarden / react-containers / packages / selection / src / useSelection.ts View on Github external
function stateReducer(state: IUseSelectionState, action: SELECTION_ACTION) {
  switch (action.type) {
    case 'FOCUS': {
      if (action.onFocus) {
        if (action.payload !== action.focusedItem) {
          action.onFocus(action.payload);
        }

        return state;
      }

      return { ...state, focusedItem: action.payload };
    }
    case 'INCREMENT': {
      const controlledFocusedItem = getControlledValue(action.focusedItem, state.focusedItem);
      const controlledSelectedItem = getControlledValue(action.selectedItem, state.selectedItem);
      const currentItemIndex =
        controlledFocusedItem === undefined
          ? action.items.indexOf(controlledSelectedItem)
          : action.items.indexOf(controlledFocusedItem);
      const newFocusedItem = action.items[(currentItemIndex + 1) % action.items.length];

      if (action.onFocus) {
        action.onFocus(newFocusedItem);

        return state;
      }

      return { ...state, focusedItem: newFocusedItem };
    }
    case 'DECREMENT': {
github zendeskgarden / react-containers / packages / selection / src / useSelection.ts View on Github external
function stateReducer(state: IUseSelectionState, action: SELECTION_ACTION) {
  switch (action.type) {
    case 'FOCUS': {
      if (action.onFocus) {
        if (action.payload !== action.focusedItem) {
          action.onFocus(action.payload);
        }

        return state;
      }

      return { ...state, focusedItem: action.payload };
    }
    case 'INCREMENT': {
      const controlledFocusedItem = getControlledValue(action.focusedItem, state.focusedItem);
      const controlledSelectedItem = getControlledValue(action.selectedItem, state.selectedItem);
      const currentItemIndex =
        controlledFocusedItem === undefined
          ? action.items.indexOf(controlledSelectedItem)
          : action.items.indexOf(controlledFocusedItem);
      const newFocusedItem = action.items[(currentItemIndex + 1) % action.items.length];

      if (action.onFocus) {
        action.onFocus(newFocusedItem);

        return state;
      }

      return { ...state, focusedItem: newFocusedItem };
    }
    case 'DECREMENT': {
      const controlledFocusedItem = getControlledValue(action.focusedItem, state.focusedItem);
github zendeskgarden / react-containers / packages / accordion / src / useAccordion.ts View on Github external
export function useAccordion({
  idPrefix,
  expandedSections,
  onChange,
  expandable = true,
  collapsible = true
}: IUseAccordionProps = {}): IUseAccordionReturnValue {
  const [prefix] = useState(idPrefix || generateId('garden-accordion-container'));
  const TRIGGER_ID = `${prefix}--trigger`;
  const PANEL_ID = `${prefix}--panel`;
  const [expandedState, setExpandedState] = useState(expandedSections || []);

  const controlledExpandedState = getControlledValue(onChange && expandedSections, expandedState);

  const [disabledState, setDisabledState] = useState(collapsible ? [] : controlledExpandedState);

  const sectionIndices: number[] = [];
  const toggle = (index: number) => {
    const expanded: number[] = [];
    const disabled: number[] = [];

    sectionIndices.forEach(sectionIndex => {
      let isExpanded = false;

      if (sectionIndex === index) {
        isExpanded = collapsible ? controlledExpandedState.indexOf(sectionIndex) === -1 : true;
      } else if (expandable) {
        isExpanded = controlledExpandedState.indexOf(sectionIndex) !== -1;
      }