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

How we can clear the old value? #23

Closed
sajidfrommerqata opened this issue Nov 21, 2020 · 8 comments
Closed

How we can clear the old value? #23

sajidfrommerqata opened this issue Nov 21, 2020 · 8 comments
Assignees
Labels
enhancement New feature or request

Comments

@sajidfrommerqata
Copy link

How we can clear the old value without a page refresh?

I'm using react-datalist-input in bootstrap modal. I want to clear the textbox when someone clicks on the close button of the modal.

Screenshot: https://prnt.sc/vn866j

Thanks

@andrelandgraf
Copy link
Owner

You could probably make use of initialValue.

const YourComponent = ({items}) => {
  const [userSelectedValue, setUserSelectedValue] = useState('');

  const onSelect = useCallback((selectedItem) => {
     setUserSelectedValue(selectedItem.label);
     ...
    }, []);

  const onClose = useCallback(() => {
    setUserSelectedValue('');
    ...
  }, [setUserSelectedValue]);
  
  return (
     <Modal>
         ....
         <DataListInput
           placeholder="Select an option from the drop down menu..."
          items={items}
          onSelect={onSelect}
          initialValue={userSelectedValue}
         />
        ....
    </Modal>
  );
};

Let me know if that works!

@margadtaikhir
Copy link

Hi,
I'm trying to clear the input as well,
your suggested solution doesn't work.

maybe we could have ref prop that will have the input ref.

thanks.

@andrelandgraf
Copy link
Owner

andrelandgraf commented Nov 25, 2020

Hi @margadt,

Thanks for your feedback. Yeah, I was testing around with it earlier and I think the cleanest solution would be to introduce an optional value (type string) prop that let's you manage the value of the input field directly.

I would probably deprecate initialValue since it would be confusing to have both (I guess).

Internally I would replace initialValue with value and just do a:

useEffect(() => {
  if (value !== currentInput) {
    setCurrentInput(value)
  }
}, [value, currentInput])

(Currently, initalValue works kinda the same but I am only setting current input if it's empty and initialValue is not to protect the user input when the user is typing).

So in case you need to manipulate the input field value in edge cases, you will have to manage the state of the input value on your own. The package already offers the onSelect and onInput lifecycle methods.

Would that work for you?

@andrelandgraf andrelandgraf self-assigned this Nov 25, 2020
@andrelandgraf andrelandgraf added the enhancement New feature or request label Nov 25, 2020
@margadtaikhir
Copy link

Hi @andrelandgraf ,

Thank you for the fast response,
value prop would be great!

I'm trying to implement clean button like @sajidfrommerqata ,
onSelect/onInput wouldn't be invoked onclick on the clean button.
I guess I'll build my own data-list component for now.

great package though,

@andrelandgraf
Copy link
Owner

andrelandgraf commented Nov 25, 2020

Your clean button would need to setValue('') of the value prop that you pass down to the component. You would probably need to create two callback functions for onSelect and onInput to setValue(currentInput) so that your value is always in sync with the user input.

There is a similar package react-select that makes use of this. You could use that package as well.

@andrelandgraf
Copy link
Owner

andrelandgraf commented Nov 26, 2020

Published as 2.1.1 🎉 Have a look at the updated documentation on how to use the new value prop. It's optional to pass value but if you do, you will have to update the value based on the lifecycle methods like onInput and onSelect. This way you have full control to clear the input field by setting value to empty string.

@margadtaikhir
Copy link

Published as 2.1.1 🎉 Have a look at the updated documentation on how to use the new value prop. It's optional to pass value but if you do, you will have to update the value based on the lifecycle methods like onInput and onSelect. This way you have full control to clear the input field by setting value to empty string.

Works great, thanks for the patch.

@sajidfrommerqata
Copy link
Author

sajidfrommerqata commented Dec 17, 2020

Thank you for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants