const handleSelection = (type) => { setIsSelected({ …isSelected, [type]:…
- User
const handleSelection = (type) => { setIsSelected({ …isSelected, [type]: !isSelected[type], }); };
// usage <Button title={isSelected.planning ? ‘Planning selected’ : ‘Select planning’} onPress={() => handleSelection(‘planning’)} /> <Button title={isSelected.urgent ? ‘Urgent selected’ : ‘Select urgent’} onPress={() => handleSelection(‘urgent’)} /> update this code to disabled button when other enabled
- Assistant
const handleSelection = (type) => { setIsSelected({ ...isSelected, [type]: !isSelected[type], }); };
const isDisabled = (type) => { for (let key in isSelected) { if (key !== type && isSelected[key]) { return true; } } return false; };
// usage <Button title={isSelected.planning ? ‘Planning selected’ : ‘Select planning’} onPress={() => handleSelection(‘planning’)} disabled={isDisabled(‘planning’)} /> <Button title={isSelected.urgent ? ‘Urgent selected’ : ‘Select urgent’} onPress={() => handleSelection(‘urgent’)} disabled={isDisabled(‘urgent’)} />
- User
<TouchableOpacity style={[ styles.buttonBottomSheet, { backgroundColor: isSelected.planning ? colors.transparent : colors.secondary, }, ]} onPress={() => handlSelect('planning')}> <Text style={[ styles.labelButton, { color: isSelected.planning ? colors.secondary : colors.background, }, ]}> {t('home.homeAppointment')}
<TouchableOpacity style={[ styles.buttonBottomSheet, { backgroundColor: isSelected.urgent ? colors.transparent : colors.secondary, }, ]} onPress={() => handlSelect('urgent')}> <Text style={[ styles.labelButton, { color: isSelected.urgent ? colors.secondary : colors.background, }, ]}> {t('home.homeCare')} </Text> </TouchableOpacity> if the isSelected.planning cange the color and isSelected.urgent not changing - Assistant
You can use the
disabledprop of TouchableOpacity to disable the second button if the first button is selected. Here's an updated code:<TouchableOpacity style={[ styles.buttonBottomSheet, { backgroundColor: isSelected.planning ? colors.transparent : colors.secondary, }, ]} onPress={() => handleSelection('planning')} > <Text style={[ styles.labelButton, { color: isSelected.planning ? colors.secondary : colors.background, }, ]} > {t('home.homeAppointment')} </Text> </TouchableOpacity> <TouchableOpacity style={[ styles.buttonBottomSheet, { backgroundColor: isSelected.urgent ? colors.transparent : colors.secondary, }, ]} onPress={() => handleSelection('urgent')} disabled={isSelected.planning} > <Text style={[ styles.labelButton, { color: isSelected.urgent ? colors.secondary : colors.background, }, ]} > {t('home.homeCare')} </Text> </TouchableOpacity>In the second TouchableOpacity, the
disabledprop is set to , which means that if is true, the second TouchableOpacity will be disabled. So when is true and the first button is already selected, the second button will be disabled and its color won't change. - User
with this solution we can select the 2 touchable opacity in same time but i want to select one and the second take the opositive state of the touchable selected
- Assistant
In order to achieve that behavior, you can update the
handleSelectionfunction to first reset all the other selections when a button is selected, and then toggle the selected button. Here is the updated code:const handleSelection = (type) => { setIsSelected((prev) => { const newState = {}; for (const key in prev) { newState[key] = key === type ? !prev[key] : false; } return { ...prev, ...newState, }; }); };In this updated code, we are using the
prevparameter in thesetIsSelectedfunction, which represents the previous state of theisSelectedobject. We are creating anewStateobject that will contain the updated state of all the buttons. We loop over all keys in theprevobject, set the value of the current key tofalseif it is not the selected key, and toggle the value of the selected key. Finally, we merge theprevStateandnewStateobjects to create the updated state.This way, when a button is selected, the other button will automatically be unselected. And their colors will be updated accordingly.
- User
const [isSelected, setIsSelected] = useState({ planning: false, urgent: false, }); const handlSelect = type => { console.log({typeRDV: type}); setIsSelected(prev => { const newState = {}; for (const key in prev) { newState[key] = key === type ? !prev[key] : false; } return { ...prev, ...newState, }; }); setTypeRequest(type); }; <TouchableOpacity style={[ styles.buttonBottomSheet, { backgroundColor: isSelected.planning ? colors.transparent : colors.secondary, }, ]} onPress={() => handlSelect('planning')}> <Text style={[ styles.labelButton, { color: isSelected.planning ? colors.secondary : colors.background, }, ]}> {t('home.homeAppointment')}
- Assistant
It looks like the code you posted is the same code that we discussed earlier. However, if you want to update the code to set the opposite state of the second button, you can modify the
handleSelectionfunction to set the opposite state of the second button when the first button is selected and vice versa. Here is the updated code:const handleSelection = (type) => { setIsSelected((prev) => { const newState = {}; for (const key in prev) { if (key === type) { newState[key] = !prev[key]; } else if (type === 'planning') { newState[key] = prev[key] ? false : prev[key]; } else if (type === 'urgent') { newState[key] = prev[key] ? false : prev[key]; } } return { ...prev, ...newState, }; }); };In this updated code, we are checking if the selected key is the same as the current key. If it is, we toggle the value of the current key. If it is not, we check the value of the selected key. If the selected key is "planning" and has a truthy value ( or a value that can be coerced to ), we set the current key to . If the selected key is "urgent" and has a truthy value, we also set the current key to . Finally, we merge the and objects to create the updated state.
- User
rewrite all code
- Assistant
- User
this not the solution i want , when i selected one of the touchableopacity le second touchable take the oposit state if the isSelected.plannig is true and and the type is planning and the color is change the isSelected.urgent take false and is the isSelected.urgent is true isSelected.planning take false and the color change
- Assistant
- User
name a commit of gitlab that discription this work
- Assistant
Here's a possible commit message that could describe this work:
feat: update button selection logic in HomeScreen component Updated the button selection logic in the HomeScreen component to allow only one button to be selected at a time. When one button is selected, the other button will take the opposite state and its color will be updated accordingly. Refactored the code to use the useState hook to manage the component state and to use the TouchableOpacity component for the buttons with different background colors when selected or unselected.Feel free to adjust the commit message as per your requirements.
- User
compare 2 string and return the % of match between the 2 string
- Assistant
To compare two strings and return the percentage of match between them, you can use the Levenshtein distance algorithm. This algorithm calculates the minimum number of single-character edits (insertions, deletions, or substitutions) required to change one word into the other. You can then use the formula
(1 - (distance / length of longer string)) * 100to calculate the percentage of similarity between the two strings, wheredistanceis the Levenshtein distance and thelength of longer stringis the length of the longer of the two strings.Here's an example implementation of this approach in JavaScript:
- User
now we have a string str="pediatree" and we have a list contain items compare the string with items of list and return the item that have more than 50% of matching
- Assistant
Here's an implementation in JavaScript that takes a string
strand a list of stringslistand returns the item in the list that has more than 50% matching:function findMatch(str, list) { let bestMatch = null; let bestSimilarity = 0; for (let i = 0; i < list.length; i++) { const similarity = compareStrings(str, list[i]); if (similarity > 50 && similarity > bestSimilarity) { bestMatch = list[i]; bestSimilarity = similarity; } } return bestMatch; } function compareStrings(str1, str2) { const len1 = str1.length; const len2 = str2.length; const matrix = []; for (let i = 0; i <= len1; i++) { matrix[i] = [i]; } for (let j = 0; j <= len2; j++) { matrix[0][j] = j; } for (let i = 1; i <= len1; i++) { for (let j = 1; j <= len2; j++) { if (str
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.