We also use React Hooks to manage the state and the example is based off of the Ionic ReactJS Template so we are supporting Typescript.
Setting Up The Component
importReactfrom"react";import{PickerColumn}from"@ionic/core";import{IonPicker}from"@ionic/react";/**
* the component has three properties that are defined in this
* interface since we are using typescript
*/interface_Props{isOpen:booleanonSave:FunctiononCancel:Function}constMyPicker:React.FC<_Props>=({onSave,onCancel,isOpen})=>{return<div></div>}
Picker properties that will be handled
onSave, when the user selects the options from the picker, the onSave method will be called to return the values that are selected.
onCancel, when the onCancel is selected, no actions are taking inside of the component in this example
isOpen, a property that is passed into the component to let the component determine if it should be visible
What the Picker Will Render
Setting up the two columns of data for the picker component. Since this component will allow the user to select the day of the week and the session time of morning or afternoon
We use the IonPicker Component and pass it the appropriate properties. isOpen which is passed in from the parent component. The columns are the constants we created above DayColumn and SessionTimeColumn and then finally the buttons for the available actions the user can take.
We also use the event properties for onSave and onCancel to let the parent container know what events were trigger in the component and pass back values where appropriate.
When a user selects the specific items from the picker, below is an example of the object that is return in the onSave function. This is the information that will be passed back to the parent component when the function is executed.
We need the appropriate imports in the App.tsx file to utilize the MyPicker Component and also react state hooks to manage the visibility of the Picker and track the selected values from the picker.
We are using typescript in this react example so it is helpful to define the interface for the state information we are tracking for the user selection from MyPicker component.
Next we render another IonItem wherethe user selection is shown from the state variable sessionTime and allow the user to change the selection when the item is clicked by calling setPickerIsOpen to update the state and cause the MyPicker Component to be rendered.
Finally we render the actual MyPicker Component using the state variables and the appropriate functions passed as properties.
In the onCancel event handler, we set state of the pickerIsOpen property using setPickerIsOpen(false).
In the onSave we need to set the results from MyPicker to the local state using setSessionTime and we also set the visibility of the picker setPickerIsOpen(false)
This is a sample Ionic ReactJS Application using the IonPicker Component derived from plain javascript example provided in the Ionic Framework Picker Documentation.
Clearly Innovative is a minority-owned solutions provider that develops digital products. We shape ideas into viable products and transform client needs into enhanced technology solutions. As a leader in early adoption and implementation of cutting edge technologies, Clearly Innovative provides services focused on product strategy, user experience, design and development. According to CEO, Aaron Saunders "We are not just designers and developers, but end-to-end digital solution providers." Clearly Innovative has created a tech education program, Clearly Innovative Education, whose mission is to create a world where people from underrepresented backgrounds can have a seat at the digital table as creators, innovators and entrepreneurs.
#TheFutureIsWrittenInCode
The Future is Written in Code series, as part of Inclusive Innovation Incubator, provides introductory and advanced programming classes as well as coding courses with a focus on business and entrepreneurship. Select programming offered includes Coding, UI/UX, Coding & Business, Coding & Entrepreneurship, Business Canvassing, Entrepreneurship: Developing Your Idea into App, to name a few. Please contact info@in3dc.com to find out more!