How to use the recompose/withState function in recompose

To help you get started, we’ve selected a few recompose 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 istarkov / revue / src / HOCs / dimensions.js View on Github external
(BaseComponent) => {
    const factory = createEagerFactory(BaseComponent);
    const sizePropName = sizePropNameArg || '@@__SIZE';
    const sizeSetterPropName = `@@__SET__${sizePropName}`;
    // const pickDependentProps = props => pick(props, props[dependentPropKeysName]);

    return compose(
      withState(sizePropName, sizeSetterPropName, defaultSize || { width: 0, height: 0 }),
    )(
      class extends Component {
        cancelHandler = null;

        componentDidMount() {
          window.addEventListener('resize', this.resizeHandle, false);
          this.resizeHandle();
          // allow to finish draw
          this.cancelHandler = setTimeout(this.resizeHandle, 0);
        }

        componentWillUnmount() {
          window.removeEventListener('resize', this.resizeHandle, false);
          clearTimeout(this.cancelHandler);
        }
github dhis2 / maintenance-app / src / EditModel / event-program / notifications / TrackerProgramNotifications.js View on Github external
{
            removeStageNotification,
            removeProgramNotification,
            setEditProgramStageModel: model =>
                setEditModel(model, 'PROGRAM_STAGE_NOTIFICATION'),
            setEditProgramModel: model =>
                setEditModel(model, 'PROGRAM_NOTIFICATION'),
            setAddModel,
        },
        dispatch
    );

const enhance = compose(
    // TODO: Impure connect when the reducer is fixed to emit a pure model this can be a pure action
    connect(state => ({}), mapDispatchToProps, undefined, { pure: false }),
    withState('open', 'setOpen', false),
    withState('modelToDelete', 'setModelToDelete', null),
    withHandlers({
        onCancel: ({ setOpen }) => () => setOpen(false),
        onDelete: ({
            setOpen,
            removeStageNotification,
            removeProgramNotification,
            modelToDelete,
        }) => () => {
            setOpen(false);
            if(modelToDelete.programStage) {
                removeStageNotification(modelToDelete);
            } else {
                removeProgramNotification(modelToDelete);
            }
github MCS-Lite / mcs-lite-app / client / app / components / profile / dialogs / changePasswordDialog.js View on Github external
<button kind="default">
        {t('cancel')}
      </button>
      <button kind="primary">
        {t('save')}
      </button>
    
  
);

export default compose(
  pure,
  withGetMessages(messages, 'Profile'),
  withState('newPassword', 'setNewPassword', ''),
  withState('confirmPassword', 'setConfirmPassword', ''),
  withState('error', 'setError', {
    newPassword: true,
    confirmPassword: true,
  }),
  withState('touched', 'setTouched', {
    newPassword: false,
    confirmPassword: false,
  }),
  withHandlers({
    closeDialog: props =&gt; () =&gt; props.setDialogShow('none'),
    onBlur: props =&gt; field =&gt; () =&gt; {
      props.setTouched({ ...props.touched, [field]: true });
    },
    onNewPasswordChange: props =&gt; (e) =&gt; {
      const newPassword = e.target.value;
      props.setNewPassword(newPassword);
github frodare / addon-redux / src / lib / components / HistoryPanel.js View on Github external
this.stopListeningOnStory = this.props.api.onStory(() => {
      this.props.onDispatch()
    })
  },
  componentWillUnmount () {
    const { channel } = this.props
    if (this.stopListeningOnStory) {
      this.stopListeningOnStory()
    }
    channel.removeListener(events.INIT, this.props.onInit)
    channel.removeListener(events.ON_DISPATCH, this.props.onDispatch)
  }
})

const enhance = compose(
  withState('changes', 'setChanges', []),
  withState('enabled', 'setEnabled', false),
  withState('actionFilter', 'setActionFilter', ''),
  withState('diffFilter', 'setDiffFilter', ''),
  withHandlers(buildHandlers),
  lifecycle(lifecycleHandlers)
)

export default enhance(HistoryPanel)
github MCS-Lite / mcs-lite-app / client / app / components / prototypeDetail / dialogs / selectCreateDataChannel / index.js View on Github external
setIsCreateDataChannel={setIsCreateDataChannel}
          setIsSelectCreateDataChannel={setIsSelectCreateDataChannel}
          retrieveUnitTypes={retrieveUnitTypes}
          createUnitTypes={createUnitTypes}
          unitTypes={unitTypes}
          pushToast={pushToast}
        /&gt;
      }
    
  
);

export default compose(
  pure,
  withState('displayCardType', 'setDisplayCardType', 0),
  withState('isCreateDataChannel', 'setIsCreateDataChannel', false),
  withHandlers({
    closeSelectCreateDataChannel: props =&gt; () =&gt; props.setIsSelectCreateDataChannel(false),
  }),
  withGetMessages(messages, 'PrototypeDetail'),
)(CreateDataChannelDialog);
github MCS-Lite / mcs-lite-app / client / app / components / header / dropDownButton.js View on Github external
child.props.className,
                  children.length === index + 1 ? dropButtonStyles.dropDownIconborder : {},
                ),
              }))
          }
        
      
    );
  }
}

export default compose(
  pure,
  withState('isOpen', 'setIsOpen', false),
  withState('isHover', 'setIsHover', false),
  withState('onDocumentClickListener', 'setOnDocumentClickListener', null),
  withState('onDocumentKeyupListener', 'setOnDocumentKeyupListener', null),
  withHandlers({
    onMouseEnter: props =&gt; () =&gt; props.setIsHover(true),
    onMouseLeave: props =&gt; () =&gt; props.setIsHover(false),
    handleClick: props =&gt; (e) =&gt; {
      const hide = () =&gt; {
        props.setIsOpen(false);
        if (props.onDocumentClickListener) {
          props.onDocumentClickListener.remove();
        }

        if (props.onDocumentKeyupListener) {
          props.onDocumentKeyupListener.remove();
        }
      };
github dhis2 / maintenance-app / src / EditModel / event-program / notifications / NotificationDialog.js View on Github external
import withProps from 'recompose/withProps';
import { createStepperFromConfig } from '../../stepper/stepper';
import {
    setEditModel,
    saveStageNotification,
    saveProgramNotification,
} from './actions';
import {
    getProgramStageDataElementsByStageId,
    getNotificationType,
} from './selectors';
import Subheader from 'material-ui/Subheader/';
import { branchWithMessage } from '../../../Snackbar/snackBarUtils';

const withStepper = compose(
    withState('activeStep', 'setActiveStep', 0),
    withProps(({ setActiveStep, dataElements }) => ({
        stepperClicked(stepKey) {
            setActiveStep(
                programStageSteps.findIndex(step => step.key === stepKey)
            );
        },
        dataElements,
    }))
);

const stepperForSteps = steps =>
    withStepper(createStepperFromConfig(steps, 'vertical'));

const ProgramStageStepper = stepperForSteps(programStageSteps);
const ProgramStepper = stepperForSteps(programSteps);
github MCS-Lite / mcs-lite-app / client / app / components / header / dropDownButton.js View on Github external
className: c(
                  child.props.className,
                  children.length === index + 1 ? dropButtonStyles.dropDownIconborder : {},
                ),
              }))
          }
        
      
    );
  }
}

export default compose(
  pure,
  withState('isOpen', 'setIsOpen', false),
  withState('isHover', 'setIsHover', false),
  withState('onDocumentClickListener', 'setOnDocumentClickListener', null),
  withState('onDocumentKeyupListener', 'setOnDocumentKeyupListener', null),
  withHandlers({
    onMouseEnter: props =&gt; () =&gt; props.setIsHover(true),
    onMouseLeave: props =&gt; () =&gt; props.setIsHover(false),
    handleClick: props =&gt; (e) =&gt; {
      const hide = () =&gt; {
        props.setIsOpen(false);
        if (props.onDocumentClickListener) {
          props.onDocumentClickListener.remove();
        }

        if (props.onDocumentKeyupListener) {
          props.onDocumentKeyupListener.remove();
        }
      };
github tkh44 / cuneiform / src / index.js View on Github external
import withHandlers from "recompose/withHandlers";

const payloadKeys = [
  "children",
  "getValue",
  "setValue",
  "isPristine",
  "isDirty",
  "onSubmit",
  "formValues",
  "resetForm",
  "updateValues"
];

export default compose(
  withState(
    "formValues",
    "updateValues",
    ({ initialValues = {} }) => initialValues
  ),
  withHandlers({
    resetForm: ({ initialValues, updateValues }) => {
      return () => updateValues(initialValues);
    },
    isPristine: ({ formValues, initialValues, updateValues }) => {
      return deepEqual(formValues, initialValues);
    },
    isDirty: ({ formValues, initialValues, updateValues }) => {
      return () => !deepEqual(formValues, initialValues);
    },
    getValue: ({ formValues }) => {
      return name => formValues[name];
github MCS-Lite / mcs-lite-app / client / app / components / toastCenter / index.js View on Github external
{
        isToastShow &amp;&amp;
        
          <div>
            {toast.message}
          </div>
        
      }
    
  
);

export default compose(
  pure,
  withState('isToastShow', 'setIsToastShow', false),
  withState('toast', 'setToast', {}),
  lifecycle({
    componentWillReceiveProps(nextProps) {
      const toastCount = pipe(
        pathOr([], ['toasts', 'toastList']),
        length,
      )(nextProps);

      if (toastCount &gt; 0 &amp;&amp; !nextProps.isToastShow) {
        const toast = pathOr({}, ['toasts', 'toastList', 0])(nextProps);
        this.props.setIsToastShow(true);
        this.props.setToast(toast);

        setTimeout(() =&gt; {
          this.props.dropToast();
          this.props.setIsToastShow(false);
        }, 3000);