Skip to content

Instantly share code, notes, and snippets.

@joeporpeglia
Last active June 30, 2017 16:25
Show Gist options
  • Select an option

  • Save joeporpeglia/aab0f72ed26959a95575c6a246d89049 to your computer and use it in GitHub Desktop.

Select an option

Save joeporpeglia/aab0f72ed26959a95575c6a246d89049 to your computer and use it in GitHub Desktop.

Revisions

  1. joeporpeglia revised this gist Jun 29, 2017. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion behavior-with-state-and-structure.js
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ document
    handleSave
    );

    const isSaving = false;
    let isSaving = false;

    const handleSave = () => {
    if (isSaving) {
    2 changes: 1 addition & 1 deletion behavior-with-state.js
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ document
    handleSave
    );

    const isSaving = false;
    let isSaving = false;

    const handleSave = () => {
    if (isSaving) {
  2. joeporpeglia revised this gist Jun 29, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions withDropdownState.jsx
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    const withDropdownState = WrappedComponent => {
    return class extends Component {
    // Initialize state in constructor
    render() {
    return (
    <WrappedComponent
  3. joeporpeglia revised this gist Jun 29, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions redux-recompose.js
    Original file line number Diff line number Diff line change
    @@ -6,10 +6,10 @@ const withCounter = connect(
    const withCounter = compose(
    withState('value', 'setValue', 0),
    withHandlers({
    onIncrement: ({ setValue, value }) => {
    onIncrement: ({ setValue, value }) => () => {
    setValue(value + 1);
    },
    onDecrement: ({ setValue, value }) => {
    onDecrement: ({ setValue, value }) => () => {
    setValue(value - 1);
    },
    })
  4. joeporpeglia revised this gist Jun 29, 2017. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions higher-order-function.jsx
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,11 @@ const increment = num => num + 1;

    const incrementWithLog = withLog(increment);

    const withLog = WrappedComponent => (props) => {
    console.log('rendered with props:', props);
    return <WrappedComponent {...props} />;
    }

    const Button = props => <button {...props} />;

    const ButtonWithLog = withLog(Button);
  5. joeporpeglia revised this gist Jun 29, 2017. 2 changed files with 12 additions and 3 deletions.
    10 changes: 8 additions & 2 deletions Dropdown.jsx
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,21 @@
    type DropdownProps = {
    expanded: boolean,
    value: string | number,
    onChange: (string | number) => void
    onChange: (string | number) => void,
    onExpand: () => void,
    onCollapse: () => void,
    options: {
    [optionValue: string | number]: string | React$Element
    },
    };

    const Dropdown = props => (
    <div>
    <button aria-haspopup="true" aria-expanded={props.expanded}>
    <button
    aria-haspopup="true"
    aria-expanded={props.expanded}
    onClick={props.expanded ? props.onCollapse : props.onExpand}
    >
    {props.options[props.value]}
    </button>
    <ul aria-hidden={!props.expanded}>
    5 changes: 4 additions & 1 deletion withDropdownHandlers.jsx
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,10 @@ const withDropdownHandlers = WrappedComponent => {
    {...props}
    onExpand={() => props.setExpanded(true)}
    onCollapse={() => props.setExpanded(false)}
    onChange={props.setValue}
    onChange={value => {
    props.setValue(value);
    props.setExpanded(false);
    }
    />
    );
    };
  6. joeporpeglia revised this gist Jun 29, 2017. 1 changed file with 7 additions and 2 deletions.
    9 changes: 7 additions & 2 deletions Dropdown.jsx
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,10 @@
    type DropdownProps = {
    expanded: boolean,
    value: string | number,
    options: { [optionValue: string | number]: string | React$Element },
    onChange: (string | number) => void
    options: {
    [optionValue: string | number]: string | React$Element
    },
    };

    const Dropdown = props => (
    @@ -12,7 +15,9 @@ const Dropdown = props => (
    <ul aria-hidden={!props.expanded}>
    {Object.keys(props.options).map(value => (
    <li key={value}>
    {props.options[value]}
    <button onClick={() => props.onChange(value)}>
    {props.options[value]}
    </button>
    </li>
    ))}
    </ul>
  7. joeporpeglia revised this gist Jun 29, 2017. 1 changed file with 10 additions and 3 deletions.
    13 changes: 10 additions & 3 deletions redux-recompose.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,16 @@
    const withCounter = connect(selectCounterState, counterActions);
    const withCounter = connect(
    selectCounterState,
    counterActions
    );

    const withCounter = compose(
    withState('value', 'setValue', 0),
    withHandlers({
    onIncrement: ({ setValue, value }) => setValue(value + 1),
    onDecrement: ({ setValue, value }) => setValue(value - 1),
    onIncrement: ({ setValue, value }) => {
    setValue(value + 1);
    },
    onDecrement: ({ setValue, value }) => {
    setValue(value - 1);
    },
    })
    );
  8. joeporpeglia revised this gist Jun 29, 2017. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions redux-recompose.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    const withCounter = connect(selectCounterState, counterActions);

    const withCounter = compose(
    withState('value', 'setValue', 0),
    withHandlers({
    onIncrement: ({ setValue, value }) => setValue(value + 1),
    onDecrement: ({ setValue, value }) => setValue(value - 1),
    })
    );
  9. joeporpeglia revised this gist Jun 29, 2017. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions layout-content.jsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    const Article = (props) => (
    <article>
    <header>{props.header}</header>
    <main>{props.content}</main>
    <footer>{props.footer}</footer>
    </article>
    );

    const Strong = (props) => (
    <strong>{props.children}</strong>
    );
  10. joeporpeglia renamed this gist Jun 29, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  11. joeporpeglia revised this gist Jun 29, 2017. 2 changed files with 12 additions and 0 deletions.
    File renamed without changes.
    12 changes: 12 additions & 0 deletions higher-order-function.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    const withLog = wrappedFn => (...args) => {
    console.log('called with args:', args);
    return wrappedFn(...args);
    }

    const increment = num => num + 1;

    const incrementWithLog = withLog(increment);

    const Button = props => <button {...props} />;

    const ButtonWithLog = withLog(Button);
  12. joeporpeglia revised this gist Jun 29, 2017. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions compose.jsx
    Original file line number Diff line number Diff line change
    @@ -4,4 +4,8 @@ const Composed = () => (
    <Outer>
    <Inner />
    </Outer>
    );

    const Composed = () => (
    <Outer children={<Inner />} />
    );
  13. joeporpeglia revised this gist Jun 29, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion compose.jsx
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    const composed = () => outer(inner(...args));
    const composed = () => outer(inner());

    const Composed = () => (
    <Outer>
  14. joeporpeglia revised this gist Jun 29, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion compose.jsx
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    const compose2 = (f, g) => (...args) => f(g(...args));
    const composed = () => outer(inner(...args));

    const Composed = () => (
    <Outer>
  15. joeporpeglia revised this gist Jun 29, 2017. 6 changed files with 61 additions and 0 deletions.
    18 changes: 18 additions & 0 deletions behavior-with-state-and-structure.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    document
    .querySelector('.btn-save')
    .addEventListener(
    'click',
    handleSave
    );

    const isSaving = false;

    const handleSave = () => {
    if (isSaving) {
    return;
    }

    isSaving = true;
    showLoader();
    saveForm();
    };
    17 changes: 17 additions & 0 deletions behavior-with-state.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    document
    .querySelector('.btn-save')
    .addEventListener(
    'click',
    handleSave
    );

    const isSaving = false;

    const handleSave = () => {
    if (isSaving) {
    return;
    }

    isSaving = true;
    saveForm();
    };
    10 changes: 10 additions & 0 deletions behavior.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    document
    .querySelector('.btn-save')
    .addEventListener(
    'click',
    handleSave
    );

    const handleSave = () => {
    saveForm();
    };
    7 changes: 7 additions & 0 deletions compose.jsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    const compose2 = (f, g) => (...args) => f(g(...args));

    const Composed = () => (
    <Outer>
    <Inner />
    </Outer>
    );
    4 changes: 4 additions & 0 deletions presentation.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    .btn-save {
    background: #1ab7ea;
    color: #ffffff;
    }
    5 changes: 5 additions & 0 deletions semantic-structure.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    <form>
    <button class=”btn-save”>
    Save
    </button>
    </form>
  16. joeporpeglia revised this gist Jun 29, 2017. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion OriginalDropdownProps.jsx
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    type DropdownProps = {
    className: string,
    options: string[] | number[] | Element[],
    options: Array<string>
    | Array<number>
    | Array<{ label: React$Element, value: string | number }>
    onChange: Function,
    placeholder: string | Element,
    value: string | number,
  17. joeporpeglia renamed this gist Jun 29, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  18. joeporpeglia revised this gist Jun 29, 2017. 5 changed files with 55 additions and 10 deletions.
    20 changes: 20 additions & 0 deletions Dropdown.jsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    type DropdownProps = {
    expanded: boolean,
    value: string | number,
    options: { [optionValue: string | number]: string | React$Element },
    };

    const Dropdown = props => (
    <div>
    <button aria-haspopup="true" aria-expanded={props.expanded}>
    {props.options[props.value]}
    </button>
    <ul aria-hidden={!props.expanded}>
    {Object.keys(props.options).map(value => (
    <li key={value}>
    {props.options[value]}
    </li>
    ))}
    </ul>
    </div>
    );
    11 changes: 11 additions & 0 deletions DropdownProps.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    type DropdownProps = {
    className: string,
    options: string[] | number[] | Element[],
    onChange: Function,
    placeholder: string | Element,
    value: string | number,
    defaultValue: string | number,
    readOnly: boolean,
    optionsMaxHeight: number,
    calculateWidth: boolean,
    };
    10 changes: 0 additions & 10 deletions dropdown-behavior.js
    Original file line number Diff line number Diff line change
    @@ -1,10 +0,0 @@
    const withDropdownHandlers = WrappedComponent => {
    return props => (
    <WrappedComponent
    {...props}
    onExpand={...}
    onCollapse={...}
    onChange={...}
    />
    );
    };
    10 changes: 10 additions & 0 deletions withDropdownHandlers.jsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    const withDropdownHandlers = WrappedComponent => {
    return props => (
    <WrappedComponent
    {...props}
    onExpand={() => props.setExpanded(true)}
    onCollapse={() => props.setExpanded(false)}
    onChange={props.setValue}
    />
    );
    };
    14 changes: 14 additions & 0 deletions withDropdownState.jsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    const withDropdownState = WrappedComponent => {
    return class extends Component {
    render() {
    return (
    <WrappedComponent
    {...this.props}
    expanded={this.state.expanded}
    value={this.state.value}
    setExpanded={expanded => this.setState({ expanded })}
    setValue={value => this.setState({ value })}
    />
    );
    }
    };
  19. joeporpeglia created this gist Jun 29, 2017.
    10 changes: 10 additions & 0 deletions dropdown-behavior.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    const withDropdownHandlers = WrappedComponent => {
    return props => (
    <WrappedComponent
    {...props}
    onExpand={...}
    onCollapse={...}
    onChange={...}
    />
    );
    };