Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset the state of the component and its inputs when a different props.value is passed in #155

Open
wants to merge 30 commits into
base: master
Choose a base branch
from

Conversation

acusti
Copy link

@acusti acusti commented Oct 15, 2021

note that this branch was branched off of #154 and will be easier to review in isolation from those changes.


this PR simplifies component state handling and updates state.value and state.input when props.value changes to enable resetting the component’s state as requested in #129 (also provides a better solution for #69). it also adds fallbacks and checks to work with props.value being null or undefined. these changes enable the following pattern, which i added as a new “With Reset” story to the docs:

function Resettable() {
    const [value, setValue] = React.useState(null);

    return (
        <Fragment>
          <ReactCodeInput onChange={() => setValue(null)} value={value} />
            <button onClick={() => setValue('')}>Reset</button>
        </Fragment>
    );
}

commits are atomic and explain in detail what changes are contained in the PR.

changes also include upgrading the package-lock.json file for the latest version of npm (7.24.0)
there is a direct import from prop-types in ReactCodeInput.js
prevents this error after upgrade to storybook v5: webpack/webpack#8082
no more need to add sass loader manually by extending the webpack config
Array.prototype.map() won’t early return if you return true, but Array.prototype.some() will
also, fix naming typo from chart → char
cleans up the code where that variable is used and ensures the value being used is always a number (e.g. in the else condition of the ternary for determining the index within this.textInput when assigning newTarget)
fixes 40818419#62 (reproducible in chrome and safari also)
• removes need for special keydown handling for ↑/↓/E keys
• removes need for styling to remove default input type="number" browser styles
• leaves filtering of non-numeric values to existing logic in handleChange (works for pasted text and keyboard entry)
when using maxLength, if user has "623 293" and pastes it into an input with fields={6}, the last digit (“3”) won’t be pasted because the max length on the focused input is 6 digits
better to check that the rendered input is disabled rather than that state has { disabled: true }
@acusti acusti changed the title Reset the state of the when a different props.value is passed in Reset the state of the component and its inputs when a different props.value is passed in Oct 15, 2021
@codecov
Copy link

codecov bot commented Oct 15, 2021

Codecov Report

Merging #155 (3593aa9) into master (2599c5d) will increase coverage by 0.44%.
The diff coverage is n/a.

❗ Current head 3593aa9 differs from pull request most recent head 61d697e. Consider uploading reports for the commit 61d697e to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master     #155      +/-   ##
==========================================
+ Coverage   96.55%   96.99%   +0.44%     
==========================================
  Files           2        2              
  Lines         145      133      -12     
  Branches       41       36       -5     
==========================================
- Hits          140      129      -11     
+ Misses          5        4       -1     
Impacted Files Coverage Δ
src/utils.js
src/ReactCodeInput.js
ReactCodeInput.js 96.72% <0.00%> (ø)
utils.js 100.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2599c5d...61d697e. Read the comment docs.

also, remove empty string default for props.value to enable alternating between passing undefined  and empty string as props.value to clear the component input values (because {value: undefined} no longer gets defaulted to empty string)
also, add getValueFromProps util to handle props.forceUppercase and null | undefined
…#129

makes it possible to reset ReactCodeInput’s internal state and the values it renders in its inputs by passing in an updated this.props.value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant