-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(components): add basic input and story
- Loading branch information
1 parent
04fbf57
commit 10cb8ee
Showing
4 changed files
with
152 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Field } from '@elements/Field' | ||
import { Label } from '@elements/Label' | ||
import { inputStyle } from '@fields/shared/StyledInputBox' | ||
import { forwardRef, useId, type ComponentProps } from 'react' | ||
import { IMaskInput } from 'react-imask' | ||
import styled from 'styled-components' | ||
|
||
import type { Promisable } from 'type-fest' | ||
|
||
export type PhoneInputProps = { | ||
error?: string | ||
label: string | ||
onChange: (nextValue: string | undefined) => Promisable<void> | ||
value: string | undefined | ||
} & Omit<ComponentProps<'input'>, 'onChange' | 'value'> | ||
|
||
export const PhoneInput = forwardRef<HTMLInputElement, PhoneInputProps>(({ label, onChange, value, ...props }, ref) => { | ||
const id = useId() | ||
|
||
return ( | ||
<Field> | ||
<Label htmlFor={id}>{label}</Label> | ||
<StyledIMaskInput | ||
ref={ref} | ||
id={id} | ||
mask={[ | ||
{ definitions: { '#': /[6-7]/, '@': /0/ }, mask: '@# 00 00 00 00' }, | ||
{ definitions: { '#': /[1-9]/, '+': /[\\+]/ }, mask: '+#[000] 000 000 000 000 000' } | ||
]} | ||
onAccept={(nextValue: string) => { | ||
onChange(nextValue) | ||
}} | ||
unmask | ||
value={value} | ||
{...props} | ||
/> | ||
</Field> | ||
) | ||
}) | ||
|
||
const StyledIMaskInput = styled(IMaskInput)` | ||
${inputStyle} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { useState } from 'react' | ||
|
||
import { Output } from '../../.storybook/components/Output' | ||
import { ARG_TYPE } from '../../.storybook/constants' | ||
import { generateStoryDecorator } from '../../.storybook/utils/generateStoryDecorator' | ||
import { PhoneInput, useFieldControl } from '../../src' | ||
|
||
import type { PhoneInputProps } from '../../src' | ||
import type { Meta } from '@storybook/react' | ||
|
||
/* eslint-disable sort-keys-fix/sort-keys-fix */ | ||
const meta: Meta<PhoneInputProps> = { | ||
title: 'Fields/PhoneInput', | ||
component: PhoneInput, | ||
|
||
argTypes: { | ||
// disabled: ARG_TYPE.OPTIONAL_BOOLEAN, | ||
// error: ARG_TYPE.OPTIONAL_STRING, | ||
// isErrorMessageHidden: ARG_TYPE.OPTIONAL_BOOLEAN, | ||
// isLabelHidden: ARG_TYPE.OPTIONAL_BOOLEAN, | ||
// isLight: ARG_TYPE.OPTIONAL_BOOLEAN, | ||
// isRequired: ARG_TYPE.OPTIONAL_BOOLEAN, | ||
// isTransparent: ARG_TYPE.OPTIONAL_BOOLEAN, | ||
// isUndefinedWhenDisabled: ARG_TYPE.OPTIONAL_BOOLEAN, | ||
onChange: ARG_TYPE.NO_CONTROL_INPUT, | ||
// readOnly: ARG_TYPE.OPTIONAL_BOOLEAN, | ||
value: ARG_TYPE.OPTIONAL_STRING | ||
}, | ||
|
||
args: { | ||
// disabled: false, | ||
// error: '', | ||
// isErrorMessageHidden: false, | ||
// isLabelHidden: false, | ||
// isLight: false, | ||
// isRequired: true, | ||
// isTransparent: false, | ||
// isUndefinedWhenDisabled: false, | ||
label: 'A phone number input' | ||
// name: 'myNumberInput', | ||
// placeholder: 'Pick a number', | ||
// readOnly: false | ||
}, | ||
|
||
decorators: [ | ||
generateStoryDecorator({ | ||
box: { width: 640 }, | ||
withBackgroundButton: true | ||
}) | ||
] | ||
} | ||
/* eslint-enable sort-keys-fix/sort-keys-fix */ | ||
|
||
export default meta | ||
|
||
export function _PhoneInput(props: PhoneInputProps) { | ||
const [outputValue, setOutputValue] = useState<string | undefined | '∅'>('∅') | ||
|
||
const { controlledOnChange, controlledValue } = useFieldControl(props.value, setOutputValue, '' as any) | ||
|
||
return ( | ||
<> | ||
<PhoneInput {...props} onChange={controlledOnChange} value={controlledValue} /> | ||
|
||
{outputValue !== '∅' && <Output value={outputValue} />} | ||
</> | ||
) | ||
} |