diff --git a/example/storybook-nativewind/babel.config.js b/example/storybook-nativewind/babel.config.js index 59f28c28d4..b0083a2920 100644 --- a/example/storybook-nativewind/babel.config.js +++ b/example/storybook-nativewind/babel.config.js @@ -39,6 +39,10 @@ module.exports = function (api) { __dirname, '../../packages/unstyled/input/src' ), + '@gluestack-ui/time-input': path.resolve( + __dirname, + '../../packages/unstyled/time-input/src' + ), '@gluestack-ui/pin-input': path.resolve( __dirname, '../../packages/unstyled/pin-input/src' diff --git a/example/storybook-nativewind/src/components/TimeInput/TimeInput.stories.tsx b/example/storybook-nativewind/src/components/TimeInput/TimeInput.stories.tsx new file mode 100644 index 0000000000..c21402ac3d --- /dev/null +++ b/example/storybook-nativewind/src/components/TimeInput/TimeInput.stories.tsx @@ -0,0 +1,45 @@ +import type { ComponentMeta } from '@storybook/react-native'; +import TimeInput from './TimeInput'; + +const TimeInputMeta: ComponentMeta = { + title: 'stories/TimeInput', + component: TimeInput, + // metaInfo is required for figma generation + // @ts-ignore + metaInfo: { + componentDescription: `The TimeInput component is designed to take the time from user in the form of day.js object`, + }, + argTypes: { + size: { + control: 'select', + options: ['sm', 'md', 'lg', 'xl'], + }, + variant: { + control: 'select', + options: ['outlined', 'underlined'], + }, + isInvalid: { + control: 'boolean', + options: [true, false], + }, + isDisabled: { + control: 'boolean', + options: [true, false], + }, + isReadOnly: { + control: 'boolean', + options: [true, false], + }, + }, + args: { + size: 'sm', + variant: 'outlined', + isInvalid: false, + isDisabled: false, + isReadOnly: false, + }, +}; + +export default TimeInputMeta; + +export { TimeInput }; diff --git a/example/storybook-nativewind/src/components/TimeInput/TimeInput.tsx b/example/storybook-nativewind/src/components/TimeInput/TimeInput.tsx new file mode 100644 index 0000000000..dbe2b4753a --- /dev/null +++ b/example/storybook-nativewind/src/components/TimeInput/TimeInput.tsx @@ -0,0 +1,36 @@ +import React, { useState } from 'react'; +import { + TimeInput, + TimeInputHr, + TimeInputMin, + TimeInputMeridiem, + TimeInputMeridiemText, + TimeInputColon, +} from '@/components/ui/time-input'; + +const TimeInputBasic = ({ ...props }: any) => { + const [timeValue, setTimeValue] = useState(null); + return ( + <> + + + + + + + + + + ); +}; + +TimeInputBasic.description = + 'This is a basic TimeInput component example. TimeInputs are used to get time input from the user.'; + +export default TimeInputBasic; +export { TimeInput }; diff --git a/example/storybook-nativewind/src/components/TimeInput/index.nw.stories.mdx b/example/storybook-nativewind/src/components/TimeInput/index.nw.stories.mdx new file mode 100644 index 0000000000..7ae2aace6e --- /dev/null +++ b/example/storybook-nativewind/src/components/TimeInput/index.nw.stories.mdx @@ -0,0 +1,605 @@ +--- +title: gluestack-ui TimeInput Component | Installation, Usage, and API + +description: A component that allows users to input a time value. + +pageTitle: TimeInput + +pageDescription: A component that allows users to input a time value. + +showHeader: true +--- + +import { Meta } from '@storybook/addon-docs'; + + + +import { + TimeInput, + TimeInputHr, + TimeInputMin, + TimeInputMeridiem, + TimeInputMeridiemText, + TimeInputColon, +} from '../../core-components/nativewind/time-input'; +import { transformedCode } from '../../utils'; +import { + AppProvider, + CodePreview, + Table, + TableContainer, + Text, + InlineCode, + CollapsibleCode, + Tabs +} from '@gluestack/design-system'; +import Wrapper from '../../core-components/nativewind/Wrapper'; +import AnatomyImage from '../../extra-components/nativewind/AnatomyImage'; + +This is an illustration of **TimeInput** component. + +<> + + + + + + + + + `, + transformCode: (code) => { + return transformedCode(code); + }, + scope: { + Wrapper, + TimeInput, + TimeInputHr, + TimeInputMin, + TimeInputMeridiem, + TimeInputMeridiemText, + TimeInputColon, + }, + argsType: { + variant: { + control: 'select', + options: ['outlined', 'underlined'], + default: 'outlined', + }, + size: { + control: 'select', + options: ['sm', 'md', 'lg', 'xl'], + default: 'md', + }, + isDisabled: { + control: 'boolean', + default: false, + }, + isInvalid: { + control: 'boolean', + default: false, + }, + isReadOnly: { + control: 'boolean', + default: false, + }, + }, + }} + /> + + +
+ +## Installation + + + + + CLI + + + Manual + + + + +<> + +### Run the following command: + ```bash + npx gluestack-ui add time-input + ``` + + + +<> + +### Step 1: Install the following dependencies: +```bash +npm i @gluestack-ui/time-input +``` + +### Step 2: Copy and paste the following code into your project. + + +```jsx +%%-- File: core-components/nativewind/time-input/index.tsx --%% +``` + + +### Step 3: Update the import paths to match your project setup. + + + + + +## API Reference + +To use this component in your project, include the following import statement in your file. + +```jsx +import { TimeInput } from '@/components/ui/time-input'; +``` + + +```jsx +export default () => ( + + + + + + + + +); +``` + +### Component Props + +This section provides a comprehensive reference list for the component props, detailing descriptions, properties, types, and default behavior for easy project integration. + +#### TimeInput + +It inherits all the properties of React Native's [View](https://reactnative.dev/docs/view) component. + +<> + + + + + + Prop + + + Type + + + Default + + + Description + + + + + + + + isInvalid + + + + bool + + + false + + + {`When true, the timeinput displays an error state.`} + + + + + + isDisabled + + + + bool + + + false + + + {`When true, the timeinput is disabled and cannot be edited.`} + + + + + + isHovered + + + + bool + + + false + + + {`When true, the timeinput displays a hover state.`} + + + + + + isRequired + + + + bool + + + false + + + {`If true, sets aria-required="true" on the minutes and hours feilds of timeinput.`} + + + + + + isReadOnly + + + + bool + + + false + + + {`If true, the timeinput values cannot be edited.`} + + + +
+
+ + + +#### Minutes and Hours Fields + +Contains all TextInput related layout style props and actions. +It inherits all the properties of React Native's [TextInput](https://reactnative.dev/docs/textInput#props) component. + +<> + + + + + + Prop + + + Type + + + Default + + + Description + + + + + + + + isFocused + + + + bool + + + false + + + {`If true, the timeinput minutes and hours feilds displays a focus state.`} + + + + + + isHovered + + + + bool + + + false + + + {`If true, the specific timeinput feilds displays a hover state.`} + + + + + + editable + + + + bool + + + true + + + {`If true, the specific timeinput feild is editable.`} + + + +
+
+ + +#### TimeInputMeridiem + +Contains all button related layout style props and actions. It inherits all the properties of React Native's [Pressable](https://reactnative.dev/docs/pressable) component. + +<> + + + + + + Prop + + + Type + + + Default + + + Description + + + + + + + + isHovered + + + + bool + + + false + + + {`To manually set hover to the meridiem.`} + + + + + + isPressed + + + + bool + + + false + + + {`To manually set pressable state to the meridiem.`} + + + + + + isFocused + + + + bool + + + false + + + {`To manually set focused state to the meridiem.`} + + + +
+
+ + +### Features + +- Keyboard support for actions. +- Support for hover, focus and active states. +- Option to add your styles or use the default styles. + +### Accessibility + +We have outlined the various features that ensure the TimeInput component is accessible to all users, including those with disabilities. These features help ensure that your application is inclusive and meets accessibility standards.Adheres to the [WAI-ARIA design pattern](https://www.w3.org/TR/wai-aria-1.2/#textbox). + +#### Keyboard + +- Setting the `aria-label` and `aria-hint` to help users understand the purpose and function of the TimeInput + +#### Screen Reader + +- Compatible with screen readers such as VoiceOver and Talk-back. +- The `accessible` and `aria-label` props to provide descriptive information about the TimeInput +- Setting `aria-traits` and `aria-hint` to provide contextual information about the various states of the TimeInput, such as "double tap to edit". + +#### Focus Management + +- The `onFocus` and `onBlur` props to manage focus states and provide visual cues to users. This is especially important for users who rely on keyboard navigation. + +#### States + +- In error state, `aria-invalid` will be passed to indicate that the TimeInput has an error, and providing support for an `aria-errormessage` to describe the error in more detail. +- In disabled state, `aria-hidden` will be passed to make input not focusable. +- In required state, `aria-required` will be passed to indicate that the TimeInput is required. + + +### Data Attributes Table + +Component receives states as props as boolean values, which are applied as ```data-*``` attributes. These attributes are then used to style the component via classNames, enabling state-based styling. + +<> + + + + + + State + + + Data Attribute + + + Values + + + + + + + + hover + + + + + data-hover + + + + + true | false + + + + + + + disabled + + + + data-disabled + + + + true | false + + + + + + + focus + + + + data-focus + + + + true | false + + + + + + + invalid + + + + data-invalid + + + + true | false + + + + +
+
+ + +#### Input + +<> + + + + + + Name + + + Value + + + Default + + + + + + + + size + + + + xl | lg | md | sm + + + md + + + + + + variant + + + + outlined | rounded + + + outlined + + + +
+
+ + + + + + + diff --git a/example/storybook-nativewind/src/core-components/nativewind/dependencies.json b/example/storybook-nativewind/src/core-components/nativewind/dependencies.json index 5db25292bc..42b44663b1 100644 --- a/example/storybook-nativewind/src/core-components/nativewind/dependencies.json +++ b/example/storybook-nativewind/src/core-components/nativewind/dependencies.json @@ -208,6 +208,11 @@ "@gluestack-ui/textarea": "latest" } }, + "time-input": { + "dependencies": { + "@gluestack-ui/time-input": "latest" + } + }, "toast": { "dependencies": { "@gluestack-ui/toast": "latest", @@ -234,5 +239,5 @@ "hooks": ["useBreakpointValue"] } }, - "IgnoredComponents": ["bottomsheet", "image-viewer"] + "IgnoredComponents": ["bottomsheet", "image-viewer", "time-input"] } diff --git a/example/storybook-nativewind/src/core-components/nativewind/time-input/index.tsx b/example/storybook-nativewind/src/core-components/nativewind/time-input/index.tsx new file mode 100644 index 0000000000..f61bb59367 --- /dev/null +++ b/example/storybook-nativewind/src/core-components/nativewind/time-input/index.tsx @@ -0,0 +1,257 @@ +'use client'; +import React from 'react'; +import { createTimeInput } from '@gluestack-ui/time-input'; +import { View, Pressable, TextInput, Text } from 'react-native'; +import { tva } from '@gluestack-ui/nativewind-utils/tva'; +import { cssInterop } from 'nativewind'; +import { + withStyleContext, + useStyleContext, +} from '@gluestack-ui/nativewind-utils/withStyleContext'; +import type { VariantProps } from '@gluestack-ui/nativewind-utils'; + +const SCOPE = 'TIMEINPUT'; + +const UITimeInput = createTimeInput({ + Root: withStyleContext(View, SCOPE), + TimeInputHr: TextInput, + TimeInputMin: TextInput, + TimeInputMeridiem: Pressable, + TimeInputMeridiemText: Text, +}); + +cssInterop(UITimeInput, { + className: { + target: 'style', // map className->style + }, +}); + +const timeInputStyle = tva({ + base: 'flex flex-row items-center self-center', + variants: { + size: { + xl: 'gap-5', + lg: 'gap-4', + md: 'gap-3', + sm: 'gap-2', + }, + variant: { + outlined: '', + underlined: '', + }, + }, +}); + +const timeInputFieldStyle = tva({ + base: 'border-background-300 data-[hover=true]:border-outline-400 data-[focus=true]:border-primary-700 data-[focus=true]:data-[hover=true]:border-primary-700 data-[disabled=true]:opacity-40 data-[disabled=true]:data-[hover=true]:border-background-300 text-center placeholder:text-typography-500', + + parentVariants: { + size: { + xl: 'text-xl h-12 w-12', + lg: 'text-lg h-11 w-11', + md: 'text-base h-10 w-10', + sm: 'text-sm h-9 w-9', + }, + variant: { + underlined: + 'border-b data-[invalid=true]:border-b-2 data-[invalid=true]:border-error-700 data-[invalid=true]:data-[hover=true]:border-error-700 data-[invalid=true]:data-[focus=true]:border-error-700 data-[invalid=true]:data-[focus=true]:data-[hover=true]:border-error-700 data-[invalid=true]:data-[disabled=true]:data-[hover=true]:border-error-700', + outlined: + 'rounded border data-[invalid=true]:border-error-700 data-[invalid=true]:data-[hover=true]:border-error-700 data-[invalid=true]:data-[focus=true]:border-error-700 data-[invalid=true]:data-[focus=true]:data-[hover=true]:border-error-700 data-[invalid=true]:data-[disabled=true]:data-[hover=true]:border-error-700 data-[focus=true]:web:ring-1 data-[focus=true]:web:ring-inset data-[focus=true]:web:ring-indicator-primary data-[invalid=true]:web:ring-1 data-[invalid=true]:web:ring-inset data-[invalid=true]:web:ring-indicator-error data-[invalid=true]:data-[focus=true]:data-[hover=true]:web:ring-1 data-[invalid=true]:data-[focus=true]:data-[hover=true]:web:ring-inset data-[invalid=true]:data-[focus=true]:data-[hover=true]:web:ring-indicator-error data-[invalid=true]:data-[disabled=true]:data-[hover=true]:web:ring-1 data-[invalid=true]:data-[disabled=true]:data-[hover=true]:web:ring-inset data-[invalid=true]:data-[disabled=true]:data-[hover=true]:web:ring-indicator-error', + }, + }, +}); + +const timeInputMeridiemTextStyle = tva({ + base: 'web:select-none data-[invalid=true]:text-error-700', + + parentVariants: { + size: { + sm: 'text-sm', + md: 'text-base', + lg: 'text-lg', + xl: 'text-xl', + }, + variant: { + outlined: 'text-typography-0', + underlined: '', + }, + }, +}); + +const timeInputColonStyle = tva({ + base: 'text-sm font-semibold', + variants: { + size: { + xl: 'text-xl', + lg: 'text-lg', + md: 'text-base', + sm: 'text-sm', + }, + }, +}); + +const timeInputMeridiemStyle = tva({ + base: 'rounded flex-row items-center justify-center data-[focus-visible=true]:web:outline-none data-[focus-visible=true]:web:ring-2 data-[disabled=true]:opacity-40', + + parentVariants: { + size: { + xl: 'h-12 w-12', + lg: 'h-11 w-11', + md: 'h-10 w-10', + sm: 'h-9 w-9', + }, + variant: { + outlined: 'bg-primary-500', + underlined: 'border border-background-300', + }, + }, +}); + +type ITimeInputProps = React.ComponentProps & + VariantProps & { className?: string }; +const TimeInput = React.forwardRef< + React.ElementRef, + ITimeInputProps +>(({ className, size = 'md', variant = 'outlined', ...props }, ref) => { + return ( + + ); +}); + +type ITimeInputFieldHrProps = React.ComponentProps & + VariantProps & { className?: string }; + +const TimeInputHr = React.forwardRef< + React.ElementRef, + ITimeInputFieldHrProps +>(({ className, ...props }, ref) => { + const { variant: parentVariant, size: parentSize } = useStyleContext(SCOPE); + + return ( + + ); +}); + +type ITimeInputFieldMinProps = React.ComponentProps & + VariantProps & { className?: string }; + +const TimeInputMin = React.forwardRef< + React.ElementRef, + ITimeInputFieldMinProps +>(({ className, ...props }, ref) => { + const { variant: parentVariant, size: parentSize } = useStyleContext(SCOPE); + + return ( + + ); +}); + +type ITimeInputFieldMeridiemProps = React.ComponentProps< + typeof UITimeInput.Meridiem +> & + VariantProps & { className?: string }; + +const TimeInputMeridiem = React.forwardRef< + React.ElementRef, + ITimeInputFieldMeridiemProps +>(({ className, ...props }, ref) => { + const { size: parentSize, variant: parentVariant } = useStyleContext(SCOPE); + return ( + + ); +}); + +type ITimeInputFieldMeridiemTextProps = React.ComponentProps< + typeof UITimeInput.MeridiemText +> & + VariantProps & { className?: string }; + +const TimeInputMeridiemText = React.forwardRef< + React.ElementRef, + ITimeInputFieldMeridiemTextProps +>(({ className, ...props }, ref) => { + const { size: parentSize, variant: parentVariant } = useStyleContext(SCOPE); + + return ( + + ); +}); + +const TimeInputColon = ({ className, ...props }: { className?: string }) => { + const { size: parentSize } = useStyleContext(SCOPE); + + return ( + + : + + ); +}; + +TimeInput.displayName = 'TimeInput'; +TimeInputHr.displayName = 'TimeInputHr'; +TimeInputMin.displayName = 'TimeInputMin'; +TimeInputMeridiem.displayName = 'TimeInputMeridiem'; +TimeInputMeridiemText.displayName = 'TimeInputMeridiemText'; +TimeInputColon.displayName = 'TimeInputColon'; + +export { + TimeInput, + TimeInputHr, + TimeInputMin, + TimeInputMeridiem, + TimeInputMeridiemText, + TimeInputColon, +}; diff --git a/example/storybook-nativewind/tsconfig.json b/example/storybook-nativewind/tsconfig.json index 8269b1e8b0..98954e61f1 100644 --- a/example/storybook-nativewind/tsconfig.json +++ b/example/storybook-nativewind/tsconfig.json @@ -14,13 +14,13 @@ "@gluestack-ui/button": ["../../packages/unstyled/button/src"], "@gluestack-ui/toast": ["../../packages/unstyled/toast/src"], "@gluestack-ui/alert": ["../../packages/unstyled/alert/src"], + "@gluestack-ui/time-input": ["../../packages/unstyled/time-input/src"], "@gluestack-ui/input": ["../../packages/unstyled/input/src"], "@gluestack-ui/pin-input": ["../../packages/unstyled/pin-input/src"], "@gluestack-ui/checkbox": ["../../packages/unstyled/checkbox/src"], "@gluestack-ui/form-control": [ "../../packages/unstyled/form-control/src" ], - "@/components/ui/utils/*": ["src/core-components/hooks/*"], "@gluestack-ui/modal": ["../../packages/unstyled/modal/src"], "@gluestack-ui/radio": ["../../packages/unstyled/radio/src"], "@gluestack-ui/accordion": ["../../packages/unstyled/accordion/src"], diff --git a/example/storybook-v7/babel.config.js b/example/storybook-v7/babel.config.js index 0ac0d7dc1b..a970def10c 100644 --- a/example/storybook-v7/babel.config.js +++ b/example/storybook-v7/babel.config.js @@ -124,6 +124,10 @@ module.exports = function (api) { __dirname, '../../packages/unstyled/image-viewer/src' ), + '@gluestack-ui/time-input': path.join( + __dirname, + '../../packages/unstyled/time-input/src' + ), '@gluestack-ui/toast': path.join( __dirname, '../../packages/unstyled/toast/src' diff --git a/example/storybook-v7/tsconfig.json b/example/storybook-v7/tsconfig.json index fa0e0cccdd..925633f97d 100644 --- a/example/storybook-v7/tsconfig.json +++ b/example/storybook-v7/tsconfig.json @@ -5,7 +5,10 @@ "baseUrl": ".", "paths": { "@gluestack-ui/pin-input": ["../../packages/unstyled/pin-input/src"], - "@gluestack-ui/image-viewer": ["../../packages/unstyled/image-viewer/src"] + "@gluestack-ui/image-viewer": [ + "../../packages/unstyled/image-viewer/src" + ], + "@gluestack-ui/time-input": ["../../packages/unstyled/time-input/src"] } } } diff --git a/packages/unstyled/image-viewer/README.md b/packages/unstyled/image-viewer/README.md index 69cb883a1c..9c1e003835 100644 --- a/packages/unstyled/image-viewer/README.md +++ b/packages/unstyled/image-viewer/README.md @@ -68,11 +68,7 @@ export default () => ( { - return ( - - ); + return ; }} keyExtractor={(item, index) => `${item.id}-${index}`} /> @@ -89,7 +85,7 @@ export default () => ( | -------- | --------- | ------- | ------------------------------------------------------ | -------- | | isOpen | boolean | false | If true, the image viewer modal will open | Yes | | onClose | function | - | Callback invoked when the image viewer modal is closed | Yes | -| children | ReactNode | - | The content to be rendered inside the image viewer | _ | +| children | ReactNode | - | The content to be rendered inside the image viewer | \_ | ### ImageViewerContent diff --git a/packages/unstyled/time-input/.npmignore b/packages/unstyled/time-input/.npmignore new file mode 100644 index 0000000000..187790b632 --- /dev/null +++ b/packages/unstyled/time-input/.npmignore @@ -0,0 +1,20 @@ +# Dotfiles +.babelrc +.eslintignore +.eslintrc.json +.gitattributes +_config.yml +.editorconfig + + +#Config files +babel.config.js + +# Documents +CONTRIBUTING.md +ISSUE_TEMPLATE.txt +img + +# Test cases +__tests__ +dist/__tests__ diff --git a/packages/unstyled/time-input/CHANGELOG.md b/packages/unstyled/time-input/CHANGELOG.md new file mode 100644 index 0000000000..8cce7554b3 --- /dev/null +++ b/packages/unstyled/time-input/CHANGELOG.md @@ -0,0 +1,5 @@ +# @gluestack-ui/input + +## 0.0.1 + +- Initial release diff --git a/packages/unstyled/time-input/README.md b/packages/unstyled/time-input/README.md new file mode 100644 index 0000000000..729b0e21bb --- /dev/null +++ b/packages/unstyled/time-input/README.md @@ -0,0 +1,61 @@ +# @gluestack-style/input + +## Installation + +To use `@gluestack-ui/input`, all you need to do is install the +`@gluestack-ui/input` package: + +```sh +$ yarn add @gluestack-ui/input + +# or + +$ npm i @gluestack-ui/input +``` + +## Usage + +The Input component is your go-to tool for gathering user input in a sleek and user-friendly text field. Whether you're designing a simple login form or a complex search feature, this component has got you covered. Here's an example how to use this package to create one: + +```jsx +import { Root, Input } from '../components/core/input/styled-components'; +import { createInput } from '@gluestack-ui/input'; +const InputField = createInput({ + Root, + Input, +}); +``` + +## Customizing the input: + +Default styling of all these components can be found in the components/core/input file. For reference, you can view the [source code](https://github.com/gluestack/gluestack-ui/blob/development/example/storybook/src/ui-components/Input/index.tsx) of the styled `input` components. + +```jsx +// import the styles +import { Root, Input } from '../components/core/input/styled-components'; + +// import the createInput function +import { createInput } from '@gluestack-ui/input'; + +//import any icon +import { searchIcon } from '@gluestack/icons'; + +// Understanding the API +const InputField = createInput({ + Root, + Input, +}); + +// Using the input component +export default () => ( + + + + + + +); +``` + +More guides on how to get started are available +[here](https://ui.gluestack.io/docs/components/forms/input). diff --git a/packages/unstyled/time-input/babel.config.js b/packages/unstyled/time-input/babel.config.js new file mode 100644 index 0000000000..d19155f22d --- /dev/null +++ b/packages/unstyled/time-input/babel.config.js @@ -0,0 +1,29 @@ +const path = require('path'); + +module.exports = function (api) { + api.cache(true); + return { + presets: ['babel-preset-expo'], + plugins: [ + process.env.NODE_ENV !== 'production' + ? [ + 'module-resolver', + { + alias: { + ['@gluestack-ui/form-control']: path.resolve( + __dirname, + '../form-control/src' + ), + ['@gluestack-ui/utils']: path.resolve( + __dirname, + '../utils/src' + ), + // ['@gluestack-ui/utils']: path.resolve(__dirname, '../utils/src'), + // For development, we want to alias the library to the source + }, + }, + ] + : ['babel-plugin-react-docgen-typescript', { exclude: 'node_modules' }], + ], + }; +}; diff --git a/packages/unstyled/time-input/package.json b/packages/unstyled/time-input/package.json new file mode 100644 index 0000000000..3ac508683f --- /dev/null +++ b/packages/unstyled/time-input/package.json @@ -0,0 +1,82 @@ +{ + "name": "@gluestack-ui/time-input", + "description": "A universal headless time input component for React Native, Next.js & React", + "version": "0.0.1", + "main": "lib/index", + "module": "lib/index", + "types": "lib/index.d.ts", + "react-native": "src/index", + "source": "src/index", + "typings": "lib/index.d.ts", + "scripts": { + "prepare": "tsc", + "release": "release-it", + "watch": "tsc --watch", + "build": "tsc", + "clean": "rm -rf lib", + "dev:web": "cd example/native && yarn web --clear", + "storybook": "cd example/native/storybook && yarn web" + }, + "devDependencies": { + "@types/react": "^18.0.22", + "@types/react-native": "^0.72.3", + "babel-plugin-transform-remove-console": "^6.9.4", + "react": "^18.1.0", + "react-dom": "^18.1.0", + "react-native": "^0.72.4", + "react-native-builder-bob": "^0.20.1", + "react-native-web": "^0.19.9", + "tsconfig": "7", + "typescript": "^5.6.3" + }, + "dependencies": { + "@gluestack-ui/form-control": "^0.1.19", + "@gluestack-ui/utils": "^0.1.14", + "@react-native-aria/focus": "^0.2.9", + "@react-native-aria/interactions": "0.2.13", + "dayjs": "^1.11.13" + }, + "peerDependencies": { + "react": ">=16", + "react-dom": ">=16" + }, + "homepage": "https://github.com/gluestack/gluestack-ui/tree/main/packages/unstyled/time-input#readme", + "repository": { + "type": "git", + "url": "git+https://github.com/gluestack/gluestack-ui.git" + }, + "files": [ + "lib/", + "src/" + ], + "jest": { + "preset": "jest-expo", + "transform": { + "^.+\\.js$": "/node_modules/react-native/jest/preprocessor.js" + }, + "modulePathIgnorePatterns": [ + "/example/*", + "/lib/" + ], + "transformIgnorePatterns": [ + "node_modules/(?!(@react-native|react-native|expo-asset|expo-constants|@unimodules|react-native-unimodules|expo-font|react-native-svg|@expo/vector-icons|react-native-vector-icons|@react-native-aria/checkbox|@react-native-aria/interactions|@react-native-aria/button|@react-native-aria/switch|@react-native-aria/toggle|@react-native-aria/utils|@react-native-aria/*))" + ], + "setupFiles": [ + "/src/jest/mock.ts" + ] + }, + "keywords": [ + "react", + "native", + "react-native", + "time-input", + "gluestack-ui", + "universal", + "headless", + "typescript", + "component", + "android", + "ios", + "nextjs" + ] +} diff --git a/packages/unstyled/time-input/src/TimeInput.tsx b/packages/unstyled/time-input/src/TimeInput.tsx new file mode 100644 index 0000000000..59ce8522ac --- /dev/null +++ b/packages/unstyled/time-input/src/TimeInput.tsx @@ -0,0 +1,81 @@ +import React, { forwardRef, useState } from 'react'; +import { TimeInputProvider } from './TimeInputContext'; +import { useFormControlContext } from '@gluestack-ui/form-control'; +import { mergeRefs } from '@gluestack-ui/utils'; +import dayjs, { Dayjs } from 'dayjs'; +import type { ITimeInputProps } from './types'; +export const TimeInput = (StyledTimeInputRoot: any) => + forwardRef( + ( + { + children, + isReadOnly, + isDisabled, + isInvalid, + isRequired, + value: externalValue, + onChange, + ...props + }: Omit & { children: React.ReactNode[] }, + ref?: any + ) => { + const hourRef = React.useRef(null); + const minuteRef = React.useRef(null); + const meridiemRef = React.useRef(null); + + const [timeValue, setTimeValue] = useState( + externalValue ? externalValue : dayjs() + ); + const handleTimeChange = (newTimeValue: Dayjs) => { + setTimeValue(newTimeValue); + onChange?.(newTimeValue); + }; + const [meridiemHovered, setMeridiemHovered] = useState(false); + const [meridiemPressed, setMeridiemPressed] = useState(false); + const [meridiem, setMeridiem] = useState(timeValue.format('A')); + + const timeInputProps = useFormControlContext(); + + return ( + + + {children} + + + ); + } + ); diff --git a/packages/unstyled/time-input/src/TimeInputContext.tsx b/packages/unstyled/time-input/src/TimeInputContext.tsx new file mode 100644 index 0000000000..75d1dbfd09 --- /dev/null +++ b/packages/unstyled/time-input/src/TimeInputContext.tsx @@ -0,0 +1,5 @@ +import { createContext } from '@gluestack-ui/utils'; +import type { TimeInputContext } from './types'; + +export const [TimeInputProvider, useTimeInput] = + createContext('TimeInputContext'); diff --git a/packages/unstyled/time-input/src/TimeInputHr.tsx b/packages/unstyled/time-input/src/TimeInputHr.tsx new file mode 100644 index 0000000000..1574caf755 --- /dev/null +++ b/packages/unstyled/time-input/src/TimeInputHr.tsx @@ -0,0 +1,143 @@ +import React, { forwardRef, useMemo, useRef, useState } from 'react'; +import { Platform } from 'react-native'; +import { useFormControl } from '@gluestack-ui/form-control'; +import { useTimeInput } from './TimeInputContext'; +import { mergeRefs } from '@gluestack-ui/utils'; +import { useHover } from '@react-native-aria/interactions'; +import { useFocusRing } from '@react-native-aria/focus'; +import type { ITimeInputFieldProps } from './types'; + +export const TimeInputHr = (StyledTimeInputHr: any) => + forwardRef( + ( + { + children, + onKeyPress, + 'isHovered': isHoveredProp = true, + 'aria-label': ariaLabel = 'Hours', + editable, + 'isDisabled': isDisabledProp = false, + 'isFocused': isFocusedProp = false, + 'isFocusVisible': isFocusVisibleProp, + ...props + }: ITimeInputFieldProps & { children: React.ReactNode }, + ref?: any + ) => { + const { + isDisabled, + isReadOnly, + isInvalid, + isRequired, + value, + setTimeValue, + minuteRef, + hourRef, + } = useTimeInput('TimeInputContext'); + + const inputProps = useFormControl({ + isDisabled: isDisabledProp, + isInvalid: isInvalid, + isReadOnly: isReadOnly, + isRequired: isRequired, + id: props.id, + }); + + const inputRef = useRef(null); + const { isHovered }: any = useHover({}, inputRef); + + const [isFocused, setIsFocused] = useState(false); + const handleFocus = (focusState: boolean, callback: any) => { + setIsFocused(focusState); + callback(); + }; + + const { isFocusVisible }: any = useFocusRing(); + const mergedRef = mergeRefs([ref, inputRef, hourRef]); + + const editableProp = useMemo(() => { + if (editable !== undefined) { + return editable; + } else { + return isDisabled || + inputProps.isDisabled || + isReadOnly || + isDisabledProp + ? false + : true; + } + }, [ + isDisabled, + inputProps.isDisabled, + isReadOnly, + editable, + isDisabledProp, + ]); + + const handleChange = (newHoursInt: string) => { + const newHours = newHoursInt.replace(/[^0-9]/g, ''); + if (newHours === newHoursInt) { + const newTimeValue = newHours + ? value + .set('hour', parseInt(newHours)) + .second(new Date().getSeconds()) + : value.set('hour', 0).second(new Date().getSeconds()); + setTimeValue(newTimeValue); + if (parseInt(newHours) > 1) { + minuteRef.current.focus(); + } + } + }; + + return ( + { + e.persist(); + onKeyPress && onKeyPress(e); + }} + onFocus={(e: any) => { + handleFocus(true, () => props.onFocus?.(e)); + }} + onBlur={(e: any) => { + handleFocus(false, () => props.onBlur?.(e)); + }} + value={(value.get('hour') % 12).toString().padStart(2, '0')} + onChangeText={handleChange} + ref={mergedRef} + keyboardType="number-pad" + selectTextOnFocus={true} + > + {children} + + ); + } + ); diff --git a/packages/unstyled/time-input/src/TimeInputMeridiem.tsx b/packages/unstyled/time-input/src/TimeInputMeridiem.tsx new file mode 100644 index 0000000000..1c814df97b --- /dev/null +++ b/packages/unstyled/time-input/src/TimeInputMeridiem.tsx @@ -0,0 +1,131 @@ +import React, { forwardRef, useRef } from 'react'; +import { useTimeInput } from './TimeInputContext'; +import { composeEventHandlers } from '@gluestack-ui/utils'; +import { mergeRefs } from '@gluestack-ui/utils'; +import { useHover, usePress } from '@react-native-aria/interactions'; +import { useFocusRing, useFocus } from '@react-native-aria/focus'; +import type { ITimeInputMeridiemProps } from './types'; + +export const TimeInputMeridiem = (StyledTimeInputMeridiem: any) => + forwardRef( + ( + { + children, + isHovered: isHoveredProp, + isFocused: isFocusedProp, + isPressed: isPressedProp, + isFocusVisible: isFocusVisibleProp, + isDisabled: isDisabledProp, + ...props + }: ITimeInputMeridiemProps & { + children: React.ReactNode; + }, + ref?: any + ) => { + const { + isDisabled, + isReadOnly, + isInvalid, + value, + meridiem, + setMeridiem, + isRequired, + setTimeValue, + setMeridiemHovered, + setMeridiemPressed, + meridiemRef, + } = useTimeInput('TimeInputContext'); + + const { isFocusVisible, focusProps: focusRingProps }: any = + useFocusRing(); + const { isFocused, focusProps } = useFocus(); + + const { pressProps: pressableProps, isPressed } = usePress({ + isDisabled, + }); + + const pressableRef = useRef(null); + const { isHovered } = useHover({}, pressableRef); + + const mergedRef = mergeRefs([ref, pressableRef, meridiemRef]); + + const updateMeridiem = (meridiem: string) => { + if (meridiem === 'AM') { + const newTimeValue = value.set('hour', value.get('hour') + 12); + setMeridiem('PM'); + setTimeValue(newTimeValue); + } else { + const newTimeValue = value.set('hour', value.get('hour') - 12); + setMeridiem('AM'); + setTimeValue(newTimeValue); + } + }; + + return ( + setMeridiemHovered(true)} + onHoverOut={() => setMeridiemHovered(false)} + onPressIn={composeEventHandlers( + props?.onPressIn, + pressableProps.onPressIn, + () => { + if (isPressedProp) { + setMeridiemPressed(true); + } + } + )} + onPressOut={composeEventHandlers( + props?.onPressOut, + pressableProps.onPressOut, + () => { + if (isPressedProp) { + setMeridiemPressed(false); + } + } + )} + onPress={composeEventHandlers(props?.onPress, () => + updateMeridiem(meridiem) + )} + // @ts-ignore - web only + onFocus={composeEventHandlers( + composeEventHandlers(props?.onFocus, focusProps.onFocus), + focusRingProps.onFocus + )} + // @ts-ignore - web only + onBlur={composeEventHandlers( + composeEventHandlers(props?.onBlur, focusProps.onBlur), + focusRingProps.onBlur + )} + > + {children} + + ); + } + ); diff --git a/packages/unstyled/time-input/src/TimeInputMeridiemText.tsx b/packages/unstyled/time-input/src/TimeInputMeridiemText.tsx new file mode 100644 index 0000000000..0ba6b4bfc4 --- /dev/null +++ b/packages/unstyled/time-input/src/TimeInputMeridiemText.tsx @@ -0,0 +1,34 @@ +import React, { forwardRef } from 'react'; +import { useTimeInput } from './TimeInputContext'; + +export const TimeInputMeridiemText = (StyledTimeInputMeridiemText: any) => + forwardRef(({ ...props }: any, ref?: any) => { + const { + isDisabled, + meridiemHovered, + meridiem, + meridiemPressed, + isInvalid, + } = useTimeInput('TimeInputContext'); + + return ( + + {meridiem} + + ); + }); diff --git a/packages/unstyled/time-input/src/TimeInputMin.tsx b/packages/unstyled/time-input/src/TimeInputMin.tsx new file mode 100644 index 0000000000..f33eeee44e --- /dev/null +++ b/packages/unstyled/time-input/src/TimeInputMin.tsx @@ -0,0 +1,138 @@ +import React, { forwardRef, useMemo, useRef, useState } from 'react'; +import { Platform } from 'react-native'; +import { useFormControl } from '@gluestack-ui/form-control'; +import { useTimeInput } from './TimeInputContext'; +import { mergeRefs } from '@gluestack-ui/utils'; +import { useHover } from '@react-native-aria/interactions'; +import { useFocusRing } from '@react-native-aria/focus'; +import type { ITimeInputFieldProps } from './types'; + +export const TimeInputMin = (StyledTimeInputMin: any) => + forwardRef( + ( + { + children, + onKeyPress, + 'isHovered': isHoveredProp = true, + 'aria-label': ariaLabel = 'Minutes', + editable, + 'isDisabled': isDisabledProp = false, + 'isFocused': isFocusedProp = false, + 'isFocusVisible': isFocusVisibleProp, + ...props + }: ITimeInputFieldProps & { children: React.ReactNode }, + ref?: any + ) => { + const { + isDisabled, + isReadOnly, + isInvalid, + isRequired, + value, + setTimeValue, + minuteRef, + meridiemRef, + } = useTimeInput('TimeInputContext'); + + const inputRef = useRef(null); + const { isHovered }: any = useHover({}, inputRef); + + const inputProps = useFormControl({ + isDisabled: isDisabledProp, + isInvalid: isInvalid, + isReadOnly: isReadOnly, + isRequired: isRequired, + id: props.id, + }); + + const [isFocused, setIsFocused] = useState(false); + const handleFocus = (focusState: boolean, callback: any) => { + setIsFocused(focusState); + callback(); + }; + + const { isFocusVisible }: any = useFocusRing(); + + const mergedRef = mergeRefs([ref, inputRef, minuteRef]); + + const editableProp = useMemo(() => { + if (editable !== undefined) { + return editable; + } else { + return isDisabled || inputProps.isDisabled || isReadOnly + ? false + : true; + } + }, [isDisabled, inputProps.isDisabled, isReadOnly, editable]); + + const handleChange = (newMinutesInt: string) => { + const newMinutes = newMinutesInt.replace(/[^0-9]/g, ''); + if (newMinutes === newMinutesInt) { + const newTimeValue = newMinutes + ? value + .set('minute', parseInt(newMinutes)) + .second(new Date().getSeconds()) + : value.set('minute', 0).second(new Date().getSeconds()); + setTimeValue(newTimeValue); + if (parseInt(newMinutes) > 5) { + if (Platform.OS === 'web') { + meridiemRef.current.focus(); + } + minuteRef.current.blur(); + } + } + }; + + return ( + { + e.persist(); + onKeyPress && onKeyPress(e); + }} + onFocus={(e: any) => { + handleFocus(true, () => props.onFocus?.(e)); + }} + onBlur={(e: any) => { + handleFocus(false, () => props.onBlur?.(e)); + }} + ref={mergedRef} + value={value.get('minute').toString().padStart(2, '0')} + onChangeText={handleChange} + keyboardType="number-pad" + selectTextOnFocus={true} + > + {children} + + ); + } + ); diff --git a/packages/unstyled/time-input/src/index.tsx b/packages/unstyled/time-input/src/index.tsx new file mode 100644 index 0000000000..3dd4bbf450 --- /dev/null +++ b/packages/unstyled/time-input/src/index.tsx @@ -0,0 +1,45 @@ +import { TimeInput as TimeInputComponent } from './TimeInput'; +import { TimeInputHr } from './TimeInputHr'; +import { TimeInputMin } from './TimeInputMin'; +import type { ITimeInputComponentType } from './types'; +import { TimeInputMeridiem } from './TimeInputMeridiem'; +import { TimeInputMeridiemText } from './TimeInputMeridiemText'; + +export const createTimeInput = < + Root, + TimeInputHr, + TimeInputMin, + TimeInputMeridiem, + TimeInputMeridiemText +>({ + Root, + TimeInputHr: Hr, + TimeInputMin: Min, + TimeInputMeridiem: Meridiem, + TimeInputMeridiemText: MeridiemText, +}: { + Root: React.ComponentType; + TimeInputHr: React.ComponentType; + TimeInputMin: React.ComponentType; + TimeInputMeridiem: React.ComponentType; + TimeInputMeridiemText: React.ComponentType; +}) => { + const TimeInput = TimeInputComponent(Root) as any; + TimeInput.Hr = TimeInputHr(Hr); + TimeInput.Min = TimeInputMin(Min); + TimeInput.Meridiem = TimeInputMeridiem(Meridiem); + TimeInput.MeridiemText = TimeInputMeridiemText(MeridiemText); + TimeInput.displayName = 'TimeInput'; + TimeInput.Hr.displayName = 'TimeInput.Hr'; + TimeInput.Min.displayName = 'TimeInput.Min'; + TimeInput.Meridiem.displayName = 'TimeInput.Meridiem'; + TimeInput.MeridiemText.displayName = 'TimeInput.MeridiemText'; + + return TimeInput as ITimeInputComponentType< + Root, + TimeInputHr, + TimeInputMin, + TimeInputMeridiem, + TimeInputMeridiemText + >; +}; diff --git a/packages/unstyled/time-input/src/types.ts b/packages/unstyled/time-input/src/types.ts new file mode 100644 index 0000000000..f46765c2f7 --- /dev/null +++ b/packages/unstyled/time-input/src/types.ts @@ -0,0 +1,183 @@ +import { Dayjs } from 'dayjs'; +export interface TimeInputContext { + isDisabled?: boolean; + isInvalid?: boolean; + isReadOnly?: boolean; + isRequired?: boolean; + timeInputFieldRef?: any; //remove it + value: Dayjs; + setTimeValue: (value: Dayjs) => void; + meridiem: string; + setMeridiem: (meridiem: string) => void; + meridiemHovered: boolean; + setMeridiemHovered: (meridiemHovered: boolean) => void; + meridiemPressed: boolean; + setMeridiemPressed: (meridiemPressed: boolean) => void; + hourRef: React.RefObject; + minuteRef: React.RefObject; + meridiemRef: React.RefObject; +} +export interface ITimeInputProps { + /** + * If true, the input will indicate an error. + */ + isInvalid?: boolean; + /** + * If true, the input will be disabled. + */ + isDisabled?: boolean; + /** + * If true, the input will be hovered. + */ + isHovered?: boolean; + /** + * If true, prevents the value of the input from being edited. + */ + isReadOnly?: boolean; + /** + * This will set aria-required="true" on web when passed in formcontrol. + */ + isRequired?: boolean; + /** + * value that will be set to the input + */ + value?: Dayjs; + /** + * callback function that will be called when the value changes + */ + onChange?: (value: Dayjs) => void; +} +export interface ITimeInputFieldProps { + /** + * If true, the input will be hovered. + */ + 'isHovered'?: boolean; + /** + * If true, the input will be disabled. + */ + 'isDisabled'?: boolean; + /** + * the input will be editable. + */ + 'editable'?: boolean; + /** + * If true, the input will be focused. + */ + 'isFocused'?: boolean; + /** + * If true, the input will be focus visible. + */ + 'isFocusVisible'?: boolean; + /** + * aria-label for the input + */ + 'aria-label'?: string; + /** + * callback function that will be called when the key is pressed + */ + 'onKeyPress'?: (e: any) => void; + /** + * callback function that will be called when the input is focused + */ + 'onFocus'?: (e: any) => void; + /** + * callback function that will be called when the input is blurred + */ + 'onBlur'?: (e: any) => void; + /** + * id for the input + */ + 'id'?: string; +} +export interface ITimeInputMeridiemProps { + /** + * If true, the input will be on active state on press. + */ + isPressed?: boolean; + /** + * If true, the input will be hovered. + */ + isHovered?: boolean; + /** + * callback function that will be called when the input is pressed + */ + onPress?: () => void; + /** + * callback function that will be called when the input is pressed in + */ + onPressIn?: () => void; + /** + * callback function that will be called when the input is pressed out + */ + onPressOut?: () => void; + /** + * If true, the input will be focused. + */ + isFocused?: boolean; + /** + * If true, the input will be focus visible. + */ + isFocusVisible?: boolean; + /** + * callback function that will be called when the input is focused + */ + onFocus?: (e: any) => void; + /** + * callback function that will be called when the input is blurred + */ + onBlur?: (e: any) => void; +} + +export type ITimeInputComponentType = + React.ForwardRefExoticComponent< + React.RefAttributes & React.PropsWithoutRef & ITimeInputProps + > & { + Hr: React.ForwardRefExoticComponent< + React.RefAttributes
& React.PropsWithoutRef
& ITimeInputFieldProps + >; + Min: React.ForwardRefExoticComponent< + React.RefAttributes & + React.PropsWithoutRef & + ITimeInputFieldProps + >; + Meridiem: React.ForwardRefExoticComponent< + React.RefAttributes & + React.PropsWithoutRef & + ITimeInputMeridiemProps + >; + MeridiemText: React.ForwardRefExoticComponent< + React.RefAttributes & React.PropsWithoutRef + >; + Text: React.FC; + }; +export interface ITimeInputMeridiemProps { + /** + * If true, the input will be on active state on press. + */ + isPressed?: boolean; + /** + * If true, the input will be disabled. + */ + isDisabled?: boolean; + /** + * If true, the input will be hovered. + */ + isHovered?: boolean; + /** + * If true, the input will be focused. + */ + isFocused?: boolean; + /** + * If true, the input will be focus visible. + */ + isFocusVisible?: boolean; + children: JSX.Element | Array | ((props: any) => JSX.Element); + /** + * callback function that will be called when the input is focused + */ + onFocus?: (e: any) => void; + /** + * callback function that will be called when the input is blurred + */ + onBlur?: (e: any) => void; +} diff --git a/packages/unstyled/time-input/tsconfig.json b/packages/unstyled/time-input/tsconfig.json new file mode 100644 index 0000000000..ac8f6075d2 --- /dev/null +++ b/packages/unstyled/time-input/tsconfig.json @@ -0,0 +1,31 @@ +{ + "include": ["./src"], + "exclude": ["node_modules", "example"], + "paths": {}, + "compilerOptions": { + "ignoreDeprecations": "5.0", + "noEmit": false, + "declaration": true, + "allowJs": true, + "allowUnreachableCode": false, + "allowUnusedLabels": true, + "esModuleInterop": true, + "verbatimModuleSyntax": true, + "forceConsistentCasingInFileNames": true, + "jsx": "preserve", + "lib": ["esnext", "dom"], + "module": "esnext", + "moduleResolution": "node", + "noFallthroughCasesInSwitch": true, + "noImplicitReturns": true, + "noImplicitUseStrict": false, + "noStrictGenericChecks": false, + "noUnusedLocals": false, + "noUnusedParameters": true, + "resolveJsonModule": true, + "skipLibCheck": true, + "strict": true, + "target": "esnext", + "outDir": "./lib" + } +} diff --git a/yarn.lock b/yarn.lock index 4882a525c6..f203916d6e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9911,7 +9911,7 @@ data-view-byte-offset@^1.0.1: es-errors "^1.3.0" is-data-view "^1.0.1" -dayjs@^1.8.15: +dayjs@^1.11.13, dayjs@^1.8.15: version "1.11.13" resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.11.13.tgz#92430b0139055c3ebb60150aa13e860a4b5a366c" integrity sha512-oaMBel6gjolK862uaPQOVTA7q3TZhuSvuMQAAglQDOWYO9A91IrAOUJEyKVlqJlHE0vq5p5UXxzdPfMH/x6xNg==