Repository navigation
Description
Prerequisites
- I have read the Contributing Guidelines.
- I agree to follow the Code of Conduct.
- I have searched for existing issues that already include this feature request, without success.
Describe the Feature Request
This pull request updates the value property of the segment component to accept numbers in addition to strings. Previously, the property was defined as a string, but this update extends it to be a union of string and number. This change will enable users to input numerical values into the segment component and increase its versatility.
Describe the Use Case
The updated value property of the segment component will provide users with more flexibility in their data input. Prior to this change, the segment component only accepted string values, which could be limiting in certain use cases. For instance, if a user wanted to display numerical data in a segmented format, they would have to manually convert the numbers to strings. However, with the updated value property, users can now input numerical values directly into the component. This change will improve the usability and efficiency of the segment component for users who work with numerical data.
Describe Preferred Solution
The preferred solution for this feature request is to update the value property of the segment component to accept a union of string and number types. This change should be made in a backwards-compatible manner, meaning that existing code that uses the segment component with a string value will continue to function as before. The documentation for the segment component should also be updated to reflect this change, and any examples should demonstrate the use of numerical values in addition to strings. By implementing this solution, users of the Ionic Framework will have increased flexibility in their data input and improved usability of the segment component.
Describe Alternatives
One alternative solution to this feature request would be to create a separate component specifically for numerical data input. However, this would lead to unnecessary complexity and duplication of code. Another alternative would be to continue to only accept string values in the segment component, but provide a separate utility function to convert numerical values to strings. While this approach would work, it would add an extra step for users and could potentially lead to errors. Overall, the preferred solution of updating the value property of the segment component to accept a union of string and number types is the simplest and most straightforward solution.
Related Code
Here is an example of the updated value property of the segment component with a numerical value in React:
import { useState } from 'react';
import { IonSegment, IonSegmentButton } from '@ionic/react';
function Example() {
const [selectedValue, setSelectedValue] = useState<string | number>('1');
const handleValueChange = (event) => {
setSelectedValue(event.detail.value);
};
return (
<IonSegment value={selectedValue} onIonChange={handleValueChange}>
<IonSegmentButton value="1">Option 1</IonSegmentButton>
<IonSegmentButton value="2">Option 2</IonSegmentButton>
<IonSegmentButton value="3">Option 3</IonSegmentButton>
<IonSegmentButton value={4}>Option 4</IonSegmentButton>
<IonSegmentButton value={5}>Option 5</IonSegmentButton>
</IonSegment>
);
}In this example, the value property of each segment button can now accept numerical values in addition to strings. This will allow for more versatile data input in the segment component, particularly for users who work with numerical data. The useState hook is used to manage the selected value state, and the onIonChange event handler is used to update the state when the value changes.
Additional Information
No response
Activity
feat(segment, segment-button): update segment value property to accep…
liamdebeasi commentedon Jun 15, 2023
This was added via #27222. The feature will be available in the next minor release of Ionic. Thank you again for the contribution!
ionitron-bot commentedon Jul 15, 2023
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.