Skip to content

feat: Update Segment component "value" property to accept numbers #27221

Closed

Description

@Morritz

Prerequisites

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

  1. self-assigned this
    on Apr 18, 2023
  2. added
    type: feature request
    package: core
    on Apr 18, 2023
  3. removed their assignment
    on Apr 18, 2023
  4. added a commit that references this issue on May 24, 2023
    ec95ae5
  5. liamdebeasi commented on Jun 15, 2023

    @liamdebeasi
    Contributor

    This was added via #27222. The feature will be available in the next minor release of Ionic. Thank you again for the contribution!

  6. ionitron-bot commented on Jul 15, 2023

    @ionitron-bot

    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.

  7. locked and limited conversation to collaborators on Jul 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    package: coretype: feature request

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions