-
-
Notifications
You must be signed in to change notification settings - Fork 58
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
Add bind isChecked to onPress #502
Add bind isChecked to onPress #502
Conversation
Hello @vladbelozertsev Thank you so much for the contribution, let me review and test it :) |
Hi. I add little changes that improve code quality. |
Hi @WrathChaos. Is there any problem? In some cases we need uncheckable checkboxes or checkable after some validation. After disableBuiltInState been removed i cant make this. Could you please merge this pr or provide similar functional? |
Hey @vladbelozertsev Extremely sorry, I could not spend time on open source nowadays but I am working on it finally :) I checked the code and your explanation but I don't understand some things so please explain it to me.
Like this one, I am assuming you're controlling the check status with useState or something else on your side to put it as
|
Hi @WrathChaos.
In this example we try to sync developer outer state with the inner lib state. But it doesn't work if after some validation inside onPress we don't toggle our outer state, and we have two different states - developer outer state for example true and lib inner state for example false. Because inner lib state don't have the same validation, and always toggle value.
Yes, main idea is: if developer provide his own state by passing But in current moment lib can break this logic by call This code fix the issue: if developer don't provide their own state i.e. |
Imo for those who want use only lib state mb will be better to provide additional props value, something like isCheckedDefault. Who want to use their own state can set isChecked prop, otherwise we can set isCheckedDefault (initial value for the lib state) These two props values (isChecked/isCheckedDefault) are incompatible. I can write some additional code... Wait for response, thanks! |
Hello @vladbelozertsev If possible can you give me a minimum example so I can work on it to reproduce the same issue and try to provide a bit better solution for everyone? |
Hi @WrathChaos
Yes np. Its example from my app, that i currently working on. As you can see, here is the basic services, that user can't toggle (base tariff, documents fee) and additional services, that user can add / remove. |
Do you mind share the code with me? I only care the states, checkboxes and how you handle the checkboxes part. You can send me an email if you want it privately: [email protected] |
This is a code from the example above. I use my own BouncyCheckbox component ( import BouncyCheckbox from 'src/components/checkbox';
import React, { FC } from 'react';
import { AddBookingForm } from 'src/api/bookings';
import { MyText } from 'src/components/my-text';
import { RouteTariffAPI } from 'src/types/route-api';
import { ScaledSheet, ms } from 'react-native-size-matters';
type Props = {
tarifs?: RouteTariffAPI[];
field: Field<AddBookingForm, 'CheckedTariffs'>;
fieldState: FieldState;
};
export const Tariffs: FC<Props> = props => {
const { tarifs, field } = props;
return tarifs?.map(tr => {
const arr = field?.value || [];
const isChecked = arr.includes(tr.ID);
const isRequired = tr.Required?.toString() === '1' || tr.Group === '1';
const addChecked = () => field.onChange([...arr, tr.ID]);
const delChecked = () => field.onChange(arr.filter(id => id !== tr.ID));
return (
<BouncyCheckbox
fillColor={CLR_APP}
iconImageStyle={{ width: ms(12), height: ms(12) }}
iconStyle={styles.checkboxContainer}
innerIconStyle={styles.innerIcon}
isChecked={isRequired || isChecked}
key={tr.ID}
onPress={isRequired ? undefined : isChecked ? delChecked : addChecked}
size={ms(34)}
style={styles.checkbox}
textComponent={<MyText $v={tr.Name + ' ' + +tr.Value + '$'} $fs="16" />}
/>
);
});
};
const styles = ScaledSheet.create({
checkbox: {
marginBottom: '25@ms',
},
checkboxContainer: {
borderColor: '#FFF',
marginRight: '5@ms',
borderRadius: 5,
},
innerIcon: {
borderRadius: 5,
borderWidth: 3,
},
}); |
@vladbelozertsev |
hi @WrathChaos. And we will have 2 cases:
Examples: I think its right solution, because when we pass isChecked prop it makes illusion that checkbox state value always been controlled by this (isChecked) value. |
This fix would be greatly appreciated! |
Hello @vladbelozertsev and @DTX-Elliot I released 4.1.1 version with the Simply we just need to Thank you so much for the patience! |
New version is available to break the breaking change 🤭 Please move to 4.1.2 |
Sorry for the late answer. Its worked for me, thanks! |
Hi.
I add bind to isChecked prop. Because when isChecked is defined as props from an app
example: <BouncyCheckbox isChecked={someValue} />
, and onPress action happened it don't see that isChecked is defined and set new checked (by toggling current checked value) regardless of isChecked prop. And we have two different states - lib state checked for example is false and app state checked for example is true. It means we have unchecked (false) checkbox on screen and checked (true) state app value.Please check the changes, thanks!